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

See Float literals in the language reference.

Defined in:

chem/core_ext/float.cr

Instance Method Summary

Instance methods inherited from struct Number

*(other : Chem::Spatial::Vec3) : Chem::Spatial::Vec3 *, close_to?(rhs : Number, delta : Number = 0) : Bool close_to?, degree degree, degrees degrees, radian radian, radians radians, scale(min : Number, max : Number) : Float64
scale(range : Range(Number, Number)) : Float64
scale
, unscale(min : Number, max : Number) : Float64
unscale(range : Range(Number, Number)) : Float64
unscale

Instance Method Detail

def close_to?(other : Number, delta : Number = {{ @type.constant("EPSILON") }}) : Bool #

Returns true if numbers are within delta from each other, else false.

1.0.close_to?(1.0)                          # => true
1.0_f32.close_to?(1.0)                      # => true
1.0.close_to?(1.0 + Float64::EPSILON)       # => true
1.0_f32.close_to?(1.0 + Float32::EPSILON)   # => true
1.0.close_to?(1.0005, 1e-3)                 # => true
1.0.close_to?(1.0 + Float64::EPSILON*2)     # => false
1.0_f32.close_to?(1.0 + Float32::EPSILON*2) # => false
1.0.close_to?(1.01, 1e-3)                   # => false

[View source]