struct BigRational
Overview
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.
r = BigRational.new(BigInt.new(7),BigInt.new(3))
r.to_s # => "7/3"
r = BigRational.new(3,-9)
r.to_s # => "-1/3"
It is implemented under the hood with GMP.
Included Modules
Defined in:
big/big_rational.crClass Method Summary
-
.new(num : Int)
Creates a new BigRational with num as the numerator and 1 for denominator.
-
.new(numerator : Int, denominator : Int)
Create a new BigRational.
Instance Method Summary
- #*(other : BigRational)
- #*(other : Int)
- #+(other : BigRational)
- #+(other : Int)
- #-(other : BigRational)
- #-
- #-(other : Int)
- #/(other : Int)
- #/(other : BigRational)
-
#<<(other : Int)
Multiplies the rational by (2**
other
) - #<=>(other : Int)
- #<=>(other : BigRational)
- #<=>(other : Float)
-
#>>(other : Int)
Divides the rational by (2**
other
) - #abs
- #clone
- #denominator
- #hash
- #inspect
- #inspect(io)
-
#inv
Returns a new BigRational as 1/r.
- #numerator
-
#to_f
Returns the
Float64
representing this rational. - #to_f32
- #to_f64
-
#to_s(base = 10)
Returns the string representing this rational.
- #to_s(io : IO, base = 10)
- #to_unsafe
Instance methods inherited from module Comparable({"T", T})
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from module Comparable({"T", T})
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from module Comparable({"T", T})
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from struct Number
*(other : Complex)*(other : BigFloat) *, +
+(other : BigFloat)
+(other : Complex) +, -(other : Complex)
-(other : BigFloat) -, /(other : Complex) /, <=>(other : BigFloat)
<=>(other) <=>, ==(other : Complex) ==, abs abs, abs2 abs2, cis cis, clamp(range : Range)
clamp(min, max) clamp, divmod(number) divmod, i i, round(digits, base = 10) round, sign sign, significant(digits, base = 10) significant, step(limit = nil, by = 1)
step(limit = nil, by = 1, &block) step, to_big_f to_big_f, to_c to_c, to_yaml(emitter : YAML::Emitter) to_yaml
Class methods inherited from struct Number
zero : self
zero
Instance methods inherited from module Comparable({"T", T})
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from module Comparable({"T", T})
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from struct Value
==(other)
==,
dup
dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml
to_yaml(io : IO) to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
clone
clone,
dup
dup,
from_json(string_or_io) : selffrom_json(string_or_io, root : String) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |
Class Method Detail
Creates a new BigRational with num as the numerator and 1 for denominator.
Create a new BigRational.
If #denominator
is 0, this will raise an exception.
Instance Method Detail
Multiplies the rational by (2**other
)
BigRational.new(2,3) << 2 # => 8/3
Divides the rational by (2**other
)
BigRational.new(2,3) >> 2 # => 1/6
Returns the string representing this rational.
Optionally takes a radix base (2 through 36).
r = BigRational.new(8243243,562828882)
r.to_s # => "8243243/562828882"
r.to_s(16) # => "7dc82b/218c1652"
r.to_s(36) # => "4woiz/9b3djm"