struct

BigFloat

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

A BigFloat can represent arbitrarily large floats.

It is implemented under the hood with GMP.

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

Constructors

new(num : Float, precision : Int)
Source
new(str : String)
Source
new(num : BigInt)
Source
new(num : BigFloat)
Source
new(num : Int)
Source
new(num : Number)
Source
new(mpf : LibGMP::MPF)
Source
new(pull : JSON::PullParser) : self
Source

Class methods

default_precision
Source
default_precision=(prec : Int) : Nil
Source
from_json_object_key?(key : String) : BigFloat | Nil
Source

Instance methods

*(other : Number) : BigFloat
Source
**(other : BigInt) : BigFloat
Source
**(other : Int) : BigFloat
Source
+(other : Number) : BigFloat
Source
-(other : Number) : BigFloat
Source
-

Negates this value's sign.

Source
/(other : BigInt) : BigFloat
Source
/(other : Float32) : BigFloat
Source
/(other : Float64) : BigFloat
Source
<=>(other : BigFloat)

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 : BigInt)
Source
<=>(other : Float::Primitive) : Int32 | Nil
Source
<=>(other : Int) : Int32
Source
<=>(other : BigRational)
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 : Number)
Source
abs

Returns the absolute value of this number.

123.abs  # => 123
-123.abs # => 123
Source
ceil

Rounds towards positive infinity.

Source
clone
Source
fdiv(other : Number::Primitive) : self
Source
floor

Rounds towards negative infinity.

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
infinite?

Checks whether this value is infinite. Returns 1 if this value is positive infinity, -1 if this value is negative infinity, or nil otherwise.

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
nan?

Returns whether this value is a not-a-number.

This includes both quiet and signalling NaNs from IEEE 754.

Source
round_away

Rounds towards the nearest integer. If both neighboring integers are equidistant, rounds away from zero.

Source
round_even

Rounds towards the nearest integer. If both neighboring integers are equidistant, rounds towards the even neighbor (Banker's rounding).

Source
to_big_f
Source
to_big_i

Returns a BigInt representing this float (rounded using floor).

require "big"

1212341515125412412412421.0.to_big_i
Source
to_f
Source
to_f!
Source
to_f32
Source
to_f32!
Source
to_f64
Source
to_f64!
Source
to_i
Source
to_i!
Source
to_i16
Source
to_i16!
Source
to_i32
Source
to_i32!
Source
to_i64
Source
to_i64!
Source
to_i8
Source
to_i8!
Source
to_json(json : JSON::Builder) : Nil
Source
to_json_object_key
Source
to_s(io : IO) : 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_u
Source
to_u!
Source
to_u16
Source
to_u16!
Source
to_u32
Source
to_u32!
Source
to_u64
Source
to_u64!
Source
to_u8
Source
to_u8!
Source
to_unsafe
Source
trunc

Rounds towards zero.

Source