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.
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.
Creates a new BigRational
.
Creates a new BigRational
with num as the numerator and 1 for denominator.
Creates a exact representation of float as rational.
Creates a BigRational
from the given num.
Creates a BigRational
from the given num.
Raises the rational to the otherth power
Multiplies the rational by (2 ** other)
The comparison operator.
Divides the rational by (2 ** other)
Returns the absolute value of this number.
TODO improve this
Returns a String
representation of this object suitable to be embedded inside other expressions, sometimes providing more information about this object.
Appends a string representation of this object to the given IO
object.
Returns a new BigRational
as 1/r.
Converts self
to BigDecimal
.
Returns the Float64
representing this rational.
Returns the string representing this rational.
Comparable(BigDecimal)
Comparable(Float)
Comparable(Int)
Comparable(BigRational)
Number
Number
Number
Comparable(BigFloat)
Comparable(Number)
Value
Object
Object
Creates a new BigRational
.
If denominator is 0, this will raise an exception.
Creates a new BigRational
with num as the numerator and 1 for denominator.
Creates a BigRational
from the given num.
Creates a BigRational
from the given num.
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
Multiplies the rational by (2 ** other)
require "big" BigRational.new(2, 3) << 2 # => 8/3
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 greter 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]
Divides the rational by (2 ** other)
require "big" BigRational.new(2, 3) >> 2 # => 1/6
Returns the absolute value of this number.
123.abs # => 123 -123.abs # => 123
TODO improve this
Returns a String
representation of this object suitable to be embedded inside other expressions, sometimes providing more information about this object.
#inspect
(and #inspect(io)
) are the methods used when you invoke #to_s
or #inspect
on an object that holds other objects and wants to show them. For example when you invoke Array#to_s
, #inspect
will be invoked on each element:
ary = ["one", "two", "three, etc."] ary.inspect # => ["one", "two", "three, etc."]
Note that if Array invoked #to_s
on each of the elements above, the output would have been this:
ary = ["one", "two", "three, etc."] # If inspect invoked to_s on each element... ary.inspect # => [one, two, three, etc.]
Note that it's not clear how many elements the array has, or which are they, because #to_s
doesn't guarantee that the string representation is clearly delimited (in the case of String
the quotes are not shown).
Also note that sometimes the output of #inspect
will look like a Crystal expression that will compile, but this isn't always the case, nor is it necessary. Notably, Reference#inspect
and Struct#inspect
return values that don't compile.
Classes must usually not override this method. Instead, they must override #inspect(io)
, which must append to the given IO
object.
Returns a new BigRational
as 1/r.
This will raise an exception if rational is 0.
Converts self
to BigDecimal
.
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"
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/BigRational.html