# struct BigRational

- BigRational
- Number
- Value
- Object

##
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

- Comparable(BigDecimal)
- Comparable(Float)
- Comparable(Int)
- Comparable(BigRational)

##
Defined in:

##
Constructors

- .new(numerator : Int, denominator : Int)

  Creates a new `BigRational`.

- .new(num : Int)

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

- .new(num : Float)

  Creates a exact representation of float as rational.

- .new(num : BigRational)

  Creates a `BigRational` from the given num.

- .new(num : BigDecimal)

  Creates a `BigRational` from the given num.

##
Instance Method Summary

- #*(other : Int)
- #*(other : BigRational)
- #**(other : Int) : BigRational

  Raises the rational to the otherth power

- #+(other : Int)
- #+(other : BigRational)
- #-
- #-(other : Int)
- #-(other : BigRational)
- #/(other : Int64) : BigRational
- #/(other : UInt64) : BigRational
- #/(other : Int128) : BigRational
- #/(other : UInt128) : BigRational
- #/(other : Float32) : BigRational
- #/(other : Float64) : BigRational
- #/(other : UInt32) : BigRational
- #/(other : Int32) : BigRational
- #/(other : UInt16) : BigRational
- #/(other : Int16) : BigRational
- #/(other : UInt8) : BigRational
- #/(other : Int8) : BigRational
- #/(other : BigDecimal) : BigRational
- #/(other : BigFloat) : BigRational
- #/(other : BigInt) : BigRational
- #/(other : BigRational)
- #<<(other : Int)

  Multiplies the rational by (2 ** other)

- #<=>(other : BigDecimal)

  The comparison operator.

- #<=>(other : Int)
- #<=>(other : Float)
- #<=>(other : Float32 | Float64)
- #<=>(other : BigRational)
- #>>(other : Int)

  Divides the rational by (2 ** other)

- #abs

  Returns the absolute value of this number.

- #ceil
- #clone
- #denominator
- #floor
- #hash(hasher)

  TODO improve this

- #inspect : String

  Returns a `String` representation of this object suitable to be embedded inside other expressions, sometimes providing more information about this object.

- #inspect(io : IO) : Nil

  Appends a string representation of this object to the given `IO` object.

- #inv

  Returns a new `BigRational` as 1/r.

- #numerator
- #to_big_d

  Converts `self` to `BigDecimal`.

- #to_big_f
- #to_big_i
- #to_f

  Returns the `Float64` representing this rational.

- #to_f!
- #to_f32
- #to_f32!
- #to_f64
- #to_f64!
- #to_i16(*args, **options)
- #to_i16(*args, **options, &)
- #to_i32(*args, **options)
- #to_i32(*args, **options, &)
- #to_i64(*args, **options)
- #to_i64(*args, **options, &)
- #to_i8(*args, **options, &)
- #to_i8(*args, **options)
- #to_s(io : IO, base : Int = 10) : Nil

  Appends a `String` representation of this object to the given `IO` object.

- #to_s(base : Int = 10) : String

  Returns the string representing this rational.

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

### Instance methods inherited from module `Comparable(BigDecimal)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### Instance methods inherited from module `Comparable(Float)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### Instance methods inherited from module `Comparable(Int)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### Instance methods inherited from module `Comparable(BigRational)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### Instance methods inherited from struct `Number`

*(other : Complex)
*(other : BigFloat) *, +(other : BigFloat)
+(other : Complex)
+ +, -(other : Complex)
-(other : BigFloat) -, /(other : Complex)
/(other : BigFloat) /, //(other) //, <=>(other : BigFloat)
<=>(other) : Int32? <=>, ==(other : Complex) ==, abs abs, abs2 abs2, cis cis, divmod(number) divmod, format(separator = '.', delimiter = ',', decimal_places : Int? = nil, *, group : Int = 3, only_significant : Bool = false) : String
format(io : IO, separator = '.', delimiter = ',', decimal_places : Int? = nil, *, group : Int = 3, only_significant : Bool = false) : Nil format, humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes : Indexable = SI_PREFIXES) : Nil
humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes : Proc) : Nil
humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, &) : String
humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, &prefixes : Int32, Float64 -> Tuple(Int32, _) | Tuple(Int32, _, Bool)) : Nil
humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes = SI_PREFIXES) : String
humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes : Proc) : Nil humanize, 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, &) 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

### Class methods inherited from struct `Number`

si_prefix(magnitude : Int, prefixes = SI_PREFIXES) : Char? si_prefix

### Instance methods inherited from module `Comparable(BigFloat)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### Instance methods inherited from module `Comparable(Number)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### 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) =~, __crystal_pseudo_! : Bool __crystal_pseudo_!, __crystal_pseudo_as(type : Class) __crystal_pseudo_as, __crystal_pseudo_as?(type : Class) __crystal_pseudo_as?, __crystal_pseudo_is_a?(type : Class) : Bool __crystal_pseudo_is_a?, __crystal_pseudo_nil? : Bool __crystal_pseudo_nil?, __crystal_pseudo_responds_to?(name : Symbol) : Bool __crystal_pseudo_responds_to?, class class, dup dup, hash(hasher)
hash hash, inspect : String
inspect(io : IO) : Nil 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(&) 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 : String
to_s(io : IO) : Nil to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

### Class methods inherited from class `Object`

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

##
Constructor Detail

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

Creates a new `BigRational`.

If denominator is 0, this will raise an exception.

def self.new(num : Int) #

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

def self.new(num : Float) #

Creates a exact representation of float as rational.

def self.new(num : BigRational) #

Creates a `BigRational` from the given num.

def self.new(num : BigDecimal) #

Creates a `BigRational` from the given num.

##
Instance Method Detail

def *(other : Int) #
def *(other : BigRational) #
def **(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

def +(other : Int) #
def +(other : BigRational) #
def - #
def -(other : Int) #
def -(other : BigRational) #
def /(other : Int64) : BigRational #
def /(other : UInt64) : BigRational #
def /(other : Int128) : BigRational #
def /(other : UInt128) : BigRational #
def /(other : Float32) : BigRational #
def /(other : Float64) : BigRational #
def /(other : UInt32) : BigRational #
def /(other : Int32) : BigRational #
def /(other : UInt16) : BigRational #
def /(other : Int16) : BigRational #
def /(other : UInt8) : BigRational #
def /(other : Int8) : BigRational #
def /(other : BigDecimal) : BigRational #
def /(other : BigFloat) : BigRational #
def /(other : BigInt) : BigRational #
def /(other : BigRational) #
def <<(other : Int) #

Multiplies the rational by (2 ** other)

    require "big"

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

def <=>(other : BigDecimal) #
Description copied from module Comparable(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 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]

def <=>(other : Int) #
def <=>(other : Float) #
def <=>(other : Float32 | Float64) #
def <=>(other : BigRational) #
def >>(other : Int) #

Divides the rational by (2 ** other)

    require "big"

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

def abs #
Description copied from struct Number

Returns the absolute value of this number.

    123.abs  # => 123
    -123.abs # => 123

def ceil #
def clone #
def denominator #
def floor #
def hash(hasher) #

TODO improve this

def inspect : String #
Description copied from class Object

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.

def inspect(io : IO) : Nil #
Description copied from class Object

Appends a string representation of this object to the given `IO` object.

Similar to `#to_s(io)`, but usually appends more information about this object. See `#inspect`.

def inv #

Returns a new `BigRational` as 1/r.

This will raise an exception if rational is 0.

def numerator #
def to_big_d #

Converts `self` to `BigDecimal`.

def to_big_f #
def to_big_i #
def to_f #

Returns the `Float64` representing this rational.

def to_f! #
def to_f32 #
def to_f32! #
def to_f64 #
def to_f64! #
def to_i16(*args, **options) #
def to_i16(*args, **options, &) #
def to_i32(*args, **options) #
def to_i32(*args, **options, &) #
def to_i64(*args, **options) #
def to_i64(*args, **options, &) #
def to_i8(*args, **options, &) #
def to_i8(*args, **options) #
def to_s(io : IO, base : Int = 10) : Nil #
Description copied from class Object

Appends a `String` representation of this object to the given `IO` object.

An object must never append itself to the io argument, as this will in turn call `#to_s(io)` on it.

def 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"

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