C++ simple question about abs()


Another dummy newbie C++ question:

Why does this code line does this error ? (Important and quite philosophical question, in fact)

    double _thisPeriod = abs(_thisTime - m_timeStart);  

D:\PROJETS\_OF\apps\ByMySelf\myMPD_02\src\Animation.cpp|32|error: call of overloaded ‘abs(double)’ is ambiguous|

And this one not ?

    double _thisPeriod = abs((int)(_thisTime - m_timeStart));  

Confusingly, there’s two abs(). There’s the c++ one and the c one. When you use the std namespace (which OF does) it’s pulling in the c++ one, which is declared like this:

int abs ( int n );  
long abs ( long n );  

hence the need for your cast to int. When you don’t use the c++ version, you use the c one, which you might be thinking you’re using (but you’re not) and that looks like this:

double abs (      double x );  
float abs (       float x );  
long double abs ( long double x );  

long story short for the absolute value of floating point numbers you want

double _thisPeriod = fabs(_thisTime - m_timeStart);    


quick and clear as usual in this place :slight_smile: