Sunday 27 May 2018

How to get approximate float value in Python?




The value of A is



A = 0.4000070095062256


and after that I have the below code to check the value of A



if (A == 0.400):
print('pass')
else:
print('Fail')


I want to print Pass. The above code is printing Fail every time. How will i get approx value of A??



Note: In the condition I have to check the value of A with exactly equal to 0.400. and I mean to say I need approx value of A in float.


Answer



You could check, if the distance to 0.4 is less than a given limit, e.g.:



if abs(A - 0.4) < 0.0001:
print("Pass")

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...