JavaScript: demystifying  numbers
Tags: javascript, computerscience, programming
Published: 2021-01-12 17:43:01

> 
This story contains the anecdote behind how I came across the quirkiness of JS number during the development of a personal finance app. 

---

As the application would grow and populate more numeric data, I came to wonder how commercial finance applications deal with fraction points of currencies. This question was derived from the multiple fraction point numbers after all the money values tallied up by JavaScript. When building a commercial application, this would be a prime discussion among developers, accountants and other stakeholders. As result of my lack of time and expertise in finance, I wasn’t sure how to approach to this issue and couldn’t conduct significant research. Considering this is a premature version of a personal finance app which doesn’t set out any rules in terms of any complex financial covenants, I just decided to keep it simple for now. Furthermore, the current implementation wouldn’t have a great effect on the actual monetisation of the users’ financial flow in a direct manner.


Putting aside how to define the fraction numbers, the reason why the aggregated result from JavaScript returns the unexpected fraction numbers was investigated.

> 
This would be an example of the quirkiness of JavaScript numbers. Wouldn't you expect the result would be 58.87 if you have learn the basic arithmetic from school unless you are a CS student or a well-versed developer? 

![carbon.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610462468862/tzkHBwW0I.png)

As I understand it, all computational processes run with binary code and representational data such as images, audio, characters and numbers which are stored as binary and are encoded in different formats to deliver their means. Specifically, JavaScript encodes all the numeric values with double precision floating point numbers(64 bit) following IEEE Standard. Even though we may expect the aggregate result from the example above to simply be 58.87, it returns all the fraction points by the nature of how JavaScript processes numeric value with double-precision floating point number. As such I decided to delve into this further to provide rationale and to advocate my decision regarding rounding up/down fraction numbers.


![alexander-sinn-YYUM2sNvnvU-unsplash.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1610472422302/Tt1W_PhLQ.jpeg)
> 
<span>Photo by <a href="https://unsplash.com/@swimstaralex?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexander Sinn</a> on <a href="https://unsplash.com/s/photos/binary?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>

### IEEE754 Double-Precision binary floating-point format : Binary64

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610464013100/j_F6Ea3MI.png)
*[&sup1;]  Significand precision is implicitly 53 bits. However, 1 bit is not stored since it goes through the normalisation and it always leads with value “1”. It’s called implicit bit, hidden bit and so on.*

---

JavaScript has adapted Double-Precision floating point format as its standard for numbers. As we can conjecture from its naming, this format provides a wider range of numbers and higher accuracy compared to single-precision or half-precision floating-point format. 

Specifically speaking, JavaScripts can process numbers in the range between `Number.MAX_SAFE_INTEGER(253 - 1)` and `Number.MIN_SAFE_INTEGER(-(253 - 1))` based on the binary 64 format. However, ECMAScript 2020 which was published in June 2020 updated their specification and it includes a new built-in object `BigInt` which provides a larger number representation for JavaScript. 

Naturally this format takes up more memory and requires a better processor to perform this computation. During this research, I also learned how to convert binary to denary and vice versa. This was a very constructive learning in order to understand the quirkiness of JavaScript Number. As such I’d like to articulate how the denary number is converted to 64bit binary number under the hood.

#### Denary 19.25

First, Convert the whole number 19 to binary : divide the number until the remainder is 0/1.    
The Converted binary is **10011**.    

![carbon (1).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610464947324/peWS7OttM.png)
> 
*Read the remainders from bottom to top for the whole number.*

---
Secondly, Convert the fraction number 0.25 to binary : multiply the fraction numbers by 2 until the value returns to 0.    
The Converted binary is **0.01**.

![carbon (2).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610465394098/GAs7yxVyr.png)
> 
*Read the decimal points from top to bottom at this time*

---
Thirdly, Combine the two parts of the number and Normalise for significand and unbiased exponent (move the binary point to after leftmost 1 or to the right where the first “1” value exists) : Once the binary numbers are normalised, the number of times we moved the decimal point to the leftmost 1[&sup2;]will be the exponent in the base 2 notation.

__10011.01 = 1.001101 &times; 2&#8308;__


[&sup2;]  _If whole number conversion to binary starts with a decimal point, for example, 0.00110011, you need to move the decimal point to the right where the first “1” value is located. In this case, the result will be 1.10011 &times; 2&#8315;&sup3;_

---
Fourthly, Get the biased exponent based on precision.

__4 + 1023 = 1027&#8321;&#8320; = 10000000011	&#8322;__


![carbon (4).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610467952544/AMxtuMfV1.png)
> 
*Read the remainders from bottom to top just like the first step we did.*


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610467985393/h0SkFjYX0.png)

---
Fifthly, Determine the significand removing leading 1 from step 3.

__1.001101__

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610468088574/2qjknM9xb.png)

---
Finally, we have successfully converted Decimal number 19.25 to Binary64 format.


Now, I will convert a 64bit binary to the denary value which is a simplified demonstration to show you how the computer processes this under the hood.


#### 64bit binary 
![carbon (5).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610468390780/RuDT0ZBR9.png)

For an easier explanation, refer to this table.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610468433253/dSJuSN9sV.png)

*e* = 2&sup1;&#8304; + 2&#8304; = 1024 + 1 = 1025&#8321;&#8320;    
*p* = *e* - 1023 = 2

*p indicates precision.*


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610469172985/76aZH4lYw.png)

The first column indicates the *implicit significand value 1* which is called *implicit bit[&sup1;]* and the value we get from the biased exponent subtracting the unbiased exponent denotes where the bit index starts from. If the exponent values are positive, move towards the right-hand side and if negative, move towards the left-hand side from the implicit bit as you can see on the table. Now, we have the denary value, 5. 

*n* =  2&#0178; + 2&#8304; = 4 + 1 = 5

If the number value is just an integer like in the example above, the calculation is straightforward. However, decimal is more complicated and it sometimes requires rounding up/down depending on the last value of significand.


#### 64bit binary 
![carbon (6).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610469519705/zTQIEEmzM.png)


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610469555923/gnAqS1pfz.png)


*e* =  2&#8313; + 2&#8312;  + 2&#8311; + 2&#8310; + 2&#8309; + 2&#8308; + 2&#0179; + 2&#0178; + 2&#8304;    
&nbsp;&nbsp;&nbsp;= 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 1    
&nbsp;&nbsp;&nbsp;= 1021&#8321;&#8320;    
*p* = *e* - 1021 = -2

*p indicates precision.*

This time exponent value is negative. So, we need to move to the left hand side two times. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610470175734/Zq5bvwno5.png)
*purple cells denotes its repetitions of the pattern.*

*n* = 2&#8315;&sup2; + 2&#8315;&#8309; + 2&#8315;&#8310; + 2&#8315;&#8313; + 2&#8315;&#0185;&#8304; + 2&#8315;&#0185;&#0179; + 2&#8315;&#0185;&#8308; + 2&#8315;&#0185;&#8311; + 2&#8315;&#0185;&#8312; + 2&#8315;&#0178;&#0185; 2&#8315;&#0178;&#0178; + 2&#8315;&#0178;&#8309; + 2&#8315;&#0178;&#8310; + 2&#8315;&#0178;&#8313; + 2&#8315;&#0179;&#8304; + 2&#8315;&#0179;&#0179; + 2&#8315;&#0179;&#8308; + 2&#8315;&#0179;&#8311; + 2&#8315;&#0179;&#8312; + 2&#8315;&#8308;&#0185; + 2&#8315;&#8308;&#0178; + 2&#8315;&#8308;&#8309; + 2&#8315;&#8308;&#8310; + 2&#8315;&#8308;&#8313; + 2&#8315;&#8309;&#8304; + 2&#8315;&#8309;&#0179; + 2&#8315;&#8309;&#8308;
&nbsp;&nbsp;&nbsp;= - 0.3

By the nature of binary and the larger bit binary deals with a wider range of fraction values for higher accuracy and precision, tallying-up the value of fraction point numbers by JavaScript returns quirky(?) values unlike we would expect.