+ 4
Can someone explain to me why the program prints "false"?
From math import sqrt Print(sqrt(3)*sqrt(3)==3)
4 Respuestas
+ 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.
+ 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
+ 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
+ 2
it is because you use == operator