3

I have a javascript program (running in jsdb which uses Mozilla Spidermonkey 1.8, not in a browser) that I need to convert float and doubles to/from the appropriate number of bytes of their IEEE representations. Java has Float.floatToIntBits() and Float.intBitsToFloat() and similar methods for Double.

Is there a way to compute these functions in Javascript? (and yes, I know that all javascript numbers are essentially of type double)

I guess I could follow the algorithms in the javadoc for Double.longBitsToDouble() and Float.intBitsToFloat() but that only covers one direction, and I need both.

Jason S
  • 178,603
  • 161
  • 580
  • 939
  • 1
    possible duplicate of [javascript float from/to bits](http://stackoverflow.com/questions/2003493/javascript-float-from-to-bits) – Jason S Mar 17 '11 at 14:41
  • Possible duplicate of [javascript float from/to bits](https://stackoverflow.com/questions/2003493/javascript-float-from-to-bits) – phuclv Aug 15 '18 at 16:38

2 Answers2

4

javascript float from/to bits

From the answer: IEEE-754 Floating-Point Conversion

Community
  • 1
  • 1
JaredMcAteer
  • 19,840
  • 4
  • 48
  • 63
  • Just took a look at the Javascript in the babbage.cs.qc.edu site -- It may produce correct answers, and may work well on the online calculator, but it's horribly written & not suitable for reuse in my opinion. – Jason S Mar 17 '11 at 17:53
1

As i know, javascript doesn't have such native methods.

Here is some sort of things, you want to have. Please examine the source code: http://babbage.cs.qc.edu/IEEE-754/IEEE-754hex32.html

Alexander Yezutov
  • 3,034
  • 2
  • 20
  • 23