+ 1
Why the output is false
a = 0.1 b = 0.2 c = 0.3 print(a+b==c)
6 Answers
+ 2
Because it's in float value the calculation internally will change value in decimal terms with the c value which is leading to false as the answer. Hence you can use round function to round to 1 to get true as answer
Example a = round(value, roundoff_value)
+ 1
Isn't 0.1+0.2 = 0.3
đ€đ€
+ 1
Rahul Bankar ,
in many cases this issue does not matter if it is possible to do rounding.
however there are reasons that the precission matters. in this case we can use a different numerical data format `decimals`. (decimal module is shipped with the python installation):
https://sololearn.com/compiler-playground/c2CXgHef0K0G/?ref=app
0
Rahul Bankar here is similar code with explanation in the comments:
https://sololearn.com/compiler-playground/cda62498xsW9/?ref=app
0
Brian ,
it is not just fun to read all of these comments and figure out what is true or what is an assumption đ€Ł
0
https://sololearn.com/compiler-playground/cUYHw2KG6Ws1/?ref=app
I believe its because your trying to use a base-10 system on a base-2 system (binary). You can only express fractions of a base by using its prime factors. The prime fractors of 10 are 2 and 5. So fractions like 1/2, 1/4, 1/8, 1/10,âŠ. these numbers will result in a nice rounded number. If you were to do numbers whose fractions were 1/3,1/6,1/9, then you will get repeated numbers.
Computers use binary, so they use a base-2 system. The prime fractors for 2 isâŠ2. So only fractions like 1/2,1/4,1/8 result in nice clean numbers. 1/5 and 1/10 are repeated because there prime factor is 5.
For repeated numbers, computers canât write an infinite amount of something so the numbers are rounded off.
https://0.30000000000000004.com