Number of floats between two floats

后端 未结 4 1925
一整个雨季
一整个雨季 2021-02-20 03:41

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 representati

4条回答
  •  借酒劲吻你
    2021-02-20 03:52

    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.

提交回复
热议问题