Does JavaScript have double floating point number precision?
Asked Answered
R

3

36

I know it's an odd question, but does JavaScript have the capacity to work with double's as opposed to single floats? (64 bit floats vs. 32 bits.)

Rhinoplasty answered 31/8, 2010 at 3:57 Comment(0)
K
38

All numbers in JavaScript are 64-bit floating point numbers.

Ref:

http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference

http://www.crockford.com/javascript/survey.html

Khajeh answered 31/8, 2010 at 4:2 Comment(2)
But bitwise operations will convert it to a 32 bit integer.Mcculley
ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdfGrassquit
C
40

According to the ECMA-262 specification (ECMAScript is the specification for Javascript), section 8.5:

The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic

Source: http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf (PDF)

Confab answered 31/8, 2010 at 4:5 Comment(2)
ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdfGrassquit
This is the only correct answer. It refers to and quotes the ECMAScript specification, which is the only source that matters. The other answer only has sources that are not definitive.Melleta
K
38

All numbers in JavaScript are 64-bit floating point numbers.

Ref:

http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference

http://www.crockford.com/javascript/survey.html

Khajeh answered 31/8, 2010 at 4:2 Comment(2)
But bitwise operations will convert it to a 32 bit integer.Mcculley
ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdfGrassquit
B
3

In javascript type number it's float 64-bit number that support IEEE 754 standard and it's like double in C. And you can create 32-bit typed arrays by commands below and control each byte in each component by binding corresponded buffer.

let a = new Float32Array(length);
let b = new Float64Array(length);

But note that it's not supported in IE9, here browser compatibility table.

If you want extended presicion like long double, you can use double.js or decimal.js library.

Babbie answered 16/9, 2018 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.