Computer Education

Floating Point Math || 0.1+0.2=0.30000000000000004 || Tech OH

 

Hello guys welcome back again and today I am again back with an awesome topic and hope you guys will like it . Today in the article I am going to explain you about floating point math. If you are reading this article from the PC it will be more helpful for you all guys . So what will be your answer being a computer science students if someone asked you what will be 0.1+0.2 ??? You guys may say it suddenly that it quite easy it’s answer is 0.3 but as you are computer student the answer is wrong but I am not saying that it is wrong with your daily life applied mathematics. So this is call floating point math or floating point arithmetic . 

How/Why does this happened ?

As we know that you computer or any devices understood and work in binary and binary is base 2 . And the number system we human used  to use is decimal number system in most of the time. And you may also know that the decimal point we enter in computer is converted to binary system so that our computer understood it and give us the output of our input. Computers can only naturally stores integers , so they need some way of representing decimal numbers and this representation arrives with some degree of inaccuracy or unreliable. That is why 0.1+0.2 is not equals to 0.3.

If you are thinking it’s difficult to understood about this than please drink a glass of cold water and make yourself cool first because it’s very easy if you give your best focus while reading this. We have a base 10 system and it can only express fractions that uses a prime factor of the base (10). And the prime factor of the base(10) are 2 and  5. For ex :- 1/2 , 1/4 , 1/8 and 1/10 can  be expressed clearly because the denominators all use the prime factor of 10 . But 1/3 , 1/6 and 1/9 , this all are repeating decimals because their denominators use a prime factor of 3 or 7. And we have already talk something about the binary or base 10 the prime factor is only 2. So you guys maybe clarify that we can express fraction clearly which contain 2 as a prime factor . In the binary number  system or base 10 , 1/2 , 1/4 , 1/8 would all be  expressed  cleanly as decimal  whereas 1/5 or 1/10 would be repeating decimals in the base 2 or binary system in which the computer is operating in . When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer’s base 2 (binary) number into a more human readable base 10 number. So this all happened while converting the decimal to binary . 

 

If you Like to support me you can donate me , mail me on [email protected]

 

 

Some examples for different programming are :-

Language Code Result
        C      
#include<stdio.h>
int main(int argc, char** argv) {
    printf("%.17f\n", .1+.2);
    return 0;
}
0.30000000000000004
                          C++                                                           
#include <iomanip>
std::cout << std::setprecision(17) << 0.1 + 0.2
0.30000000000000004
Mathematica                                  
0.1 + 0.2                 
                                             0.3
Python 3                                           
print(.1 + .2)              

And

.1 + .2
0.30000000000000004

And0.30000000000000004

 

By :- Admin OH

Hits: 58

OH

Leave a Reply

Your email address will not be published. Required fields are marked *