10
votes

Does anyone know of a JavaScript library that accurately implements the IEEE 754 specification for 32-bit floating-point values? I'm asking because I'm trying to write a cross-compiler in JavaScript, and since the source language has strict requirements that floating-point values adhere to IEEE 754, the generated JavaScript code must do so as well. This means that I must be able to get exactly the correct IEEE 754 values for addition, subtraction, multiplication, and division of 32-bit floats. Unfortunately, the standard JavaScript Number type is a 64-bit double, which will give different results than what I'm expecting. The project really does have to be in JavaScript, and this is the only major stumbling block I have yet to get past.

I'm also running into this problem with 64-bit longs.

1
You're making a what?!? (impressed) A compiler for what, exactly? - amphetamachine
@amphetamachine- If all goes well (crosses fingers!) an implementation of the JVM that runs in JavaScript. It's for teaching intro CS online. I didn't expect this to be the bottleneck - I figured out how to do threading and everything before this stumped me. :-) - templatetypedef
I'm guessing you've already invested a lot of effort into this, so I think it might actually be worth attempting the implementation yourself. The details are left as an exercise for the reader. :) - nss
One trick that might help: using a bitwise operator can force the interpreter to treat a number as a 32-bit signed integer, instead of a 64-bit floating point. (number|0) is particularly useful, as it doesn't affect the number but forces it to be treated as an integer value. - Zach
I do find it pretty ridiculous that Javascript enshrines IEEE-754's binary64 in its language specification (see ECMA-262 §8.5), but that's neither here nor there… - ephemient

1 Answers

8
votes

The Closure library has a 64-bit long implementation, at least.