Re: (Pi/2. ) != (90.0 DegToRad)

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Thu, 21 Dec 2006 14:53:38 +0100


Hi Kathrin,

This is the expected behavior. You should never test the equality of 2 floating point numbers.
Depending on the compiler rounding options, it may work or not. You should always test withing some numerical precision (eg 1e-13)

Rene Brun

Kathrin Stoerig wrote:
> Hello,
>
> I have been working with some trigonometric functions of the
> "TMath"-Class when I discovered a peculiar behaviour of ROOT v5.0.12e.
> I have appended a stand-alone piece of my code. The comments at the
> end show my output when compiling or interpreting.
> While interpreting with Cint, everything is okay, but when I compile
> it, the following comparison is astonishing to me:
>
> "Double_t phi=90.0*TMath::DegToRad();
> if (phi==TMath::Pi()/2.0) returns "false",
> while if(phi==TMath::PiOver2()) returns true?! "
>
> Much more interesting is the fact, that:
> "Double_t phi=90.0*TMath::DegToRad();
> cout<<phi<<endl;
> if (phi==TMath::Pi()/2.0) now returns "true" - instead of "cout" e.g.
> an empty "for"-loop is possible and still returns "true", a variable
> declaration as "Int_t a=5" returns "false" and "Double_t
> b=TMath::Cos(5.)" returns "true" again.
>
> Any ideas?
> As you can see in my code, I tried a lot of possibilities (casting the
> numbers to doubles, updating ROOT to version 5.0.14 etc.) without
> success. Please tell me whats wrong or how to prevent this
> infelicitous situation.
>
> Best regards,
> Kathrin Stoerig
>
> -------------------------------------------------------------------
>
> Kathrin Störig
>
> II.Physikalisches Institut
> Universitaet Goettingen Telefon:
> +49 551 39 12214
>
> Friedrich-Hund Platz 1 D-37077 Goettingen Telefax: +49 551 39
> 4493
>
> Kathrin.Stoerig_at_phys.uni-goettingen.de
> <mailto:Kathrin.Stoerig_at_phys.uni-goettingen.de>
>
> --------------------------------------------------------------------
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
>
> #include <iostream>
> #include <fstream>
>
> #include "TMath.h"
>
>
> Int_t PiHalbeUngleich90(void){
>
> Double_t psi_spur=(Double_t)90.0;
> Double_t phi=((Double_t)-180.0)+psi_spur;
> phi=phi*TMath::DegToRad();
> if(phi==(-TMath::Pi()/(Double_t)2.)){
> cout<<"case 1....."<<endl;
> }
> if(phi==(-TMath::PiOver2())){
> cout<<"case 2....."<<endl;
> }
> if(phi==(-(90.0*TMath::DegToRad()))){
> cout<<"case 3....."<<endl;
> }
> cout<<"Phi von Grad: "<<(Double_t)phi<<endl;
> cout<<"Phi von RAD: "<<-TMath::Pi()/2.<<endl;
> cout<<"-90GradToRad"<<-(90.0*TMath::DegToRad())<<endl;
>
> if(phi==(-(TMath::Pi()/2.))){
> cout<<"case 1a....."<<endl;
> }
> if(phi==(-(90.0*TMath::DegToRad()))){
> cout<<"case 3a....."<<endl;
> }
> phi=(Double_t)phi;
> if(phi==(-(TMath::Pi()/2.))){
> cout<<"case 1b....."<<endl;
> }
> if(phi==(-(90.0*TMath::DegToRad()))){
> cout<<"case 3b....."<<endl;
> }
> if(phi==(Double_t)(-(90.0*TMath::DegToRad()))){
> cout<<"case 4....."<<endl;
> }
>
> return 0;
> }
>
> /* Output when interpreting:
>
> root [0] .x PiHalbeUngleich90.C
> case 1.....
> case 2.....
> case 3.....
> Phi von Grad: -1.5708
> Phi von RAD: -1.5708
> -90GradToRad-1.5708
> case 1a.....
> case 3a.....
> case 1b.....
> case 3b.....
> case 4.....
> (Int_t)0
> root [1]
> */
>
> /* Output when compiling:
>
> root [0] .x PiHalbeUngleich90.C++
> Info in <TUnixSystem::ACLiC>: creating shared library /home/pcatlas03/kstoeri/workspace/dez2006_zufallszahlen/./PiHalbeUngleich90_C.so
> case 2.....
> Phi von Grad: -1.5708
> Phi von RAD: -1.5708
> -90GradToRad-1.5708
> case 1a.....
> case 1b.....
> (Int_t)0
> root [1]
> */
>
>
Received on Thu Dec 21 2006 - 14:53:48 MET

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:02 MET