abstract struct Float
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
Direct Known Subclasses
Defined in:
float.crtime/span.cr
big/big_int.cr
big/big_rational.cr
json/to_json.cr
Class Method Summary
-
.from_io(io : IO, format : IO::ByteFormat)
Reads a float from the given io in the given format.
Instance Method Summary
- #%(other)
- #-
- #<=>(other : BigInt)
- #<=>(other : BigRational)
- #days
- #fdiv(other)
- #finite?
- #hours
- #infinite?
- #milliseconds
- #minutes
- #modulo(other)
- #nan?
- #remainder(other)
- #seconds
-
#to_big_i : BigInt
Returns a BigInt representing this float (rounded using
floor). -
#to_io(io : IO, format : IO::ByteFormat)
Writes this float to the given io in the given format.
- #to_json(io)
Instance methods inherited from module Comparable(T)
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from module Comparable(T)
<(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) <=>, ==(other : Complex) ==, abs abs, abs2 abs2, cis cis, clamp(range : Range)
clamp(min, max) clamp, divmod(number) divmod, i i, round(digits, base = 10) round, sign sign, significant(digits, base = 10) significant, step(limit = nil, by = 1)
step(limit = nil, by = 1, &block) step, to_big_f to_big_f, to_c to_c, to_yaml(yaml : YAML::Generator) to_yaml
Class methods inherited from struct Number
zero : self
zero
Instance methods inherited from module Comparable(T)
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from module Comparable(T)
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from struct Value
==(other)
==
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO) inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
from_json(string_or_io) : self
from_json,
from_yaml(string : String) : self
from_yaml,
hash
hash,
inspect(io)
inspect,
name : String
name,
to_s(io)
to_s,
|(other : U.class)
|
Class Method Detail
Reads a float from the given io in the given format.
See IO#read_bytes.
Instance Method Detail
Returns a BigInt representing this float (rounded using floor).
Writes this float to the given io in the given format.
See IO#write_bytes.