Discussion:
[ROOT] Power function in TMath
Alexander Voigt
2010-07-20 18:41:27 UTC
Permalink
Dear ROOT developers,

I would like to make a suggestion for an improvement within the TMath class.

Physicists working with ROOT make heavy use of mathematics functions in
TMath. Often they want to calculate powers with an integer exponent and
are therefore doing things like

TMath::Power(x, 2)

According to this ATLAS recommendation

https://twiki.cern.ch/twiki/bin/view/Atlas/CalculatingIntegerPowers

one should always use std::pow() for these purposes (#include <cmath>),
because of a factor 10 in performance gain.
For this reason I propose the implementation of a function

Double_t TMath::Power(Double_t, Int_t)

which simply encapsulates

double std::pow( double, int );

or

long double std::pow( long double, int );

from the cmath module of the C++ standard library.

Many thanks in advance,
Alexander Voigt
Benjamin Bannier
2010-07-20 19:45:42 UTC
Permalink
Hi Alexander,
Post by Alexander Voigt
According to this ATLAS recommendation
https://twiki.cern.ch/twiki/bin/view/Atlas/CalculatingIntegerPowers
one should always use std::pow() for these purposes (#include
<cmath>), because of a factor 10 in performance gain.
Looking at cint/cint/lib/stdstrct/stdcxxfunc.h it appears that CINT
already does what the documentation suggests: #include <cmath>, and
map pow to std::pow with a using directive. These are defined there:

float pow (float, float);
float pow (float, int);
Post by Alexander Voigt
For this reason I propose the implementation of a function
Double_t TMath::Power(Double_t, Int_t)
which simply encapsulates
double std::pow( double, int );
or
long double std::pow( long double, int );
from the cmath module of the C++ standard library.
So if inside a ROOT macro you say pow(2, 2) or pow(2,2.) you should get
what you want (this doesn't even require you to include cmath since CINT
appears to already know about it).

Not that using a ROOT macro probably won't give you the performance
boost seen by in that test since you don't have the full set of
optimizaitons available -- and if you would compile your code with ACLiC
or directly you could just use std::pow from cmath yourself.


Benjamin
--
“Eighty percent of success is showing up.” – Woody Allen
Alexander Voigt
2010-07-21 18:40:37 UTC
Permalink
Hi Benjamin,
Post by Benjamin Bannier
So if inside a ROOT macro you say pow(2, 2) or pow(2,2.) you should get
what you want (this doesn't even require you to include cmath since CINT
appears to already know about it).
Not that using a ROOT macro probably won't give you the performance
boost seen by in that test since you don't have the full set of
optimizaitons available -- and if you would compile your code with ACLiC
or directly you could just use std::pow from cmath yourself.
You are completely right, I could always use std::pow(double,int) for
myself. I just thought, that it would be convenient for the users to
have a TMath::Power(Double_t,Int_t) function in ROOT which is faster
than TMath::Power(Double_t,Double_t) for integer exponents. The latter
just encapsulates pow() from math.h, so, why not have
std::pow(double,int) encapsulated in TMath? Ok, just a thought. :)

Kind regards,
Alexander Voigt
Axel Naumann
2010-07-21 19:06:21 UTC
Permalink
Hi Alexander,

what's in TMath is independent of what CINT does. So yes, I agree, this
overload should be added to TMath and I believe Lorenzo is planning on
doing that. I let him speak for himself, though :-) (He's just back from
a conference and still catching up with emails, so please give him a day
or two.)

Cheers, Axel.
Post by Alexander Voigt
Hi Benjamin,
Post by Benjamin Bannier
So if inside a ROOT macro you say pow(2, 2) or pow(2,2.) you should get
what you want (this doesn't even require you to include cmath since CINT
appears to already know about it).
Not that using a ROOT macro probably won't give you the performance
boost seen by in that test since you don't have the full set of
optimizaitons available -- and if you would compile your code with ACLiC
or directly you could just use std::pow from cmath yourself.
You are completely right, I could always use std::pow(double,int) for
myself. I just thought, that it would be convenient for the users to
have a TMath::Power(Double_t,Int_t) function in ROOT which is faster
than TMath::Power(Double_t,Double_t) for integer exponents. The latter
just encapsulates pow() from math.h, so, why not have
std::pow(double,int) encapsulated in TMath? Ok, just a thought. :)
Kind regards,
Alexander Voigt
Lorenzo Moneta
2010-07-21 22:00:46 UTC
Permalink
Hi Alexander,

I am planning to change in Tmath to use for all basic mathematical function those defined in cmath and not math.h
When I'll do this, I will add also TMath::Power(double, int).
This will happen probably when I'll be back from vacation in August.

Cheers, Lorenzo
Post by Benjamin Bannier
Hi Alexander,
what's in TMath is independent of what CINT does. So yes, I agree, this
overload should be added to TMath and I believe Lorenzo is planning on
doing that. I let him speak for himself, though :-) (He's just back from
a conference and still catching up with emails, so please give him a day
or two.)
Cheers, Axel.
Post by Alexander Voigt
Hi Benjamin,
Post by Benjamin Bannier
So if inside a ROOT macro you say pow(2, 2) or pow(2,2.) you should get
what you want (this doesn't even require you to include cmath since CINT
appears to already know about it).
Not that using a ROOT macro probably won't give you the performance
boost seen by in that test since you don't have the full set of
optimizaitons available -- and if you would compile your code with ACLiC
or directly you could just use std::pow from cmath yourself.
You are completely right, I could always use std::pow(double,int) for
myself. I just thought, that it would be convenient for the users to
have a TMath::Power(Double_t,Int_t) function in ROOT which is faster
than TMath::Power(Double_t,Double_t) for integer exponents. The latter
just encapsulates pow() from math.h, so, why not have
std::pow(double,int) encapsulated in TMath? Ok, just a thought. :)
Kind regards,
Alexander Voigt
Loading...