Well let me start by asking you a simple math.. what is 1+1 , its 2 said your mind.. so you know your math..
Now what is 10*0.01 ??
Take your time..
0.1?? Are your sure??
Certainly??
Well someone would agree to disagree..who someone?? well someone called FLOAT or DOUBLE...
One of them would say
0.099999999786
Why you ask?? Because they work that way..
Oversimplified?? Want to know more.. Alright.. lets dive right through it..
We humans are more accustomed to base 10 calculations while computers are with base 2. If we intend to know what base 2 or base 10 is.. When we say base 10 it revolves around numbers 0-9, for base 2 its just two number 0-1. So what would be 4 in base 10, it would 0100 in base 2.
A more precise explanation for this would be something around the exact representation of numbers as per IEEE-754 floating domain. For the base of 2 floats and doubles often do not have exact representation of the numbers.. Similar to for base of 10, when we divide 1/3 it would give a recurring of 333333333 i.e 0.333333333.
Let me try to explain a little bit more, for a finite representation of a number in any system viz decimal, binary or octal the basic requirement is that the number/fraction needs to be in the power of that particular system.
Like for example 524/1000 can be finitely represented as 0.524 but 1/3 cannot which is why we add a recurring sign on top of a number 0.3333‾ . Similarly for numbers in a binary system to be finitely represented the numbers have to be in power of 2 . For example in case of fractions (0.25)10 can be represented as (0.01)2 However for (0.13)10 to be represented it will become difficult as the calculations can go infinitesimally long.
To get more info on the binary handling refer below links
Binary to Decimal - Dealing with fractions
Add to the way computers or computing system stores the data, there will be loss of some bits. More information provided in the links above.
Now this isn't the case with only a particular programming language like JAVA, similar behavior is observed when dealing with languages like RUST, C# and so on!!
The difference while using float or double for money transaction aren't obvious at first but as we keep on doing the additions, subtractions, multiplications and divisions there will be a compounding of the small inaccuracies and then boooooom... you would be scratching your heads on where did your code go wrong!! or for the sake of the title.. Where did the money go????
This is where LOSS OF SIGNIFICANCE concept comes in..
Floats and Doubles do not exactly have a proper binary representation which kind of makes computers a little dizzy which might result in a loss of precision/scale.
Well some might think, why do we need to worry about loss of 0.01 or even 0.11.. Let me try to bring light to few of the matters.
Since floats and double work under the boundaries of approximation, financial calculations using those are always risky. So how do we handle money when programming..
There are different ways where one can work on currency..
For example.
1. One can leverage other datatypes like BigDecimal in JAVA, Money in SQL Server, Currency(Annotation), Decimal in C#, PHP has bcMath, Javascript has big.js
2. Banker's Rounding Algorithm
There are other approaches as well where you can calculate for example dollars and cents separately.
Its not just about using a better way to handle the numbers it's also about making sure what you do further about the numbers..
For example do you round 1.5 to 2 or 1.. This might sound trivial but makes a lot of impact in some situations.
There have been instances where the minute difference has gone to make a tremendous impact sometimes turning fatalistic.
Below are few mentions for the same..
Case 1: Patriot Missile Defense
Year 1991, during the Gulf War, a loss of significance in the Patriot MIM missile battery prevented it from intercepting a missile in Dhahran, Saudi Arabia contributing to death of around 28 soldiers, wounding 260 from the US Army.
Case 2: Rounding or Flooring
In the 1980s, a mysterious loss was observed in the Vancouver Stock Exchange for a new stock. Further investigation brought the root cause of using floor instead of round which contributed to nearly 50% of the total loss. A correction in the same saw the new stock open at 1000 which is tremendous jump considering it closed on 500.
We can find many such examples in fields of Space, Politics, Banking and many more..
I believe there would be some clarity on where the money went or who stole the money here..
That's it for now.. will update this page as and when something might come up..
Till then... remember the code talks!!!
Powered by Froala Editor