Abs Bug

Just starting out? Need help? Post your questions and find answers here.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Abs Bug

Post by User_Russian »

Of help.
Returns the absolute value. The return-value is always positive.

Why a negative result?

Code: Select all

z.l=Abs($80000000)
Debug z
This is a bug or function Abs() or inaccurate documentation.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Abs Bug

Post 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).
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Abs Bug

Post by ts-soft »

You have to use a float or double for result, as documented:

Code: Select all

z.f=Abs($80000000)
Debug z 
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Abs Bug

Post 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))
DE AA EB
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Abs Bug

Post 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.
quidquid Latine dictum sit altum videtur
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Abs Bug

Post 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?
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Abs Bug

Post 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
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Abs Bug

Post 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.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply