Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3707

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

From Richard Fateman <fateman@cs.berkeley.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Overriding the definition of Plus so that it does exact arithmetic: Bug or Feature?
Date 2011-07-14 09:24 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ivmcjs$fau$1@smc.vnet.net> (permalink)

Show all headers | View raw


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





Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Overriding the definition of Plus so that it does exact arithmetic: Bug or Feature? Richard Fateman <fateman@cs.berkeley.edu> - 2011-07-14 09:24 +0000

csiph-web