Class: Fixnum
Overview
******************************************************************
Holds Integer values that can be represented in a native machine word
(minus 1 bit). If any operation on a Fixnum exceeds this range, the value
is automatically converted to a Bignum.
Fixnum objects have immediate value. This means that when they are assigned
or passed as parameters, the actual object is passed, rather than a
reference to that object.
Assignment does not alias Fixnum objects. There is effectively only one
Fixnum object instance for any given integer value, so, for example, you
cannot add a singleton method to a Fixnum. Any attempt to add a singleton
method to a Fixnum object will raise a TypeError.
Instance Method Summary collapse
-
#%(y) ⇒ Object
Returns
fix
moduloother
. -
#&(integer) ⇒ Object
Bitwise AND.
-
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#**(numeric) ⇒ Object
Raises
fix
to the power ofnumeric
, which may be negative or fractional. -
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#-(numeric) ⇒ Object
Performs subtraction: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#- ⇒ Integer
Negates
fix
, which may return a Bignum. -
#/(numeric) ⇒ Object
Performs division: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#<(real) ⇒ Boolean
Returns
true
if the value offix
is less than that ofreal
. -
#<<(count) ⇒ Integer
Shifts
fix
leftcount
positions, or right ifcount
is negative. -
#<=(real) ⇒ Boolean
Returns
true
if the value offix
is less than or equal to that ofreal
. -
#<=>(numeric) ⇒ -1, ...
Comparison—Returns
-1
,0
,+1
ornil
depending on whetherfix
is less than, equal to, or greater thannumeric
. -
#==(other) ⇒ Boolean
Return
true
iffix
equalsother
numerically. -
#==(other) ⇒ Boolean
Return
true
iffix
equalsother
numerically. -
#>(real) ⇒ Boolean
Returns
true
if the value offix
is greater than that ofreal
. -
#>=(real) ⇒ Boolean
Returns
true
if the value offix
is greater than or equal to that ofreal
. -
#>>(count) ⇒ Integer
Shifts
fix
rightcount
positions, or left ifcount
is negative. -
#[](n) ⇒ 0, 1
Bit Reference—Returns the nth bit in the binary representation of
fix
, wherefix[0]
is the least significant bit. -
#^(integer) ⇒ Object
Bitwise EXCLUSIVE OR.
-
#abs ⇒ Object
Returns the absolute value of
fix
. -
#bit_length ⇒ Integer
Returns the number of bits of the value of int.
-
#div(numeric) ⇒ Integer
Performs integer division: returns integer result of dividing
fix
bynumeric
. -
#divmod(numeric) ⇒ Array
See Numeric#divmod.
-
#even? ⇒ Boolean
Returns
true
iffix
is an even number. -
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing
fix
bynumeric
. -
#magnitude ⇒ Object
Returns the absolute value of
fix
. -
#modulo(y) ⇒ Object
Returns
fix
moduloother
. -
#odd? ⇒ Boolean
Returns
true
iffix
is an odd number. -
#size ⇒ Fixnum
Returns the number of bytes in the machine representation of
fix
. -
#succ ⇒ Object
Returns the Integer equal to
int
+ 1. -
#to_f ⇒ Float
Converts
fix
to a Float. -
#to_s(base = 10) ⇒ String
(also: #inspect)
Returns a string containing the representation of
fix
radixbase
(between 2 and 36). -
#zero? ⇒ Boolean
Returns
true
iffix
is zero. -
#|(integer) ⇒ Object
Bitwise OR.
-
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
Methods inherited from Integer
#ceil, #chr, #denominator, #downto, #floor, #gcd, #gcdlcm, #integer?, #lcm, #next, #numerator, #ord, #pred, #rationalize, #round, #times, #to_i, #to_int, #to_r, #truncate, #upto
Methods inherited from Numeric
#+@, #abs2, #angle, #arg, #ceil, #coerce, #conj, #conjugate, #denominator, #eql?, #floor, #i, #imag, #imaginary, #initialize_copy, #integer?, #nonzero?, #numerator, #phase, #polar, #quo, #real, #real?, #rect, #rectangular, #remainder, #round, #singleton_method_added, #step, #to_c, #to_int, #truncate
Methods included from Comparable
Instance Method Details
#%(other) ⇒ Object #modulo(other) ⇒ Object
Returns fix
modulo other
.
See Numeric#divmod for more information.
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 |
# File 'numeric.c', line 3090
static VALUE
fix_mod(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long mod;
fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
return LONG2NUM(mod);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
x = rb_int2big(FIX2LONG(x));
return rb_big_modulo(x, y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
}
else {
return rb_num_coerce_bin(x, y, '%');
}
}
|
#&(integer) ⇒ Object
Bitwise AND.
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 |
# File 'numeric.c', line 3458
static VALUE
fix_and(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long val = FIX2LONG(x) & FIX2LONG(y);
return LONG2NUM(val);
}
if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_and(y, x);
}
bit_coerce(&x, &y);
return rb_funcall(x, rb_intern("&"), 1, y);
}
|
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a Bignum.
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 |
# File 'numeric.c', line 2919
static VALUE
fix_mul(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
#ifdef __HP_cc
/* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */
volatile
#endif
long a, b;
#if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
LONG_LONG d;
#else
VALUE r;
#endif
a = FIX2LONG(x);
b = FIX2LONG(y);
#if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
d = (LONG_LONG)a * b;
if (FIXABLE(d)) return LONG2FIX(d);
return rb_ll2inum(d);
#else
if (a == 0) return x;
if (MUL_OVERFLOW_FIXNUM_P(a, b))
r = rb_big_mul(rb_int2big(a), rb_int2big(b));
else
r = LONG2FIX(a * b);
return r;
#endif
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_mul(y, x);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '*');
}
}
|
#**(numeric) ⇒ Object
Raises fix
to the power of numeric
, which may be negative or fractional.
2 ** 3 #=> 8
2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623731
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 |
# File 'numeric.c', line 3200
static VALUE
fix_pow(VALUE x, VALUE y)
{
long a = FIX2LONG(x);
if (FIXNUM_P(y)) {
long b = FIX2LONG(y);
if (a == 1) return INT2FIX(1);
if (a == -1) {
if (b % 2 == 0)
return INT2FIX(1);
else
return INT2FIX(-1);
}
if (b < 0)
return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
if (a == 0) {
if (b > 0) return INT2FIX(0);
return DBL2NUM(INFINITY);
}
return int_pow(a, b);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
if (a == 1) return INT2FIX(1);
if (a == -1) {
if (int_even_p(y)) return INT2FIX(1);
else return INT2FIX(-1);
}
if (negative_int_p(y))
return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
if (a == 0) return INT2FIX(0);
x = rb_int2big(FIX2LONG(x));
return rb_big_pow(x, y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0);
if (a == 0) {
return DBL2NUM(RFLOAT_VALUE(y) < 0 ? INFINITY : 0.0);
}
if (a == 1) return DBL2NUM(1.0);
{
double dy = RFLOAT_VALUE(y);
if (a < 0 && dy != round(dy))
return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
return DBL2NUM(pow((double)a, dy));
}
}
else {
return rb_num_coerce_bin(x, y, rb_intern("**"));
}
}
|
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a Bignum.
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 |
# File 'numeric.c', line 2847
static VALUE
fix_plus(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long a, b, c;
VALUE r;
a = FIX2LONG(x);
b = FIX2LONG(y);
c = a + b;
r = LONG2NUM(c);
return r;
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_plus(y, x);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '+');
}
}
|
#-(numeric) ⇒ Object
Performs subtraction: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a Bignum.
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 |
# File 'numeric.c', line 2880
static VALUE
fix_minus(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long a, b, c;
VALUE r;
a = FIX2LONG(x);
b = FIX2LONG(y);
c = a - b;
r = LONG2NUM(c);
return r;
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
x = rb_int2big(FIX2LONG(x));
return rb_big_minus(x, y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '-');
}
}
|
#- ⇒ Integer
Negates fix
, which may return a Bignum.
2774 2775 2776 2777 2778 |
# File 'numeric.c', line 2774
static VALUE
fix_uminus(VALUE num)
{
return LONG2NUM(-FIX2LONG(num));
}
|
#/(numeric) ⇒ Object
Performs division: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a Bignum.
3060 3061 3062 3063 3064 |
# File 'numeric.c', line 3060
static VALUE
fix_div(VALUE x, VALUE y)
{
return fix_divide(x, y, '/');
}
|
#<(real) ⇒ Boolean
Returns true
if the value of fix
is less than that of real
.
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 |
# File 'numeric.c', line 3372
static VALUE
fix_lt(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue;
return Qfalse;
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) < 0 ? Qtrue : Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
}
else {
return rb_num_coerce_relop(x, y, '<');
}
}
|
#<<(count) ⇒ Integer
Shifts fix
left count
positions, or right if count
is negative.
3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 |
# File 'numeric.c', line 3530
static VALUE
rb_fix_lshift(VALUE x, VALUE y)
{
long val, width;
val = NUM2LONG(x);
if (!FIXNUM_P(y))
return rb_big_lshift(rb_int2big(val), y);
width = FIX2LONG(y);
if (width < 0)
return fix_rshift(val, (unsigned long)-width);
return fix_lshift(val, width);
}
|
#<=(real) ⇒ Boolean
Returns true
if the value of fix
is less than or equal to that of real
.
3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 |
# File 'numeric.c', line 3398
static VALUE
fix_le(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue;
return Qfalse;
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) <= 0 ? Qtrue : Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
VALUE rel = rb_integer_float_cmp(x, y);
return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
}
else {
return rb_num_coerce_relop(x, y, rb_intern("<="));
}
}
|
#<=>(numeric) ⇒ -1, ...
Comparison—Returns -1
, 0
, +1
or nil
depending on whether fix
is less than, equal to, or greater than numeric
.
This is the basis for the tests in the Comparable module.
nil
is returned if the two values are incomparable.
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 |
# File 'numeric.c', line 3294
static VALUE
fix_cmp(VALUE x, VALUE y)
{
if (x == y) return INT2FIX(0);
if (FIXNUM_P(y)) {
if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
return INT2FIX(-1);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_cmp(rb_int2big(FIX2LONG(x)), y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return rb_integer_float_cmp(x, y);
}
else {
return rb_num_coerce_cmp(x, y, id_cmp);
}
}
|
#==(other) ⇒ Boolean
Return true
if fix
equals other
numerically.
1 == 2 #=> false
1 == 1.0 #=> true
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 |
# File 'numeric.c', line 3266
static VALUE
fix_equal(VALUE x, VALUE y)
{
if (x == y) return Qtrue;
if (FIXNUM_P(y)) return Qfalse;
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_eq(y, x);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return rb_integer_float_eq(x, y);
}
else {
return num_equal(x, y);
}
}
|
#==(other) ⇒ Boolean
Return true
if fix
equals other
numerically.
1 == 2 #=> false
1 == 1.0 #=> true
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 |
# File 'numeric.c', line 3266
static VALUE
fix_equal(VALUE x, VALUE y)
{
if (x == y) return Qtrue;
if (FIXNUM_P(y)) return Qfalse;
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_eq(y, x);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return rb_integer_float_eq(x, y);
}
else {
return num_equal(x, y);
}
}
|
#>(real) ⇒ Boolean
Returns true
if the value of fix
is greater than that of real
.
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 |
# File 'numeric.c', line 3320
static VALUE
fix_gt(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue;
return Qfalse;
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) > 0 ? Qtrue : Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
}
else {
return rb_num_coerce_relop(x, y, '>');
}
}
|
#>=(real) ⇒ Boolean
Returns true
if the value of fix
is greater than or equal to that of real
.
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 |
# File 'numeric.c', line 3346
static VALUE
fix_ge(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue;
return Qfalse;
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) >= 0 ? Qtrue : Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
VALUE rel = rb_integer_float_cmp(x, y);
return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
}
else {
return rb_num_coerce_relop(x, y, rb_intern(">="));
}
}
|
#>>(count) ⇒ Integer
Shifts fix
right count
positions, or left if count
is negative.
3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 |
# File 'numeric.c', line 3562
static VALUE
rb_fix_rshift(VALUE x, VALUE y)
{
long i, val;
val = FIX2LONG(x);
if (!FIXNUM_P(y))
return rb_big_rshift(rb_int2big(val), y);
i = FIX2LONG(y);
if (i == 0) return x;
if (i < 0)
return fix_lshift(val, (unsigned long)-i);
return fix_rshift(val, i);
}
|
#[](n) ⇒ 0, 1
Bit Reference—Returns the nth bit in the binary representation of fix
, where fix[0]
is the least significant bit.
For example:
a = 0b11001100101010
30.downto(0) do |n| print a[n] end
#=> 0000000000000000011001100101010
3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 |
# File 'numeric.c', line 3602
static VALUE
fix_aref(VALUE fix, VALUE idx)
{
long val = FIX2LONG(fix);
long i;
idx = rb_to_int(idx);
if (!FIXNUM_P(idx)) {
idx = rb_big_norm(idx);
if (!FIXNUM_P(idx)) {
if (!BIGNUM_SIGN(idx) || val >= 0)
return INT2FIX(0);
return INT2FIX(1);
}
}
i = FIX2LONG(idx);
if (i < 0) return INT2FIX(0);
if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
if (val < 0) return INT2FIX(1);
return INT2FIX(0);
}
if (val & (1L<<i))
return INT2FIX(1);
return INT2FIX(0);
}
|
#^(integer) ⇒ Object
Bitwise EXCLUSIVE OR.
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 |
# File 'numeric.c', line 3504
static VALUE
fix_xor(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long val = FIX2LONG(x) ^ FIX2LONG(y);
return LONG2NUM(val);
}
if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_xor(y, x);
}
bit_coerce(&x, &y);
return rb_funcall(x, rb_intern("^"), 1, y);
}
|
#abs ⇒ Integer #magnitude ⇒ Integer
Returns the absolute value of fix
.
-12345.abs #=> 12345
12345.abs #=> 12345
3659 3660 3661 3662 3663 3664 3665 3666 3667 |
# File 'numeric.c', line 3659
static VALUE
fix_abs(VALUE fix)
{
long i = FIX2LONG(fix);
if (i < 0) i = -i;
return LONG2NUM(i);
}
|
#bit_length ⇒ Integer
Returns the number of bits of the value of int.
“the number of bits” means that the bit position of the highest bit which is different to the sign bit. (The bit position of the bit 2**n is n+1.) If there is no such bit (zero or minus one), zero is returned.
I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
(-2**12-1).bit_length #=> 13
(-2**12).bit_length #=> 12
(-2**12+1).bit_length #=> 12
-0x101.bit_length #=> 9
-0x100.bit_length #=> 8
-0xff.bit_length #=> 8
-2.bit_length #=> 1
-1.bit_length #=> 0
0.bit_length #=> 0
1.bit_length #=> 1
0xff.bit_length #=> 8
0x100.bit_length #=> 9
(2**12-1).bit_length #=> 12
(2**12).bit_length #=> 13
(2**12+1).bit_length #=> 13
This method can be used to detect overflow in Array#pack as follows.
if n.bit_length < 32
[n].pack("l") # no overflow
else
raise "overflow"
end
3726 3727 3728 3729 3730 3731 3732 3733 |
# File 'numeric.c', line 3726
static VALUE
rb_fix_bit_length(VALUE fix)
{
long v = FIX2LONG(fix);
if (v < 0)
v = ~v;
return LONG2FIX(bit_length(v));
}
|
#div(numeric) ⇒ Integer
Performs integer division: returns integer result of dividing fix
by numeric
.
3074 3075 3076 3077 3078 |
# File 'numeric.c', line 3074
static VALUE
fix_idiv(VALUE x, VALUE y)
{
return fix_divide(x, y, rb_intern("div"));
}
|
#divmod(numeric) ⇒ Array
See Numeric#divmod.
3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 |
# File 'numeric.c', line 3117
static VALUE
fix_divmod(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long div, mod;
fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);
return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
x = rb_int2big(FIX2LONG(x));
return rb_big_divmod(x, y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
{
double div, mod;
volatile VALUE a, b;
flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), &div, &mod);
a = dbl2ival(div);
b = DBL2NUM(mod);
return rb_assoc_new(a, b);
}
}
else {
return rb_num_coerce_bin(x, y, rb_intern("divmod"));
}
}
|
#even? ⇒ Boolean
Returns true
if fix
is an even number.
3951 3952 3953 3954 3955 3956 3957 3958 |
# File 'numeric.c', line 3951
static VALUE
fix_even_p(VALUE num)
{
if (num & 2) {
return Qfalse;
}
return Qtrue;
}
|
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing fix
by numeric
.
654321.fdiv(13731) #=> 47.6528293642124
654321.fdiv(13731.24) #=> 47.6519964693647
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 |
# File 'numeric.c', line 2999
static VALUE
fix_fdiv(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
return DBL2NUM((double)FIX2LONG(x) / (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_fdiv(rb_int2big(FIX2LONG(x)), y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM((double)FIX2LONG(x) / RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, rb_intern("fdiv"));
}
}
|
#abs ⇒ Integer #magnitude ⇒ Integer
Returns the absolute value of fix
.
-12345.abs #=> 12345
12345.abs #=> 12345
3659 3660 3661 3662 3663 3664 3665 3666 3667 |
# File 'numeric.c', line 3659
static VALUE
fix_abs(VALUE fix)
{
long i = FIX2LONG(fix);
if (i < 0) i = -i;
return LONG2NUM(i);
}
|
#%(other) ⇒ Object #modulo(other) ⇒ Object
Returns fix
modulo other
.
See Numeric#divmod for more information.
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 |
# File 'numeric.c', line 3090
static VALUE
fix_mod(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long mod;
fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
return LONG2NUM(mod);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
x = rb_int2big(FIX2LONG(x));
return rb_big_modulo(x, y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
}
else {
return rb_num_coerce_bin(x, y, '%');
}
}
|
#odd? ⇒ Boolean
Returns true
if fix
is an odd number.
3935 3936 3937 3938 3939 3940 3941 3942 |
# File 'numeric.c', line 3935
static VALUE
fix_odd_p(VALUE num)
{
if (num & 2) {
return Qtrue;
}
return Qfalse;
}
|
#size ⇒ Fixnum
Returns the number of bytes in the machine representation of fix
.
1.size #=> 4
-1.size #=> 4
2147483647.size #=> 4
3682 3683 3684 3685 3686 |
# File 'numeric.c', line 3682
static VALUE
fix_size(VALUE fix)
{
return INT2FIX(sizeof(long));
}
|
#next ⇒ Integer #succ ⇒ Integer
Returns the Integer equal to int
+ 1.
1.next #=> 2
(-1).next #=> 0
2592 2593 2594 2595 2596 2597 |
# File 'numeric.c', line 2592
static VALUE
fix_succ(VALUE num)
{
long i = FIX2LONG(num) + 1;
return LONG2NUM(i);
}
|
#to_f ⇒ Float
Converts fix
to a Float.
3637 3638 3639 3640 3641 3642 3643 3644 3645 |
# File 'numeric.c', line 3637
static VALUE
fix_to_f(VALUE num)
{
double val;
val = (double)FIX2LONG(num);
return DBL2NUM(val);
}
|
#to_s(base = 10) ⇒ String Also known as: inspect
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"
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 |
# File 'numeric.c', line 2823
static VALUE
fix_to_s(int argc, VALUE *argv, VALUE x)
{
int base;
if (argc == 0) base = 10;
else {
VALUE b;
rb_scan_args(argc, argv, "01", &b);
base = NUM2INT(b);
}
return rb_fix2str(x, base);
}
|
#zero? ⇒ Boolean
Returns true
if fix
is zero.
3919 3920 3921 3922 3923 3924 3925 3926 |
# File 'numeric.c', line 3919
static VALUE
fix_zero_p(VALUE num)
{
if (FIX2LONG(num) == 0) {
return Qtrue;
}
return Qfalse;
}
|
#|(integer) ⇒ Object
Bitwise OR.
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 |
# File 'numeric.c', line 3481
static VALUE
fix_or(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long val = FIX2LONG(x) | FIX2LONG(y);
return LONG2NUM(val);
}
if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_big_or(y, x);
}
bit_coerce(&x, &y);
return rb_funcall(x, rb_intern("|"), 1, y);
}
|
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
3424 3425 3426 3427 3428 |
# File 'numeric.c', line 3424
static VALUE
fix_rev(VALUE num)
{
return ~num | FIXNUM_FLAG;
}
|