Re: Overriding the definition of Plus so that it does exact arithmetic: Bug or Feature?

*To*: mathgroup at smc.vnet.net*Subject*: [mg120257] Re: Overriding the definition of Plus so that it does exact arithmetic: Bug or Feature?*From*: Dana DeLouis <dana01 at me.com>*Date*: Fri, 15 Jul 2011 04:09:05 -0400 (EDT)

> This works as expected, e.g. > P[0.1, -1/10] returns 1/180143985094819840 Not sure, but is this a good alternative? p[v___]:=Total[Rationalize[List@v,0]] p[0.1,-1/10] 0 p[a,1./3,1.1,2.3,5.6,2/3.] 10+a = = = = = = = = = = HTH : >) Dana DeLouis $Version 8.0 for Mac OS X x86 (64-bit) (November 6, 2010) On Jul 14, 5:24 am, Richard Fateman <fate... at cs.berkeley.edu> wrote: > 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