Class: Fixnum

Inherits:
Integer show all
Defined in:
src/numeric.c

Overview

Fixnum Class

Instance Method Summary collapse

Methods inherited from Integer

#ceil, #floor, #to_i, #to_int

Methods included from Integral

#div, #downto, #negative?, #next, #nonzero?, #positive?, #step, #times, #upto, #zero?

Methods inherited from Numeric

#**, #[email protected], #[email protected], #/, #<, #<=, #<=>, #>, #>=, #abs, #finite?, #infinite?, #quo

Methods included from Comparable

#<, #<=, #>, #>=, #between?, #clamp

Instance Method Details

#%(other) ⇒ Object #modulo(other) ⇒ Object

Returns fix modulo other. See numeric.divmod for more information.


800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'src/numeric.c', line 800

static mrb_value
fix_mod(mrb_state *mrb, mrb_value x)
{
  mrb_value y;
  mrb_int a;

  mrb_get_args(mrb, "o", &y);
  a = mrb_fixnum(x);
  if (mrb_fixnum_p(y)) {
    mrb_int b, mod;

    if ((b=mrb_fixnum(y)) == 0) {
#ifdef MRB_WITHOUT_FLOAT
      /* ZeroDivisionError */
      return mrb_fixnum_value(0);
#else
      return mrb_float_value(mrb, NAN);
#endif
    }
    fixdivmod(mrb, a, b, 0, &mod);
    return mrb_fixnum_value(mod);
  }
#ifdef MRB_WITHOUT_FLOAT
  mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
#else
  else {
    mrb_float mod;

    flodivmod(mrb, (mrb_float)a, mrb_to_flo(mrb, y), 0, &mod);
    return mrb_float_value(mrb, mod);
  }
#endif
}

#&(integer) ⇒ Object

Bitwise AND.


966
967
968
969
970
971
972
973
# File 'src/numeric.c', line 966

static mrb_value
fix_and(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  bit_op(x, y, and, &);
}

#*(numeric) ⇒ Object

Performs multiplication: the class of the resulting object depends on the class of numeric and on the magnitude of the result.


753
754
755
756
757
758
759
760
# File 'src/numeric.c', line 753

static mrb_value
fix_mul(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  return mrb_fixnum_mul(mrb, x, y);
}

#+(numeric) ⇒ Object

Performs addition: the class of the resulting object depends on the class of numeric and on the magnitude of the result.


1214
1215
1216
1217
1218
1219
1220
1221
# File 'src/numeric.c', line 1214

static mrb_value
fix_plus(mrb_state *mrb, mrb_value self)
{
  mrb_value other;

  mrb_get_args(mrb, "o", &other);
  return mrb_fixnum_plus(mrb, self, other);
}

#-(numeric) ⇒ Object

Performs subtraction: the class of the resulting object depends on the class of numeric and on the magnitude of the result.


1257
1258
1259
1260
1261
1262
1263
1264
# File 'src/numeric.c', line 1257

static mrb_value
fix_minus(mrb_state *mrb, mrb_value self)
{
  mrb_value other;

  mrb_get_args(mrb, "o", &other);
  return mrb_fixnum_minus(mrb, self, other);
}

#<<(count) ⇒ Integer, Float

Shifts fix left count positions (right if count is negative).

Returns:


1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
# File 'src/numeric.c', line 1079

static mrb_value
fix_lshift(mrb_state *mrb, mrb_value x)
{
  mrb_int width, val;

  mrb_get_args(mrb, "i", &width);
  if (width == 0) {
    return x;
  }
  val = mrb_fixnum(x);
  if (val == 0) return x;
  if (width < 0) {
    return rshift(val, -width);
  }
  return lshift(mrb, val, width);
}

#==(other) ⇒ Boolean

Return true if fix equals other numerically.

1 == 2 #=> false 1 == 1.0 #=> true

Returns:

  • (Boolean)

907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'src/numeric.c', line 907

static mrb_value
fix_equal(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  switch (mrb_type(y)) {
  case MRB_TT_FIXNUM:
    return mrb_bool_value(mrb_fixnum(x) == mrb_fixnum(y));
#ifndef MRB_WITHOUT_FLOAT
  case MRB_TT_FLOAT:
    return mrb_bool_value((mrb_float)mrb_fixnum(x) == mrb_float(y));
#endif
  default:
    return mrb_false_value();
  }
}

#>>(count) ⇒ Integer, Float

Shifts fix right count positions (left if count is negative).

Returns:


1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
# File 'src/numeric.c', line 1104

static mrb_value
fix_rshift(mrb_state *mrb, mrb_value x)
{
  mrb_int width, val;

  mrb_get_args(mrb, "i", &width);
  if (width == 0) {
    return x;
  }
  val = mrb_fixnum(x);
  if (val == 0) return x;
  if (width < 0) {
    return lshift(mrb, val, -width);
  }
  return rshift(val, width);
}

#^(integer) ⇒ Object

Bitwise EXCLUSIVE OR.


1000
1001
1002
1003
1004
1005
1006
1007
# File 'src/numeric.c', line 1000

static mrb_value
fix_xor(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  bit_op(x, y, or, ^);
}

#divmod(numeric) ⇒ Array

See Numeric#divmod.

Returns:


840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'src/numeric.c', line 840

static mrb_value
fix_divmod(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);

  if (mrb_fixnum_p(y)) {
    mrb_int div, mod;

    if (mrb_fixnum(y) == 0) {
#ifdef MRB_WITHOUT_FLOAT
      return mrb_assoc_new(mrb, mrb_fixnum_value(0), mrb_fixnum_value(0));
#else
      return mrb_assoc_new(mrb, ((mrb_fixnum(x) == 0) ?
                                 mrb_float_value(mrb, NAN):
                                 mrb_float_value(mrb, INFINITY)),
                           mrb_float_value(mrb, NAN));
#endif
    }
    fixdivmod(mrb, mrb_fixnum(x), mrb_fixnum(y), &div, &mod);
    return mrb_assoc_new(mrb, mrb_fixnum_value(div), mrb_fixnum_value(mod));
  }
#ifdef MRB_WITHOUT_FLOAT
  mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
#else
  else {
    mrb_float div, mod;
    mrb_value a, b;

    flodivmod(mrb, (mrb_float)mrb_fixnum(x), mrb_to_flo(mrb, y), &div, &mod);
    a = mrb_float_value(mrb, div);
    b = mrb_float_value(mrb, mod);
    return mrb_assoc_new(mrb, a, b);
  }
#endif
}

#eql?(numeric) ⇒ Boolean

Returns true if num and numeric are the same type and have equal values.

1 == 1.0 #=> true 1.eql?(1.0) #=> false (1.0).eql?(1.0) #=> true

Returns:

  • (Boolean)

Returns:

  • (Boolean)

287
288
289
290
291
292
293
294
295
# File 'src/numeric.c', line 287

static mrb_value
fix_eql(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  if (!mrb_fixnum_p(y)) return mrb_false_value();
  return mrb_bool_value(mrb_fixnum(x) == mrb_fixnum(y));
}

#to_s(base = 10) ⇒ String

Returns a string containing the representation of fix radix base (between 2 and 36).

12345.to_s #=> “12345” 12345.to_s(2) #=> “11000000111001” 12345.to_s(8) #=> “30071” 12345.to_s(10) #=> “12345” 12345.to_s(16) #=> “3039” 12345.to_s(36) #=> “9ix”

Returns:


1312
1313
1314
1315
1316
1317
1318
1319
# File 'src/numeric.c', line 1312

static mrb_value
fix_to_s(mrb_state *mrb, mrb_value self)
{
  mrb_int base = 10;

  mrb_get_args(mrb, "|i", &base);
  return mrb_fixnum_to_str(mrb, self, base);
}

#to_fObject

15.2.8.3.23


1131
1132
1133
1134
1135
# File 'src/numeric.c', line 1131

static mrb_value
fix_to_f(mrb_state *mrb, mrb_value num)
{
  return mrb_float_value(mrb, (mrb_float)mrb_fixnum(num));
}

#to_s(base = 10) ⇒ String

Returns a string containing the representation of fix radix base (between 2 and 36).

12345.to_s #=> “12345” 12345.to_s(2) #=> “11000000111001” 12345.to_s(8) #=> “30071” 12345.to_s(10) #=> “12345” 12345.to_s(16) #=> “3039” 12345.to_s(36) #=> “9ix”

Returns:


1312
1313
1314
1315
1316
1317
1318
1319
# File 'src/numeric.c', line 1312

static mrb_value
fix_to_s(mrb_state *mrb, mrb_value self)
{
  mrb_int base = 10;

  mrb_get_args(mrb, "|i", &base);
  return mrb_fixnum_to_str(mrb, self, base);
}

#|(integer) ⇒ Object

Bitwise OR.


983
984
985
986
987
988
989
990
# File 'src/numeric.c', line 983

static mrb_value
fix_or(mrb_state *mrb, mrb_value x)
{
  mrb_value y;

  mrb_get_args(mrb, "o", &y);
  bit_op(x, y, or, |);
}

#~Integer

One’s complement: returns a number where each bit is flipped. ex.0—00001 (1)-> 1—11110 (-2) ex.0—00010 (2)-> 1—11101 (-3) ex.0—00100 (4)-> 1—11011 (-5)

Returns:


936
937
938
939
940
941
942
# File 'src/numeric.c', line 936

static mrb_value
fix_rev(mrb_state *mrb, mrb_value num)
{
  mrb_int val = mrb_fixnum(num);

  return mrb_fixnum_value(~val);
}