I'm looking through some of the code from the Google Closure Library and I found this line:
var isNegative = number < 0.0 || number == 0.0 && 1 / number < 0.0;
I've figured that the reason for such an initially strange looking sign-check is to identify -0
as negative, but is there any reason to use 0.0
instead of 0
?
An integer is a whole number and a floating-point value, or float, is a number that has a decimal place.
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
A floating point number parsed from the given string , or NaN when the first non-whitespace character cannot be converted to a number. Note: JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level.
Floating-point numbers have two advantages over integers. First, they can represent values between integers. Second, because of the scaling factor, they can represent a much greater range of values.
(A lot has changed since 2011 when this answer was posted - see updates below)
BigInt has been out in V8 (Node.js and Chromium-based browsers) since May 2018. It should land in Firefox 68 - see the SpiderMonkey ticket. Also implemented in WebKit.
BigDecimal hasn't been implemented by any engine yet. Look at alternative library.
It's been over 4 years since I wrote this answer and the situation is much more complicated now.
Now we have:
Soon we'll have:
It means that the number of numeric types available in JavaScript will grow from just one:
to at least the following in WebAssembly:
(Technically the internal representations of all integer types are unsigned at the lowest level but different operators can treat them as signed or unsigned, like e.g. int32.sdiv
vs. int32.udiv
etc.)
Those are available in typed arrays:
asm.js defines the following numeric types:
There is only one number type in JavaScript – the IEEE 754 double precision floating-point number.
See those questions for some consequences of that fact:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With