Absolute Val for Integers?
-
- Addict
- Posts: 1310
- Joined: Fri Aug 28, 2015 6:10 pm
- Location: Portugal
Absolute Val for Integers?
I notice that ABS(number) is only for floating point is there such a function for integers?
CD
CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Re: Absolute Val for Integers?
It works fine with integers. I don't know what the manual is talking about.
Code: Select all
n.i = -1234567890
Debug Abs(n) ; Shows 1234567890.0
-
- Addict
- Posts: 1310
- Joined: Fri Aug 28, 2015 6:10 pm
- Location: Portugal
Re: Absolute Val for Integers?
Code: Select all
y = 100
z = 110564567689766987
x = y - z
Debug Abs(x)
I got 110564567689766880.0
There does seem to be some loss! Plus it returns a float not an integer.
CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Re: Absolute Val for Integers?
Abs() isn't for Integers/Quads, you got problems with large integers.
You have to define your own procedure:
Code: Select all
Debug Abs(-100000000000000001)
Code: Select all
Procedure.q AbsI(Value.q)
If Value < 0
ProcedureReturn -Value
Else
ProcedureReturn Value
EndIf
EndProcedure
Debug AbsI(-100000000000000001)
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
-
- Addict
- Posts: 1310
- Joined: Fri Aug 28, 2015 6:10 pm
- Location: Portugal
Re: Absolute Val for Integers?
You can of course do this:
Wouls be nice to have ABS() for integers though.
Code: Select all
y = -457
If y < 0
y = y - (2 * y)
EndIf
Debug y
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Re: Absolute Val for Integers?
And why doesn't do Pb this by itself? Shouldn't be so complicated that Abs () automatically detects whether it is an integer or a float.STARGÅTE wrote: You have to define your own procedure:Code: Select all
Procedure.q AbsI(Value.q) If Value < 0 ProcedureReturn -Value Else ProcedureReturn Value EndIf EndProcedure Debug AbsI(-100000000000000001)
Last edited by Josh on Sun Jan 20, 2019 12:11 pm, edited 1 time in total.
sorry for my bad english
Re: Absolute Val for Integers?
The quickest way (edit : no good for negative... ) :
(added)
No better way than STARGÅTE in procedures and with native instructions.
Code: Select all
#iAbs = $7FFFFFFFFFFFFFFF
...
...
MyInteger & #iAbs
No better way than STARGÅTE in procedures and with native instructions.
Code: Select all
Procedure.Q qAbs(A.Q)
! Xor RAX, RAX
! Add RAX, [p.v_A] ; let's define A
! Ja l_cancelabs ; positive? yes : cancel
; else
! Xor RAX, 0xFFFFFFFFFFFFFFFF ; A ! (-1)
! Add RAX, 1 ; A + 1
cancelabs:
ProcedureReturn
EndProcedure
Last edited by Olliv on Sun Jan 20, 2019 1:26 pm, edited 3 times in total.
Re: Absolute Val for Integers?
Agreed, but at least you don't have to resort to the Windows API to fix this.Josh wrote:And why doesn't do Pb this by itself?

Re: Absolute Val for Integers?
PB's casting rules are a bit special sometimes. If you pass an expression with a division to Abs() then PB would have to decide on some rules if, or if not, an integer division is done and/or integer or float Abs() is to be called etc., at least i think it's something in that direction.Josh wrote:And why doesn't do Pb this by itself? Shouldn't be so complicated that Abs () automatically detects whether it is an integer or a float.

<edit>
But I think an AbsI() would be nice, along with a compiler error if an integer is passed to Abs().
Re: Absolute Val for Integers?
Does PB offer you the posibility to have one procedure parameter which can reperesent different types?Josh wrote:And why doesn't do Pb this by itself? Shouldn't be so complicated that Abs () automatically detects whether it is an integer or a float.
Even if you use a structure with Union, how can you decide if it is a float or an integer value?
It is not so easy as you think.
Re: Absolute Val for Integers?
I correct my super mistake above...
maybe write on wishlist to have a native AbsI().
maybe write on wishlist to have a native AbsI().
-
- Addict
- Posts: 4777
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Absolute Val for Integers?
There are already several threads here on the forum about Abs() for integers, including feature requests:Olliv wrote:maybe write on wishlist to have a native AbsI().
viewtopic.php?f=3&t=3170
viewtopic.php?f=3&t=14073
Re: Absolute Val for Integers?
@Little John
Yes ! Thank you, but these threads contain folkloric acid, plus ASM routine was absolutely false !!
Anyway, there are several reasons, not having added integer absolute function, because technically there are several ways to prepare the op, and branch (Jcc) using could be shared, what a native function prevents to do.
Yes ! Thank you, but these threads contain folkloric acid, plus ASM routine was absolutely false !!

Anyway, there are several reasons, not having added integer absolute function, because technically there are several ways to prepare the op, and branch (Jcc) using could be shared, what a native function prevents to do.
-
- Addict
- Posts: 4777
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Absolute Val for Integers?
Because you wrote
I just wanted to say that this has already been requested (at least) twice. That's all.Olliv wrote:maybe write on wishlist to have a native AbsI().
Re: Absolute Val for Integers?
You can use for Windows x64 the msvcrt.dll (PB use this DLL for many purposes):
Code: Select all
;Windows x64
If OpenLibrary(0, "msvcrt.dll")
Prototype.q Abs64(V1.q)
Prototype.l Abs32(V2.l)
Abs64.Abs64 = GetFunction(0, "_abs64")
Abs32.Abs32 = GetFunction(0, "labs")
CloseLibrary(0)
Else
;Error
EndIf
y1.q = 100
z1.q = 110564567689766987
x1.q = y1 - z1
Debug Abs64(x1)
y2.l = 100
z2.l = 110564567
x2.l = y2 - z2
Debug Abs32(x2)