Abs value
Parameters
| value - any integer or float expression |
Description
|
Returns the value with any minus sign removed. Abs is a prefix operator rather than a function call, so Abs x and Abs(x) both work - the brackets are just grouping. Wrap anything more complicated than a single variable in brackets to be sure the whole expression is covered: Abs( a-b ), not Abs a-b. The result keeps the type it was given. Abs applied to an integer is an integer, Abs applied to a float is a float, so Abs(-3.5) is 3.5 and nothing is rounded away. Its everyday use is measuring how far apart two things are without caring which is bigger: Abs( player_x-enemy_x ) for a quick axis-aligned proximity test, or Abs( target_angle-current_angle ) to see how much a turret still has to turn. It is also the standard way to compare floats safely - instead of testing a# = b#, test whether Abs( a#-b# ) is smaller than a small tolerance. If what you actually want is the whole-number part of a float, that is Int or Floor, not Abs. See also: Sgn, Int, Floor, Sqr. |
Example
; Abs Example ; ----------- ; Abs strips the sign from a number: negative values become positive, ; positive values and zero are unchanged. Print "Abs(7) = "+Abs(7) Print "Abs(-7) = "+Abs(-7) Print "Abs(-3.5) = "+Abs(-3.5) Print "Abs(0) = "+Abs(0) Print "" ; Handy for distances: how far apart two racers are on a track, ; whichever one is in front racer1=120 racer2=200 Print "Racer positions: "+racer1+" and "+racer2 Print "Gap between them: Abs("+racer1+"-"+racer2+") = "+Abs(racer1-racer2) Print "" Print "Press any key to close the example" WaitKey End
Index