1

Greeting All,

I want to compare float number that but I don't wanna round the number here is a simple example:

p = 15.0060732 n = 15.00637396 if p == n: print('=') if p > n: print('>') if p < n: print('<') 

I want p < n , is there any method to hlpe me do that. * Note: I have a big table that represent these value but it's random so i can't determin the floating point for all table.

any help will be appreciated

2
  • 2
    what is the challenge exactly? Your code already does what you want Commented Jun 2, 2019 at 13:39
  • If you have a large data set you might want to use pandas to process it. Pandas can efficiently read a file and parse many floats very fast. Commented Jun 2, 2019 at 13:39

1 Answer 1

2

Python compares floating-point numbers. Because of the precision, you should use the isclose method of the math module.

If the difference between the two numbers is less than 1e-9, then the two floating point numbers are considered equal. Math.isclose(a, b, rel_tol=1e-9)

example:

import math p = 15.0060732 n = 15.00637396 print(math.isclose(1.0, 1.0000000001)) print(math.isclose(1.0, 1.0000000001, rel_tol=1e-10)) print(math.isclose(p, n)) print(math.isclose(p, n, rel_tol=1e-2)) 

result:

True False False True

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.