[Date Index] [Thread Index] [Author Index]
Overriding the definition of Plus so that it does exact arithmetic: Bug or Feature?
Consider the definition of the new function P which is supposed to be exactly like Plus, except that converts all "Real" floats to exact rational numbers before adding them up. P[a_Real,b__]:= Plus[Rationalize[SetAccuracy[a, Infinity]], b]; SetAttributes[P, Attributes[Plus]] P[c__]:= Plus[c] This works as expected, e.g. P[0.1, -1/10] returns 1/180143985094819840 which is the difference between the closest double-float to 0.1, and the exact number 1/10. Given this, let's put EXACTLY the same definition on Plus Unprotect[Plus]; Plus[a_Real,b__]:= Plus[Rationalize[SetAccuracy[a, Infinity]], b]; Protect[Plus]; 0.1-1/10 gives 0. (wrong) 0.1+x gives 3602879701896397/36028797018963968 + x (hm, right). My guess is that any Rules inserted on Plus by the user are checked AFTER the built-in simplifications attached to Plus. I consider this a bug. Any workaround come to mind, if you consider this a feature :) RJF