Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Floats can do it. Do your computation, and then check the inexact flag.


TIL such a thing exists. Although it seems like it is rarely used; there is some discussion here https://cs.stackexchange.com/questions/152373/what-are-the-u...

I guess most practical calculations would end up triggering the inexact flag.


Floats can represent all integers with magnitude <=2^53. Which is a sight smaller than integers, under the standard representation (magnitude <2^62), but still plenty for most applications—the point is to use this to implement integer math, not float math. That covers multiplication, addition, and subtraction; div/mod is admittedly a bit trickier, though it's a doozy with avx512.

(There's also a cute consequence: you can construct numbers outside the contiguous range of representable integers; so long as you never incur any rounding error, the result will be correct, and the inexact flag will not be set.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: