0

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

Alia
  • 71
  • 9
  • 2
    what is the challenge exactly? Your code already does what you want – Neel Jun 02 '19 at 13:39
  • If you have a large data set you might want to use [pandas](https://pandas.pydata.org/) to process it. Pandas can efficiently read a file and parse many floats very fast. – Håken Lid Jun 02 '19 at 13:39

1 Answers1

1

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

verejava
  • 7
  • 5