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

#**, #/, #<, #<=, #<=>, #>, #>=, #__coerce_step_counter, #div, #downto, #next, #quo, #step, #times, #upto

Methods inherited from Numeric

#+@, #-@, #abs, #finite?, #infinite?, #negative?, #nonzero?, #positive?, #to_r, #zero?

Methods included from Comparable

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

Instance Method Details

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

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



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'src/numeric.c', line 886

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

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

    if (b == 0) {
#ifdef MRB_WITHOUT_FLOAT
      /* ZeroDivisionError */
      return mrb_fixnum_value(0);
#else
      if (a > 0) return mrb_float_value(mrb, INFINITY);
      if (a < 0) return mrb_float_value(mrb, INFINITY);
      return mrb_float_value(mrb, NAN);
#endif
    }
    fixdivmod(mrb, a, b, NULL, &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), NULL, &mod);
    return mrb_float_value(mrb, mod);
  }
#endif
}

#&(integer) ⇒ Object

Bitwise AND.



1054
1055
1056
1057
1058
1059
1060
1061
# File 'src/numeric.c', line 1054

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.



839
840
841
842
843
844
845
846
# File 'src/numeric.c', line 839

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

  mrb_get_args(mrb, "o", &y);
  return 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.



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

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

  mrb_get_args(mrb, "o", &other);
  return 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.



1370
1371
1372
1373
1374
1375
1376
1377
# File 'src/numeric.c', line 1370

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

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

#<<(count) ⇒ Integer, Float

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

Returns:



1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
# File 'src/numeric.c', line 1167

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)


995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'src/numeric.c', line 995

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:



1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'src/numeric.c', line 1192

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.



1088
1089
1090
1091
1092
1093
1094
1095
# File 'src/numeric.c', line 1088

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:



928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'src/numeric.c', line 928

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_int_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)


368
369
370
371
372
373
374
375
376
# File 'src/numeric.c', line 368

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:



1425
1426
1427
1428
1429
1430
1431
1432
# File 'src/numeric.c', line 1425

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



1219
1220
1221
1222
1223
# File 'src/numeric.c', line 1219

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:



1425
1426
1427
1428
1429
1430
1431
1432
# File 'src/numeric.c', line 1425

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.



1071
1072
1073
1074
1075
1076
1077
1078
# File 'src/numeric.c', line 1071

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:



1024
1025
1026
1027
1028
1029
1030
# File 'src/numeric.c', line 1024

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

  return mrb_fixnum_value(~val);
}