struct

BigRational

Inherits Comparable / Comparable / Comparable / Comparable / Number / Comparable / Steppable / Comparable / Value / Object

Rational numbers are represented as the quotient of arbitrarily large numerators and denominators. Rationals are canonicalized such that the denominator and the numerator have no common factors, and that the denominator is positive. Zero has the unique representation 0/1.

NOTE: To use BigRational, you must explicitly import it with require "big"

require "big"

r = BigRational.new(7.to_big_i, 3.to_big_i)
r.to_s # => "7/3"

r = BigRational.new(3, -9)
r.to_s # => "-1/3"

It is implemented under the hood with GMP.

Constructors

new(numerator : Int, denominator : Int)

Creates a new BigRational.

If denominator is 0, this will raise an exception.

Source
new(num : Int)

Creates a new BigRational with num as the numerator and 1 for denominator.

Source
new(num : Float::Primitive) : self

Creates an exact representation of float as rational.

Raises ArgumentError if num is not finite.

Source
new(num : BigFloat) : self

Creates an exact representation of float as rational.

Source
new(num : BigRational) : self

Creates a BigRational from the given num.

Source
new(num : BigDecimal) : self

Creates a BigRational from the given num.

Source

Instance methods

%(other : Int) : BigRational
Source
*(other : Int) : BigRational
Source
**(other : Int) : BigRational

Raises the rational to the otherth power

This will raise DivisionByZeroError if rational is 0 and other is negative.

require "big"

BigRational.new(2, 3) ** 2  # => 4/9
BigRational.new(2, 3) ** -1 # => 3/2
Source
+(other : Int) : BigRational
Source
-(other : Int) : BigRational
Source
/(other : Int8) : BigRational
Source
//(other : Int) : BigRational
Source
<<(other : Int) : BigRational

Multiplies the rational by (2 ** other)

require "big"

BigRational.new(2, 3) << 2 # => 8/3
Source
<=>(other : BigRational) : Int32
Source
<=>(other : Float::Primitive) : Int32 | Nil
Source
<=>(other : BigFloat) : Int32
Source
<=>(other : BigInt) : Int32
Source
<=>(other : Int) : Int32
Source
<=>(other : BigDecimal)

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Source
==(other : BigRational) : Bool
Source
>>(other : Int) : BigRational

Divides the rational by (2 ** other)

require "big"

BigRational.new(2, 3) >> 2 # => 1/6
Source
abs

Returns the absolute value of this number.

123.abs  # => 123
-123.abs # => 123
Source
ceil
Source
clone
Source
denominator
Source
floor
Source
format(io : IO, separator = '.', delimiter = ',', decimal_places : Int | Nil = nil, *, group : Int = 3, only_significant : Bool = false) : Nil

Prints this number as a String using a customizable format.

separator is used as decimal separator, delimiter as thousands delimiter between batches of group digits.

If decimal_places is nil, all significant decimal places are printed (similar to #to_s). If the argument has a numeric value, the number of visible decimal places will be fixed to that amount.

Trailing zeros are omitted if only_significant is true.

123_456.789.format                                            # => "123,456.789"
123_456.789.format(',', '.')                                  # => "123.456,789"
123_456.789.format(decimal_places: 2)                         # => "123,456.79"
123_456.789.format(decimal_places: 6)                         # => "123,456.789000"
123_456.789.format(decimal_places: 6, only_significant: true) # => "123,456.789"
Source
inspect(io : IO) : Nil

Prints to io an unambiguous and information-rich string representation of this object, typically intended for developers.

It is similar to #to_s(IO), but often provides more information. Ideally, it should contain sufficient information to be able to recreate an object with the same value (given an identical environment).

For types that don't provide a custom implementation of this method, default implementation delegates to #to_s(IO). This said, it is advisable to have an appropriate #inspect implementation on every type. Default implementations are provided by Struct#inspect and Reference#inspect.

::p and ::p! use this method to print an object in STDOUT.

Source
inspect

Returns an unambiguous and information-rich string representation of this object, typically intended for developers.

This method should usually not be overridden. It delegates to #inspect(IO) which can be overridden for custom implementations.

Also see #to_s.

Source
integer?

Returns true if self is an integer.

Non-integer types may return true as long as self denotes a finite value without any fractional parts.

1.integer?       # => true
1.0.integer?     # => true
1.2.integer?     # => false
(1 / 0).integer? # => false
(0 / 0).integer? # => false
Source
inv

Returns a new BigRational as 1/r.

This will raise an exception if rational is 0.

Source
numerator
Source
remainder(other : BigRational) : BigRational
Source
remainder(other : Int) : BigRational
Source
round_away
Source
round_even
Source
tdiv(other : Int) : BigRational
Source
to_big_d

Converts self to BigDecimal.

Source
to_big_f
Source
to_big_i
Source
to_big_r

Returns self.

require "big"

BigRational.new(4, 5).to_big_r # => 4/5
Source
to_f

Returns the Float64 representing this rational.

Source
to_f!
Source
to_f32
Source
to_f32!
Source
to_f64
Source
to_f64!
Source
to_i
Source
to_i16(*args, **options)
Source
to_i16(*args, **options, &)
Source
to_i32(*args, **options)
Source
to_i32(*args, **options, &)
Source
to_i64(*args, **options)
Source
to_i64(*args, **options, &)
Source
to_i8(*args, **options)
Source
to_i8(*args, **options, &)
Source
to_s(base : Int = 10) : String

Returns the string representing this rational.

Optionally takes a radix base (2 through 36).

require "big"

r = BigRational.new(8243243, 562828882)
r.to_s     # => "8243243/562828882"
r.to_s(16) # => "7dc82b/218c1652"
r.to_s(36) # => "4woiz/9b3djm"
Source
to_s(io : IO, base : Int = 10) : Nil

Prints a nicely readable and concise string representation of this object, typically intended for users, to io.

This method is called when an object is interpolated in a string literal:

"foo #{bar} baz" # calls bar.to_io with the builder for this string

IO#<< calls this method to append an object to itself:

io << bar # calls bar.to_s(io)

Thus implementations must not interpolate self in a string literal or call io << self which both would lead to an endless loop.

Also see #inspect(IO).

Source
to_u16(*args, **options)
Source
to_u16(*args, **options, &)
Source
to_u32(*args, **options)
Source
to_u32(*args, **options, &)
Source
to_u64(*args, **options)
Source
to_u64(*args, **options, &)
Source
to_u8(*args, **options)
Source
to_u8(*args, **options, &)
Source
to_unsafe
Source
trunc
Source