Project

General

Profile

Actions

Feature #9834

closed

Float#{next_float,prev_float}

Added by akr (Akira Tanaka) over 10 years ago. Updated over 10 years ago.

Status:
Closed
Target version:
-
[ruby-core:62562]

Description

I'd like to add Float#next_float and Float#prev_float which
returns next representable floating-point number and
previous representable floating-point number.

p 3.0.next_float #=> 3.0000000000000004
p 3.0.prev_float #=> 2.9999999999999996

These methods can be useful to examine the behavior of floating-point numbers.

For example, they can be used to examine floating-point error in 0.1 + 0.1 + ... + 0.1.

f = 0.0
100.times { f += 0.1 }
p f                            #=> 9.99999999999998       # should be 10.0 in the ideal world.
p 10-f                         #=> 1.9539925233402755e-14 # the floating-point error.
p(10.0.next_float-10)          #=> 1.7763568394002505e-15 # 1 ulp (units in the last place).
p((10-f)/(10.0.next_float-10)) #=> 11.0                   # the error is 11 ulp.
p "%a" % f                     #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5.  16 - 5 = 11 ulp.

The methods are implemented using nextafter() function described in
IEEE 754 (Appendix), C99 and POSIX.
It seems the function is pretty portable on Unix variants.

However I implemented missing/nextafter.c for environments which don't have the function.

Any idea?


Files

next_float-and-prev_float.patch (11.9 KB) next_float-and-prev_float.patch akr (Akira Tanaka), 05/13/2014 09:47 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0