Re: Beware of adding 0.0
- To: mathgroup at smc.vnet.net
- Subject: [mg50222] Re: Beware of adding 0.0
- From: "Mirek Gruszkiewicz" <gruszkiewicz at ornl.gov>
- Date: Sat, 21 Aug 2004 03:04:27 -0400 (EDT)
- Organization: Oak Ridge National Laboratory
- References: <cg4fcn$cf5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"paul" <paul_tan at aoI.com> wrote in message news:cg4fcn$cf5$1 at smc.vnet.net... > In Mathematica 5.0, I get a very strange result when I add 0.0 to a number, > why is this? See below: > > p = SetPrecision[314159265358979323, 25] > q = SetPrecision[314159265358979323., 25] > r = SetPrecision[314159265358979323.00000000000000000000, 25] > s = SetPrecision[p + 0.0, 25] > {p == q, q == r, r == s, r == p} > {Tan[s], N[Tan[p]], Tan[q], Tan[r]} > > > \!\(3.14159265358979323`25.*^17\) > Out[47]= > \!\(3.14159265358979323`25.*^17\) > Out[48]= > \!\(3.14159265358979323`25.*^17\) > Out[49]= > \!\(3.14159265358979328`25.*^17\) *****NOTICE the 28 instead of 23 at the > end, why does this happen? ******* > Out[50]= > {True, True, False, True} > Out[51]= > {1.599808, -1.12979, -1.1297927, -1.1297927} > Also beware of multiplying by 1.0: s1=SetPrecision[p*1.0, 25] 3.14159265358979328`25*^17 This is normal: o=SetPrecision[1/9, 25] oo=SetPrecision[1./9, 25] 0.1111111111111111111111111 0.1111111111111111049432054 Here is the magic: ox = SetPrecision[o + 0.0, 25] ooxx = SetPrecision[oo + 0.0, 25] 0.1111111111111111049432054 0.1111111111111111049432054 Generally, beware of most floating point operations. MG