Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through all possible floating-point values

How can I list all possible values of a floating-point data type? I can do this using a union in C or C++ but will that be portable?

How can this be done in other languages? Javascript?

Let's just assume I am using this iteration to map theta to sin(theta).

like image 726
Agnel Kurian Avatar asked Apr 21 '10 09:04

Agnel Kurian


2 Answers

Take a look at the man pages for nextafter() and nextafterf(). They let you advance from a floating point number to the next closest one. You could use one of these to visit each FP number in order.

like image 179
John Gordon Avatar answered Oct 12 '22 01:10

John Gordon


How can I list all possible values of a floating-point data type?

By bit-twiddling the IEEE-754 representation of your float value, for a float you need 2^32 different representations assuming 4 Bytes per float would require 16GB of memory.

I assume you need a lookup table for the sine function, simply loop from 0 to 2*PI in steps of your required precision.

like image 29
stacker Avatar answered Oct 12 '22 02:10

stacker