Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of floats between two floats

Say I have two Python floats a and b, is there an easy way to find out how many representable real numbers are between the two in IEEE-754 representation (or whatever representation the machine used is using)?

like image 487
astrofrog Avatar asked Aug 27 '10 20:08

astrofrog


1 Answers

AFAIK, IEEE754 floats have an interesting property. If you have float f, then

(*(int*)&f + 1)

under certain conditions, is the next representable floating point number. So for floats a and b

*(int*)&a - *(int*)&b

Will give you the amount of floating point numbers between those numbers.

See http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm for more information.

like image 51
Dr. Snoopy Avatar answered Nov 11 '22 11:11

Dr. Snoopy