Is there an efficient way to make a float positive without squaring it?

Scenario: You have a float and you need it to be Positive, but it could be Positive or Negative. What is the most efficient way to ensure that it is Positive?

I know you can multiply it by itself(square it). Is there a faster/better way?

Would checking to see if it is less than 1, setting a bool, and branching to either multiply by +1 or by -1 be more efficient than say, multiplying -1752 by -1752? Or multiplying -17523 by -17523?

3 Likes

You’re looking for the Absolute Value of float. Absolute returns the positive value.

Math with absolute, on paper, could look like:

|-5| = 5

|4| = 4

|-2.5| = 2.5

3 Likes

It’s the Asbolute() function.

7553-absolute.png

4 Likes

Aha, that should save me some iterations thanks!

Thanks! :slight_smile:

this one seems fast enough…

not sure if serious or not, but here is a way that seems fast

if (float <0){float*-1;}//makes sure result is postative

that was the solution to my problem, thank you!

So the absolute value drops the sign of the number essentially. This is exactly what I needed. Thank you :slight_smile: