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.

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.

Included Modules

Defined in:

big.cr
big/big_rational.cr
big/big_decimal.cr

Constant Summary

MANTISSA_BITS = 53
MANTISSA_SHIFT = (1_i64 << MANTISSA_BITS).to_f64

Constructors

Instance Method Summary

Instance methods inherited from module Comparable(BigDecimal)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from module Comparable(Float)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from module Comparable(Int)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from module Comparable(BigRational)

<(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 : BigFloat)
<=>(other)
<=>
, ==(other : Complex) ==, abs abs, abs2 abs2, cis cis, clamp(min, max)
clamp(range : Range)
clamp
, divmod(number) divmod, i i, round(digits = 0, base = 10) round, sign sign, significant(digits, base = 10) significant, step(*, to = nil, by = 1)
step(*, to = nil, by = 1, &block)
step
, to_big_f to_big_f, to_c to_c, to_yaml(yaml : YAML::Nodes::Builder) to_yaml, zero? : Bool zero?

Constructor methods inherited from struct Number

zero : self zero

Instance methods inherited from module Comparable(BigFloat)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from module Comparable(Number)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

Instance methods inherited from struct Value

==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, class class, dup dup, hash(hasher)
hash
hash
, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
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(io : IO)
to_yaml
to_yaml
, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

Constructor methods inherited from class Object

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io : String | IO) : self from_yaml

Constructor Detail

def self.new(numerator : Int, denominator : Int) #

Creates a new BigRational.

If denominator is 0, this will raise an exception.


[View source]
def self.new(num : Int) #

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


[View source]
def self.new(num : Float) #

Creates a exact representation of float as rational.


[View source]

Instance Method Detail

def *(other : BigRational) #

[View source]
def *(other : Int) #

[View source]
def +(other : BigRational) #

[View source]
def +(other : Int) #

[View source]
def - #

[View source]
def -(other : BigRational) #

[View source]
def -(other : Int) #

[View source]
def /(other : Int) #

[View source]
def /(other : BigRational) #

[View source]
def <<(other : Int) #

Multiplies the rational by (2 ** other)

BigRational.new(2, 3) << 2 # => 8/3

[View source]
def <=>(other : BigRational) #

[View source]
def <=>(other : Float32 | Float64) #

[View source]
def <=>(other : Float) #

[View source]
def <=>(other : Int) #

[View source]
def <=>(other : BigDecimal) #

[View source]
def >>(other : Int) #

Divides the rational by (2 ** other)

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

[View source]
def abs #

[View source]
def clone #

[View source]
def denominator #

[View source]
def hash(hasher) #

TODO improve this


[View source]
def inspect #

[View source]
def inspect(io) #

[View source]
def inv #

Returns a new BigRational as 1/r.

This will raise an exception if rational is 0.


[View source]
def numerator #

[View source]
def to_big_d #

Converts self to BigDecimal.


[View source]
def to_big_f #

[View source]
def to_f #

Returns the Float64 representing this rational.


[View source]
def to_f32 #

[View source]
def to_f64 #

[View source]
def to_s(io : IO, base = 10) #

[View source]
def to_s(base = 10) #

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"

[View source]
def to_unsafe #

[View source]