abstract struct Number

Overview

The top-level number type.

Included Modules

Direct Known Subclasses

Defined in:

chem/core_ext/number.cr

Constant Summary

DEGREE_TO_RADIAN = Math::PI / 180
RADIAN_TO_DEGREE = 180 / Math::PI

Instance Method Summary

Instance Method Detail


[View source]
def close_to?(rhs : Number, delta : Number = 0) : 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]
def degree #

[View source]
def degrees #

[View source]
def radian #

[View source]
def radians #

[View source]
def scale(min : Number, max : Number) : Float64 #

Scales the number into the given range. The result will be between zero and one.

10.scale(0, 100)   # => 0.1
2.5.scale(0, 5)    # 0.5
401.scale(50, 500) # => 0.78

[View source]
def scale(range : Range(Number, Number)) : Float64 #

Scales the number into the given range. The result will be between zero and one.

10.scale(0, 100)   # => 0.1
2.5.scale(0, 5)    # 0.5
401.scale(50, 500) # => 0.78

[View source]
def unscale(min : Number, max : Number) : Float64 #

Reverts the scaling in the given range.

0.5.unscale(0, 100) # => 50.0 0.1.unscale(0, 5) # => 0.05 5.scale(-4, 20).unscale(-4, 20) # => 5.0


[View source]
def unscale(range : Range(Number, Number)) : Float64 #

Reverts the scaling in the given range.

0.5.unscale(0, 100) # => 50.0 0.1.unscale(0, 5) # => 0.05 5.scale(-4, 20).unscale(-4, 20) # => 5.0


[View source]