Is there a way in C++ to get the absolute value of an integer ??
if after a calculation i get a value returned -2 how can i use some function to get 2
I don't want to write
PHP Code:if(c<0) c = c -c -c ;
Printable View
Is there a way in C++ to get the absolute value of an integer ??
if after a calculation i get a value returned -2 how can i use some function to get 2
I don't want to write
PHP Code:if(c<0) c = c -c -c ;
abs(value)
Don't know if there are any float/double etc methods for this - probably, but abs is the right one for ints.
Need math.h I think for this.
HD
In C++, it's the <cmath> header :)
You can use fabs() for floating-point numbers.
Thank should have tried it first.
There are a lot of 'abs' functions - cabs(), labs(), etc.
The generally accepted dogma is that you should use a macro for functions like sgn(), abs(), etc because they are faster - a lot faster.
Kinda like this:
You can also force 'inlining' by using macros -Code:#define abs(c) ( (c<0) ? (-1)*c : c )
This does a complex reciprocal for instance.
arg & out are type Complex (a struct)
Code:#define LCMPLXrecip(arg,out) \
{ long denom; denom = lsqr((arg).x) + lsqr((arg).y);\
if(denom==0L) overflow=1; else {(out).x = divide((arg).x,denom,bitshift);\
(out).y = -divide((arg).y,denom,bitshift);}}