MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Errors in handling real numbers ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg8660] Errors in handling real numbers ?
  • From: "Piotr G. Kin" <Piotr.Kin at per.clw.csiro.au>
  • Date: Mon, 15 Sep 1997 02:48:49 -0400
  • Organization: CSIRO
  • Sender: owner-wri-mathgroup at wolfram.com

I was trying to develop a function that would return number of digits
after the decimal point in a real number. The function would work as
follows:

In[]:=	a = 1. 5.1234;
	b = 1. 0.00123;
	c = 1. 0.5;
	digitsafterdot[{a,b,c}]
	
Out[]=	{4,5,1}

(
The results would differ from those obtained through application of
RealDigits[] i.e.,

In[]:=	Apply[Length[#1] - #2 &, RealDigits[{a,b,c}], {1}]
Out[]=	{15,18,16}
)

I was REALLY surprised to discover that there is a serious error in
handling real numbers. Consider the expressions:

In[]:=	x = 0.11;
	NestList[FractionalPart[10#] &, x, 5]
	NestList[(10. # - IntegerPart[10. #]) &, x, 5]
Out[]=	                      -16            -15           -14           
-13
        {0.11, 0.1, 8.88178 10   , 8.88178 10   ,8.88178 10   , 8.88178
10   }

                              -16            -15          
-14            -13
        {0.11, 0.1, 8.88178 10   , 8.88178 10   ,8.88178 10   , 8.88178
10   }

The results are as expected and are identical for both expressions.
Remarkably, when value of x changes to (say) 0.12 the following happens:

In[]:=	x = 0.12;
	NestList[FractionalPart[10#] &, x, 5]
	NestList[(10. # - IntegerPart[10. #]) &, x, 5]

Out[]=	{0.12, 0.2, 1., 1., 1., 1.}
	{0.12, 0.2, 1., 1., 1., 1.}

The results are identical again but hardly expected. For visual
demonstration run the expression:
 
In[]:=	Plot[Function[x, Chop[Nest[FractionalPart[10 #] &, x ,2]]][y],{y,
0.00, 0.10}, PlotPoints -> 100]

It gets even funnier with numbers with precision higher than
$MachinePrecision:

In[]:=	x = 0.20000000000000000;
	NestList[FractionalPart[10#] &, x, 5]
	NestList[(10. # - IntegerPart[10. #]) &, x, 5]

Out[]=	17

{0.2000000000000000,1.000000000000000,1.00000000000000,1.0000000000000,1.000000000000,1.00000000000}
	{0.2000000000000000,0.,0.,0.,0.,0.}

The results of two mathematically equivalent expression are
significantly different.

Am I to sensitive or is this a serious problem?

Peter.

P.S. All the above results were obtained on INTEL Pentium 166 PC.


___________PLEASE NOTE NEW TEL, FAX NUMBERS AND E-MAIL
ADDRESS_____________

Piotr G. Kin
CSIRO Land & Water              e-mail: Piotr.Kin at per.clw.csiro.au
Private Bag                     WWW:    www.dwr.csiro.au
PO Wembley  WA  6014            Phone   +61 8 9333 6205
Australia                       Fax     +61 8 9387 8211
___________________________________________________________________________

This is NOT an official form of communication, it is however. It does
NOT
commit me to anything or any place or even CSIRO. All views stated here
are
wholly or partially my own or conveniently borrowed or vaguely
remembered.
___________________________________________________________________________

            ... And now for something completely different ...

The information in this message is IN-CONFIDENCE and is intended for
CSIRO
personnel only. Distribution of this information to non-CSIRO personnel
must be authorised in accordance with CSIRO Code of Conduct (Policy
94/18)
___________________________________________________________________________



  • Prev by Date: Re: 3d "Mesh" question
  • Next by Date: HTML generation in 3.0.0
  • Previous by thread: Dynamics of billiards
  • Next by thread: Re: Errors in handling real numbers ?