10

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.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    You're making a what?!? (impressed) A compiler for what, exactly? – amphetamachine Jan 26 '11 at 06:34
  • @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 Jan 26 '11 at 06:35
  • 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 Jan 26 '11 at 07:26
  • 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 Jan 26 '11 at 07:54
  • 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 Feb 02 '11 at 22:10
  • I found your question while looking for the same thing for the same reason, see https://github.com/ghewgill/lentil. We should collaborate. – Greg Hewgill Jun 05 '11 at 10:38

1 Answers1

8

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

gsnedders
  • 5,532
  • 2
  • 30
  • 41
  • @gsnedders- This is beautiful! Thanks so much for the link. I'm out of votes for today but I'll be sure to give you +1 as soon as the next day rolls around. – templatetypedef Feb 02 '11 at 22:59