# abstract struct Float

- Float
- Number
- Value
- Object

##
Overview

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

##
Included Modules

- Comparable(BigDecimal)
- Comparable(BigRational)
- Comparable(BigInt)

##
Direct Known Subclasses

- BigFloat
- Float32
- Float64

##
Defined in:

##
Constructors

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

  Reads a float from the given io in the given format.

##
Instance Method Summary

- #%(other)
- #-
- #//(other)

  Divides `self` by other using floored division.

- #<=>(other : BigDecimal)

  The comparison operator.

- #<=>(other : BigRational)
- #<=>(other : BigInt)
- #days : Time::Span

  Returns a `Time::Span` of `self` days.

- #fdiv(other) : self

  Float divivision that will obey the left hand side argument type.

- #finite?
- #hash(hasher)

  See `Object#hash(hasher)`

- #hours : Time::Span

  Returns a `Time::Span` of `self` hours.

- #infinite?
- #microseconds : Time::Span

  Returns a `Time::Span` of `self` microseconds.

- #milliseconds : Time::Span

  Returns a `Time::Span` of `self` milliseconds.

- #minutes : Time::Span

  Returns a `Time::Span` of `self` minutes.

- #modulo(other)
- #nan?
- #nanoseconds : Time::Span

  Returns a `Time::Span` of `self` nanoseconds.

- #remainder(other)
- #seconds : Time::Span

  Returns a `Time::Span` of `self` seconds.

- #to_big_d

  Converts `self` to `BigDecimal`.

- #to_big_i : BigInt

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

- #to_big_r

  Returns a `BigRational` representing this float.

- #to_io(io : IO, format : IO::ByteFormat)

  Writes this float to the given io in the given format.

- #to_json(json : JSON::Builder)
- #to_json_object_key
- #weeks : Time::Span

  Returns a `Time::Span` of `self` weeks.

### 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(BigRational)`

<(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(BigInt)`

<(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.from_io(io : IO, format : IO::ByteFormat) : self #

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

##
Instance Method Detail

def %(other) #
def - #
def //(other) #
Description copied from struct Number

Divides `self` by other using floored division.

The result will be of the same type as `self`.

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 : BigRational) #
def <=>(other : BigInt) #
def days : Time::Span #

Returns a `Time::Span` of `self` days.

def fdiv(other) : self #

Float divivision that will obey the left hand side argument type.

def finite? #
def hash(hasher) #

See `Object#hash(hasher)`

def hours : Time::Span #

Returns a `Time::Span` of `self` hours.

def infinite? #
def microseconds : Time::Span #

Returns a `Time::Span` of `self` microseconds.

def milliseconds : Time::Span #

Returns a `Time::Span` of `self` milliseconds.

def minutes : Time::Span #

Returns a `Time::Span` of `self` minutes.

def modulo(other) #
def nan? #
def nanoseconds : Time::Span #

Returns a `Time::Span` of `self` nanoseconds.

def remainder(other) #
def seconds : Time::Span #

Returns a `Time::Span` of `self` seconds.

def 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

def to_big_i : BigInt #

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

    require "big"

    1212341515125412412412421.0.to_big_i

def to_big_r #

Returns a `BigRational` representing this float.

    require "big"

    123.0.to_big_r

def to_io(io : IO, format : IO::ByteFormat) #

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

def to_json(json : JSON::Builder) #
def to_json_object_key #
def weeks : Time::Span #

Returns a `Time::Span` of `self` weeks.