Petter S <[email protected]> added the comment:
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
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue35656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com