Hey Friends!?Today I’ll talk about the concepts of floating point numbers. This is one of the trickiest part in the computer world. It is really very complicated to understand the concept of floating point numbers especially for a newbie. So, I thought of writing this article to help you understand floating point numbers better. If you are a programmer, and you think you know about the floating point numbers, well give it another thought.
Floating Point Numbers
People expect computers to be accurate. And they must expect it because we rely too much on computers for our day-to-day activity. But if you are a programmer, then one way or another you will face a situation where your computers won’t give you a correct answer for an obvious simple arithmetic operation. You don’t believe me, Right? Well, go on and run this piece of code on Java.
class SimpleFloatAddition{
public static void main (String [] args){
System.out.println((0.1+0.2));
}
}
This is the output for the program,
Now, imagine if you are creating a software which has to add up simple prices of two products, then it’s not accurate. The difference is so minute that we can ignore it but it’s there and that can cause you problem. What if an angry customer finds that out :D. If you know why this happens, then this article is probably not for you, but if you are curious to know the reason just to impress your friends or interviewer then go on and read the complete article. I have tried my best to explain it in the easiest way.
What are Floating Point numbers?
These are actually scientific notations. Let’s understand this concept with a real-world example. Suppose, you are an astrophysicist and you have to multiply the speed of light with some very small value.
Say,
300000000 * 0.00000015
We, humans, know a trick with the help of which we can solve these in less than 20sec. If we ask a human to solve this thing, he will probably go for it like this,
300000000 => 3*108
0.00000015 => 1.5*10-7
Which is, 4.5*10 = 45
This is the concept of Significant Numbers. Speed of light is approx. 300000000ms but in actual it’s something like 2.99977?? so we round it up and write it as 300000000. Floating point numbers are basically a mathematics of significant number.
Let’s talk about Base 10,
We follow base 10 rules for calculation,
100, 10, 1, -, 1/10, 1/100, 1/1000
100, 10, 1, -, 0.1, 0.01, 0.001
For computer, it will be like this,
4, 2, 1, -, ?, ? , 1/8, 1/16
4, 2, 0, -, 0 0011?. (Infinity)
A typical 32-bit machine can store only up to 23 digits and after that, it just cuts all the access digits right off. Computers also store the position of the decimal where it is in some scientific notation in base 2 and that’s what floating point actually is.
Now, let’s get back to the problem.
When we added 0.1+0.2, we got 3.00000000000004, to understand this let’s simply take the above concept that floating points numbers work on significant numbers. Which means that they don’t understand the concept of recursion as we humans do. So, for us,
1/10 + 2/10 = 3/10,
Or,
0.1+0.2 = 0.3
For computers, they don’t understand the concept of recursion so they would do something like this,
0.00011001100110011001100110011001100110011001100110011001100110… + 0. 0.00110011001100110011001100110011001100110011001100110011001100… = 0.3000 0000000000004
Yeah, it is that simple. I hope you got the concept here. If not, then no problem just go through the concept one more time, or simply enjoy Tom Scott’s Numberphile video here?
Conclusion
In nearly all cases floating point numbers are very close to the actual calculation and at top of that we do not need precision of more than 23 decimal place in most of the case and if we do we have 64 bit machines for that. Floating point numbers has really made this easy for us. These are very efficient and provides a great accuracy with less space requirements.
If you liked the article then please do share and comment below. It will really help others to increase their awareness on the concept of floating point numbers.
If you are a programmer then you would love to read these articles as well: