You don’t want any errors in your commercial or Scientific applications, do you?
In a follow-up blog, we will see a solution using decimals support.
The 64 bits are used like this: 1 bit for sign,11 bits for exponent and 53 bits for significant digits.
Unlike some other languages, there is no specific type for integers. Starting with ECMAScript 2015, one can check if a number is in the double-precision floating-point number range using Number.isSafeInteger() as well as Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER.
In the next sections, we will go into detailed explanations for each of these issues.
The issue is that some floats do not have an exact representation. This leads to incorrect results and results that cannot be compared with certainty. Another way to put it, float numbers can only approximate decimal numbers that are commonly used for many commercial purposes.
At this point, you may be wondering what I am really talking about. Let’s look at a simple example:
Try this in a browser console: (to open the console for example with Google Chrome, type CRTL Shift J on Windows/Linux or Command+Option+J on Mac—see here).
The reason is that 0.1 has the following representation as a double: 0.1000000000000000055511151231257827021181583404541015625
So, you can imagine how repeating this kind of precision errors over lots of transactions could result in unacceptable financial results (million dollars a year differences on some transactions).
Note: Only dyadic numbers have an exact representation. You can check here and also here for more info.
Here is a tool to check if a number has an inexact representation as a float.
Float computations are not deterministic (they produce different results) across machine architecture, compiler versions, etc. (Read this for issues encountered by game programmers).
This could potentially lead to different results when run on client side where the devices from cell phone to tablets to desktop run different OSes, hardware CPUs and floating point accelerators.
I haven’t personally run into this, and would be curious to know if you have. Please leave a note in the comments section if you ran into this issue.
console.log(Number.MAX_SAFE_INTEGER) produces 9007199254740991
console.log(Number.MIN_SAFE_INTEGER) produces -9007199254740991
Here is an example of errors you could get into:
Number.MAX_SAFE_INTEGER + 1: 9007199254740992 → correct.
Number.MAX_SAFE_INTEGER + 2: 9007199254740992 → clearly incorrect.
When doing any operations on your numbers, you can use Number.isSafeInteger() to verify that you have not exceeded the range.
In a follow-up blog, we will see how one can use a decimal library to minimize the issues outlined here.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
You have the right to request deletion of your Personal Information at any time.
You can also ask us not to pass your Personal Information to third parties here: Do Not Sell My Info
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.