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

From: Konstantin Olchanski <olchansk_at_triumf.ca>
Date: Fri, 9 Oct 2009 08:46:45 -0700


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().

-- 
Konstantin Olchanski
Data Acquisition Systems: The Bytes Must Flow!
Email: olchansk-at-triumf-dot-ca
Snail mail: 4004 Wesbrook Mall, TRIUMF, Vancouver, B.C., V6T 2A3, Canada
Received on Fri Oct 09 2009 - 17:47:10 CEST

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