What is the fastest way to detect then change a negative num

Post » Fri Jun 22, 2012 4:27 am

Is there a better/faster way to do this:

if PosZ < 0
PosZ = PosZ * -1
endif


if PosZ < 0
PosZ = (-PosZ) ;-- will that work?
endif

Is there some math function that can do this without an IF check?
User avatar
naome duncan
 
Posts: 3459
Joined: Tue Feb 06, 2007 12:36 am

Post » Thu Jun 21, 2012 6:50 pm

abs(PosZ)
User avatar
Jessica Colville
 
Posts: 3349
Joined: Wed Oct 18, 2006 6:53 pm

Post » Fri Jun 22, 2012 2:26 am

You could use the math.abs(number) function or multiply the number by itself and then make the square root (math.sqrt(number) ).

The problem is that calling a math function may (and surelly will) be way slower than using your method (besides you need to use a float for that functions so, you may need to make a cast first and then another one to make it an int again).
User avatar
Floor Punch
 
Posts: 3568
Joined: Tue May 29, 2007 7:18 am

Post » Thu Jun 21, 2012 4:14 pm

Your second option, using the unary operator, will work. I don't know if calling Math.Abs would be faster than a conditional statement, but that would work too. You'd still need to convert it to negative after calling that too.

Cipscis
User avatar
Jason Rice
 
Posts: 3445
Joined: Thu Aug 16, 2007 3:42 pm


Return to V - Skyrim