Re: Compatibility b/w ROOT types and regular C++ types

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Fri, 9 Oct 2009 17:58:48 +0200


Konstantin,

Note that TMath::Pi() is an inline function and any current compiler will process it as a constant.

Rene

 Olchanski wrote:
> On Thu, Oct 08, 2009 at 07:59:38PM -0700, Suvayu Ali wrote:
>
>> My code looked something like this,
>>
>>> double pi = acos(-1);
>>> float delta_phi2 = fabs(2*TMath::Pi - delta_phi);
>>>
>
>
> Traditionally, (early C, Fortran, etc), one is supposed to write
> acos(-1.0) and
> fabs(2.0*TMath::Pi)
>
> because early compilers did not know to automatically convert
> into to double (they did not know fabs() and acos() take
> an argument of type double).
>
> Also instead of TMath::Pi you can say
>
> #include <math.h>
> fabs(2.0*M_PI) <--- this becomes a compile-time constant
>
>
> You also may have 2 performance bugs, which may or may not be important
> depending on the use of the code:
>
>
>> double pi = acos(-1);
>>
>
> Invokes the acos() function, which tends to be pretty expensive,
> even on CPUs with hardware acos().
>
>
>> fabs(2*TMath::Pi())
>>
>
> Invokes the Pi() function every time. If you say 2.0*M_PI, it becomes
> a compile-time constant (I am not sure if GCC can fold fabs(constant)
> into a constant, though).
>
> Both problems, if they are problems, should show up on in
> the profiler (gprof & co) as excessive number of calls to acos(), fabs() and Pi().
>
>
>
Received on Fri Oct 09 2009 - 17:59:17 CEST

This archive was generated by hypermail 2.2.0 : Fri Oct 09 2009 - 23:50:03 CEST