Class

When mathn is required, Fixnum’s division is enhanced to return more precise values from mathematical expressions.

2/3*3  # => 0
require 'mathn'
2/3*3  # => 2

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 Methods

Returns fix modulo other.

See Numeric#divmod for more information.

Bitwise AND.

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.

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

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.

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.

Negates fix, which may return a Bignum.

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.

Returns true if the value of fix is less than that of real.

Shifts fix left count positions, or right if count is negative.

Returns true if the value of fix is less than or equal to that of real.

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.

Return true if fix equals other numerically.

1 == 2      #=> false
1 == 1.0    #=> true
An alias for ==

Returns true if the value of fix is greater than that of real.

Returns true if the value of fix is greater than or equal to that of real.

Shifts fix right count positions, or left if count is negative.

Bit Reference—Returns the +n+th 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

Bitwise EXCLUSIVE OR.

Returns the absolute value of fix.

-12345.abs   #=> 12345
12345.abs    #=> 12345

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

provides a unified clone operation, for REXML::XPathParser to use across multiple Object types

Performs integer division: returns integer result of dividing fix by numeric.

Returns true if fix is an even number.

Returns the floating point result of dividing fix by numeric.

654321.fdiv(13731)      #=> 47.6528293642124
654321.fdiv(13731.24)   #=> 47.6519964693647

Returns true if fix is an odd number.

Returns the number of bytes in the machine representation of fix.

1.size            #=> 4
-1.size           #=> 4
2147483647.size   #=> 4

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0

Converts fix to a Float.

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 true if fix is zero.

Bitwise OR.

One’s complement: returns a number where each bit is flipped.