method

prev_float

Importance_2
v2_4_6 - Show latest stable - 0 notes - Class: Float
prev_float() public

Returns the previous representable floating-point number.

(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.

Float::NAN.prev_float is Float::NAN.

For example:

p 0.01.prev_float  #=> 0.009999999999999998
p 1.0.prev_float   #=> 0.9999999999999999
p 100.0.prev_float #=> 99.99999999999999

p 0.01 - 0.01.prev_float   #=> 1.734723475976807e-18
p 1.0 - 1.0.prev_float     #=> 1.1102230246251565e-16
p 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14

f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
#=> 0x1.47ae147ae147bp-7 0.01
#   0x1.47ae147ae147ap-7 0.009999999999999998
#   0x1.47ae147ae1479p-7 0.009999999999999997
#   0x1.47ae147ae1478p-7 0.009999999999999995
#   0x1.47ae147ae1477p-7 0.009999999999999993
#   0x1.47ae147ae1476p-7 0.009999999999999992
#   0x1.47ae147ae1475p-7 0.00999999999999999
#   0x1.47ae147ae1474p-7 0.009999999999999988
#   0x1.47ae147ae1473p-7 0.009999999999999986
#   0x1.47ae147ae1472p-7 0.009999999999999985
#   0x1.47ae147ae1471p-7 0.009999999999999983
#   0x1.47ae147ae147p-7  0.009999999999999981
#   0x1.47ae147ae146fp-7 0.00999999999999998
#   0x1.47ae147ae146ep-7 0.009999999999999978
#   0x1.47ae147ae146dp-7 0.009999999999999976
#   0x1.47ae147ae146cp-7 0.009999999999999974
#   0x1.47ae147ae146bp-7 0.009999999999999972
#   0x1.47ae147ae146ap-7 0.00999999999999997
#   0x1.47ae147ae1469p-7 0.009999999999999969
#   0x1.47ae147ae1468p-7 0.009999999999999967
Show source
Register or log in to add new notes.