Sunday, May 27, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...