Dong-gyun Kim
Dong-gyun Kim

Reputation: 431

float precision comparison by specific digit

How to test if two floats are identical until a specific digit?

I tried,

aa1 = 0.043403
aa2 = 0.043392
print(int(aa1*1000) == int(aa2*1000))
>> True

I want to follow this way, but my data include NAN value, it cannot convert it to intro anyhow. I also tried math.isclose but it's tricky.

For example, I wanted to keep until 3 digits and applied the math.isclose

aa3 = 0.013041
aa4 = 0.012545
aa6 = 0.012945

print(math.isclose(aa3, aa4, abs_tol = 0.0001))
>>Flase
print(math.isclose(aa3, aa5, abs_tol = 0.0001))
>>True

But I want to get False for both cases.

Any simple idea??

Upvotes: 2

Views: 607

Answers (3)

Giacomo Catenazzi
Giacomo Catenazzi

Reputation: 9533

rel_tol should be enough:

import math

def check(a, b):
    print(math.isclose(a, b, rel_tol=0.001), abs(a/b - 1.) < 0.001)

check(aa1, aa2)
check(aa3, aa4)
check(aa3, aa6)

gives the expected

True True
False False
False False

Note: the second calculation do not handle two equal zeros: easy to add. Not sure if underflow will affect it.

Upvotes: 0

nagyl
nagyl

Reputation: 1644

Create a function which takes 2 numbers, a and b, and it has a preset threshold value. If the difference between a and b is less than the treshold, return True.

def isSame(a, b):
    return abs(a - b) < treshold

Where treshold can be a float. 0.0001 will ignore after the 3rd digit. Or cut the number as a string after 3 digits, and convert it back to float.

Edit, cut off method:

def isSame(a, b, digit):
    return float(str(a)[0:digit+2]) == float(str(b)[0:digit+2])

We ignore every number after the 3rd digit, in case your digit = 3.

Upvotes: 3

Imran
Imran

Reputation: 885

You can do it easily using assertAlmostEqual Below is the code

import unittest

unittest.TestCase.assertAlmostEqual(value1,valu2,decimalUpto)

In your case it can be like

unittest.TestCase.assertAlmostEqual(aa1 ,aa2 ,3)

Good Luck

Upvotes: 2

Related Questions