Op 20-07-16 om 07:42 schreef Steven D'Aprano: > Floating point maths is hard, thinking carefully about what you are doing and > whether it is appropriate to use == or a fuzzy almost-equal comparison, or if > equality is the right way at all. > > "But thinking is hard, can't you just tell me the answer?" > > No. But I can give some guidelines: > > Floating point arithmetic is deterministic, it doesn't just randomly mix in > error out of spite or malice. So in principle, you can always estimate the > rounding error from any calculation -- and sometimes there is none.
I would like to see a practical example of such an outcome. > Arithmetic on integer-values (e.g. 1.0) is always exact, up to a limit of > either 2**53 or approximately 1e53, I forget which. (That's why most > Javascript > programmers fail to notice that they don't have an integer type.) So long as > you're using operations that only produce integer values from integer > arguments > (such as + - * // but not / ) then all calculations are exact. It is a waste > of > time to do: > > x = 2.0 > y = x*1002.0 > is_equal(y, 2004.0, 1e-16) > > when you can just do y == 2004.0. But why perforem integer arithmetics in floats, isn't that a waste of time too? I really see no reason to use floats if you know all your results will be integers. -- Antoon. -- https://mail.python.org/mailman/listinfo/python-list