Message333222
Yes, something like this:
class APPROXIMATE:
"""Takes a floating point number and implements approximate equality."""
def __init__(self, value):
self.value = value
def __eq__(self, other):
return abs(self.value - other) / (abs(self.value) + abs(other)) < 1e-6
def __repr__(self):
return f"APPROXIMATE({self.value})"
Then the following would hold:
got = {
"name": "Petter",
"length": 1.900001
}
expected = {
"name": "Petter",
"length": APPROXIMATE(1.9)
}
assert got == expected
But not
got["length"] = 1.8
assert got == expected |
|
Date |
User |
Action |
Args |
2019-01-08 11:07:49 | Petter S | set | recipients:
+ Petter S, xtreak |
2019-01-08 11:07:48 | Petter S | set | messageid: <1546945668.24.0.684739181753.issue35656@roundup.psfhosted.org> |
2019-01-08 11:07:48 | Petter S | link | issue35656 messages |
2019-01-08 11:07:48 | Petter S | create | |
|