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

Huh, and in the case that e1 and e2 are large, the mantissa overflows into the exponent, multiplying the result by 2.


I guess we're lucky that the IEEE 754 committee put the exponent in front of the mantissa. Or maybe they were aware of this trick all along ...


This arrangement makes ordering easier anyway which is probably why they chose it.

If we have some positive 32-bit integers A and B then if A < B, f32::from_bits(A) < f32::from_bits(B)

Edited to add anecdote:

I actually had a bug in realistic (my Rust crate to implement Hans Boehm's "Towards an API for the Real Numbers") which I only fixed recently, for converting my Real number into a floating point type where I might end up with too many mantissa bits, but then sometimes coincidentally the bottom bit of the exponent is zero, and so instead of increasing the exponent by one I actually overwrite it with a one and that's the same result.

I finally caught that when it occurred to me to do "thorough testing". That is, take all possible 32-bit floats, turn them into a Real, then, turn that Real back into a 32-bit float, this should be a roundtrip, if it's not we've found a bug. There are "only" a few billion of these values to test, a machine can do that.

I fixed the 64-bit routines for the same bugs, but I don't have enough years to run all the 64-bit tests the same way and discover any that aren't paralleled.

[Of course there are presumably still bugs in the conversion algorithms which may either be symmetric, and thus the bug doesn't impact a round trip, or they don't affect the binary fractions used exclusively by floats, Real has proper rationals and various other things like the square root of integers, Pi, etc. which we can convert to a float and lose precision but we never get these from converting a float because floats are always binary fractions.]


> I fixed the 64-bit routines for the same bugs, but I don't have enough years to run all the 64-bit tests the same way and discover any that aren't paralleled.

Minor thought for today: there are more than 2^64 bits of RAM in the world.


> If we have some positive 32-bit integers A and B then if A < B, f32::from_bits(A) < f32::from_bits(B)

That is, if both are valid floats that represent a finite non-zero number.


Correct.

Floats support both -0 and 0 they compare equal but their bit representations don't. Zero can be tricky.

Not a numbers (NaNs) always compare as false. Their bit representations won't.


> realistic (my Rust crate to implement Hans Boehm's "Towards an API for the Real Numbers")

So cool, I didn't know anyone was working on this!


If you're familiar with that work, realistic implements most of it (some trig functions I didn't get to yet) but it's deliberately not 1.0 because I'm still tinkering with core API things like, I changed how the errors work this week.

If you're not familiar, a fact about the reals which cannot be emphasised enough is that almost all real numbers are non-computable. This serves to properly temper expectations, regardless of whether you know what the mathematical term "Almost all" means. We're going to be able to compute some useful reals, and then do arithmetic with them, and not get miserably bad rounding errors like for floating point for example. We just need to keep in mind that "towards" here is just gesturing in a direction, it's not a destination which can be reached.


Haha I'm familiar, I actually also tried to implement the paper at some point. It's such a cool idea and a great example of the difficulty that goes in to making a really good UX, even for something as simple-sounding as a calculator app.


What's obvious to Kahan is being relearned by the rest of us.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: