+ 4

Can someone explain to me why the program prints "false"?

From math import sqrt Print(sqrt(3)*sqrt(3)==3)

30th Apr 2017, 4:18 AM
kris
4 odpowiedzi
+ 40
If you look at the output of: print(sqrt(3)*sqrt(3)) You get the following: 2.9999999999999996 This is a floating point precision problem with sqrt() in Python that has to be handled by the user. I haven't tried it myself, but you could try the library mpmath, or make your own sqrt function using import decimal.
30th Apr 2017, 4:29 AM
David Hutto
David Hutto - avatar
+ 4
All languages have default precision problems (libraries help)...related to base-2 (binary) architecture. Various demos below...the first is direct, the second is an 'explorer', the third shows a 'precision delay' when trying to match a float (the point is: in another base...other than base 10...it doesn't always succeed). https://code.sololearn.com/ckZEfBRiUrEA/?ref=app https://code.sololearn.com/WYCTOysAGnLv/?ref=app https://code.sololearn.com/cIC8qOLAfL7m/?ref=app
13th May 2017, 4:46 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
# It's a general thing to be aware of with floats # try this in the python playground to see another example. print(0.1+0.2) # or open the developer tools on a browser # and try 0.1 +.0.2 in the console to see the # same issue with javascript
30th Apr 2017, 5:22 AM
richard
+ 2
it is because you use == operator
5th May 2017, 2:04 PM
Exotic_Code
Exotic_Code - avatar