Return Positive value of a number

 

Is there a diferent way or a function to return the positive value of a number?

I know I can use this kind of code, but is there any simple way?


if(number<0)
     number=number*-1;
 
MathAbs
 
Amine Abed #:
MathAbs

Thanks!


 
Here are manual versions:

number=number*(-1 + (2 * (number > -1)));
number=(number < NULL) ? number * -1 : number;
 
Dominik Egert #:
Here are manual versions:

You really like to complicate things :-D
 
Oh, here, try this, it's easier....

😂
#define inline_MathAbs(x) ((x < NULL) ? x * -1: x)
number=inline_MathAbs(number);
 
I would dare to claim this version is the fastest possible:

What do you think?

number=number * ((number < NULL) ? -1 : 1)
 
   number=number<0?-number:number;
 
number=sqrt(pow(number,2));

For those who _really_ like to complicate things and spend resources. :)

 
There can be a bit negation as well
pos=~num+1

but the fastest way should be

MathAbs

if the compiler is written correctly, because the function should develop to the fastest machine code available

 

Thanks folks.
It’s very useful this kind of post with many suggestions. 

I think the best users choice should be the fastest way. 

Reason: