Page 1 of 1
Abs Bug
Posted: Sun Dec 22, 2013 10:39 pm
by User_Russian
Of help.
Returns the absolute value. The return-value is always positive.
Why a negative result?
This is a bug or function Abs() or inaccurate documentation.
Re: Abs Bug
Posted: Sun Dec 22, 2013 10:45 pm
by STARGĂ…TE
The number from which to get the absolute value. This function will only work correctly with float numbers. With integers, it will fail if the integer is too large (due to a loss of precision).
Re: Abs Bug
Posted: Sun Dec 22, 2013 10:46 pm
by ts-soft
You have to use a float or double for result, as documented:
Re: Abs Bug
Posted: Mon Dec 23, 2013 12:30 am
by davido
You could use a Macro. I use the following because it is faster than ABS():
Code: Select all
Macro iABS(i,ans)
If i < 0
ans = -i
Else
ans = i
EndIf
EndMacro
EnableExplicit
Define c.i = -1234, M.i, p.i, dt = ElapsedMilliseconds()
For M = 1 To 10000000
iABS(c,p)
Next M
MessageRequester("Result: " + Str(p),"In: " + Str(ElapsedMilliseconds() - dt))
Re: Abs Bug
Posted: Mon Dec 23, 2013 12:36 am
by freak
The value is negative because it is too large to fit into a long. this has nothing to do with the ABS function.
Not a bug.
Re: Abs Bug
Posted: Mon Dec 23, 2013 7:59 am
by User_Russian
Strange logic!
This function is most needed for integer numbers than for floating numbers. Why not a similar function for integer variables?
Maybe you should reflect on adding unsigned variables?
Re: Abs Bug
Posted: Mon Dec 23, 2013 8:18 pm
by BasicallyPure
It does work for integers but the hex value must be within the range of the variable type you are using.
The number you are using, $80000000, is larger than the maximum positive value for a long.
When you use $80000000 you set bit 32 to a one which is the sign bit.
$80000000 is a negative number.
Long | .l | 4 bytes | -2147483648 to +2147483647
Code: Select all
z.l = $7FFFFFFF
Debug z
z = $80000000
Debug z
BP
Re: Abs Bug
Posted: Sun Apr 17, 2016 9:15 pm
by Sicro
Code: Select all
; min. Quad: -9223372036854775808
Define.q Value = Abs(-77777777777777777)
Debug Value ; 77777777777777776
Code: Select all
Define.l Value
; min. Long: -2147483648
Value = Abs(-2147483648)
Debug Value ; -2147483648 => Because Overflow
Debug Bin(Value, #PB_Long) ; 10000000000000000000000000000000
; min. Long: -2147483648
Value = Abs(-2147483647)
Debug Value ; 2147483647
Debug Bin(Value, #PB_Long) ; 1111111111111111111111111111111
Better use the command only with float variables, as documented.