struct

Float

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

Float is the base type of all floating point numbers.

There are two floating point types, Float32 and Float64, which correspond to the binary32 and binary64 types defined by IEEE.

A floating point literal is an optional + or - sign, followed by a sequence of numbers or underscores, followed by a dot, followed by numbers or underscores, followed by an optional exponent suffix, followed by an optional type suffix. If no suffix is present, the literal's type is Float64.

1.0     # Float64
1.0_f32 # Float32
1_f32   # Float32

1e10   # Float64
1.5e10 # Float64
1.5e-7 # Float64

+1.3 # Float64
-0.5 # Float64

The underscore _ before the suffix is optional.

Underscores can be used to make some numbers more readable:

1_000_000.111_111 # better than 1000000.111111

See Float literals in the language reference.

Constructors

from_io(io : IO, format : IO::ByteFormat) : self

Reads a float from the given io in the given format. See also: IO#read_bytes.

Source

Instance methods

%(other)
Source
-

Negates this value's sign.

Source
//(other)

Divides self by other using floored division.

The result will be of the same type as self.

Source
<=>(other : BigInt)
Source
<=>(other : BigFloat)
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
days

Returns a Time::Span of self days.

Source
fdiv(other : BigInt | BigFloat | BigDecimal | BigRational) : self
Source
finite?

Returns whether this value is finite, i.e. it is neither infinite nor a not-a-number.

Source
hours

Returns a Time::Span of self hours.

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
microseconds

Returns a Time::Span of self microseconds.

Source
milliseconds

Returns a Time::Span of self milliseconds.

Source
minutes

Returns a Time::Span of self minutes.

Source
modulo(other)
Source
nan?

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

This includes both quiet and signalling NaNs from IEEE 754.

Source
nanoseconds

Returns a Time::Span of self nanoseconds.

Source
remainder(other)
Source
seconds

Returns a Time::Span of self seconds.

Source
to_big_d

Converts self to BigDecimal.

NOTE: Floats are fundamentally less precise than BigDecimals, which makes conversion to them risky.

require "big"
1212341515125412412412421.0.to_big_d
Source
to_big_i

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

require "big"

1212341515125412412412421.0.to_big_i
Source
to_big_r

Returns a BigRational representing this float.

require "big"

123.0.to_big_r
Source
to_io(io : IO, format : IO::ByteFormat) : Nil

Writes this float to the given io in the given format. See also: IO#write_bytes.

Source
to_json(json : JSON::Builder) : Nil
Source
to_json_object_key
Source
to_yaml(yaml : YAML::Nodes::Builder) : Nil
Source
weeks

Returns a Time::Span of self weeks.

Source

Nested types