Reply to post: DC: The wrong parts

The Wun Show: Douglas Crockford has been sniffing JavaScript's bad parts again

Claptrap314 Silver badge

DC: The wrong parts

I spent a decade doing microprocessor validation at AMD & IBM. This work included building a floating point emulator. I know far, FAR too much about both the standards and the actual behavior of microprocessors (and their repeated bugs) to let this one go.

Building an integer library out of floating point operations is up there with taking out your own appendix. You better be good (he was an MD), desperate (he was stuck in the Antarctic winter), and with a high tolerance for pain & crazy (he was Russian).

Floating point operations are a LOT slower than integer. If you really want to go the route of building up an MP library, steal from CPAN (it's okay) or better yet https://gmplib.org/ (it's better).

There are three ways to go from float to int. The first is to use floats in the in range, and use the hardware float-to-int to convert. This method has a LOT of bugs historically. It is also generally one of the slowest instructions in an FPU. The second is to use denorms. Of course, denorms are also a rich source of errors, and behind the curtains, denorms are calling the same circuits as the previous convert function, only they do it for every instruction. The last is to ensure that all of the results are bounded so that you can add 3 times a power of 2 to them and keep the same exponent bits. You can then read off the integer part from the bottom bits. Of course, you have to be careful that when you add or subtract, that you rebalance your number after every add and subtract (no multiples for you!). So, again, REALLY slow. And all-original library. As someone who has reason to be confident that if he really had to, he could build such a library, just don't.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon