Re: Problems with precision

From: Patois Yannick (patois@ganil.fr)
Date: Thu Nov 18 1999 - 07:44:12 MET


Hi,

This problem has nothing to do with ROOT but with floating point
arithmetic in general.

You've just hit the fact that a decimal number (a number that can be
written in a finite number of digits) in base 10 is not always a decimal
number in base 2. So that as soon as you entered the number it has been
'rounder' because the floating point unit can only work with finites
number of digits.

Your problem will appear in any program like :
#include <stdio.h>
main()
{
        double t;
        printf("enter number : ");
        fflush(stdin);
        scanf("%lf",&t);
        printf("%.20lf  %.20lf\n",t,t,t);
}

Compiled on a linux i586 box with egcs gives :

enter number : 2.2
2.20000000000000017764  2.20000000000000017764

You see that the ROOT output is just a rounded value of the one given by
a simple C program...

There is no simple way to avoid this kind of problem (working only with
integers, using BCD notation for numbers, boudaries representation,...).

To make it short, because it seems to be your problem : Never compare 2
floating point values by doing : if (a==b) {...}

Do that instead : if (fabs(a-b)<EPSILON) {...}

When EPSILON is a majorant of the estimated error you can have.

Hope it helps,


	Yannick

 _/ Yannick Patois _________________ Address (home) __________________
| irc(undernet): Garp on #france25+ | La Villa des Sciences            |
| email : patois@ganil.fr           | 12, avenue de Cambridge          |
| web :http://www.sura.org/~patois/ | 14200 Herouville-Saint-Clair     |
| Tel/Fax-home:+33 (0)2 31 94 50 32 | FRANCE                           |
|___________________________________|__________________________________|


On Thu, 18 Nov 1999, Jan Valenta wrote:

> Hi all,
> I've written and debugged numerical program in C++. It works perfectly
> under NT and Linux (g++), but in ROOT there is some problem with 
> precision. It's obvious that the problem is in the following:
> 
> root [60] Double_t x
> root [61] x = 2.2
> (double)2.20000000000000018e+00
> root [62] Float_t s
> root [63] s = 23.2
> (double)2.31999999999999993e+01   
> 
> What to do with the garbage? How is it possible that there is "...0000018"
> at the end of 2.2 and "...999993" instead of "23.20000" ?
> At first sight it may look like a tiny deviation, but in my program it
> causes some arguments to be out of ranges.
> I would appreciate if someone could help.
> Thanks,
> 		Honza Valenta    
> 
> 



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:43 MET