3 ms·
I never noticed math.isclose() and have often done the naive a - b < eps There is also (for rolling your own fn) import sys sys.float_info.epsilon alth
by PennRobotics 5y ago
I never noticed math.isclose() and have often done the naive a - b < eps
There is also (for rolling your own fn)
import sys
sys.float_info.epsilon
although I can't immediately think of an advantage over the module.
- gotaquestion 5y agoWhy do you consider that naive? Also, I wouldn't use epsilon, because that is a very small number and is related to representation of numbers. I would instead use a larger "tolerance" value, in `if |a-b| < tol ...` because that indicates the expected accuracy of your algorithm (unless you're really working with 1e-80 tolerance?). But TFA explains that. But back to point (a) is that not reasonable? (If not, I've got some code to change... :)
- PennRobotics 5y agoNaive because it's just the easiest way. (Also, right, I forgot abs.) I imagine the downside with a chosen "epsilon" (which I used as an alias for any tolerance) is that it can be truncated on some machines, plus this equation on its own doesn't do relative accuracy. On an LEGO EV3, Python's float is 1e38 max with 7 significant digits. I've never run into problems, which is why I keep using it, too.