Page 1 of 5

Absolute Val for Integers?

Posted: Sun Jan 20, 2019 11:23 am
by collectordave
I notice that ABS(number) is only for floating point is there such a function for integers?

CD

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 11:28 am
by Dude
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

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 11:39 am
by collectordave

Code: Select all

y = 100
z = 110564567689766987

  
x = y - z


Debug Abs(x)
I expected 110564567689766887
I got 110564567689766880.0


There does seem to be some loss! Plus it returns a float not an integer.

CD

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 11:43 am
by STARGÅTE
Abs() isn't for Integers/Quads, you got problems with large integers.

Code: Select all

Debug Abs(-100000000000000001)
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)

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 11:44 am
by collectordave
You can of course do this:

Code: Select all


y = -457

If y < 0
  y = y - (2 * y)
EndIf

Debug y

Wouls be nice to have ABS() for integers though.

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 12:08 pm
by Josh
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)
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.

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 12:08 pm
by Olliv
The quickest way (edit : no good for negative... ) :

Code: Select all

#iAbs = $7FFFFFFFFFFFFFFF

...
...


MyInteger & #iAbs
(added)
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

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 12:19 pm
by RobertSF
Josh wrote:And why doesn't do Pb this by itself?
Agreed, but at least you don't have to resort to the Windows API to fix this. :wink:

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 12:42 pm
by #NULL
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.
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. :)

<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?

Posted: Sun Jan 20, 2019 1:21 pm
by infratec
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.
Does PB offer you the posibility to have one procedure parameter which can reperesent different types?
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?

Posted: Sun Jan 20, 2019 1:23 pm
by Olliv
I correct my super mistake above...

maybe write on wishlist to have a native AbsI().

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 2:05 pm
by Little John
Olliv wrote:maybe write on wishlist to have a native AbsI().
There are already several threads here on the forum about Abs() for integers, including feature requests:
viewtopic.php?f=3&t=3170
viewtopic.php?f=3&t=14073

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 8:58 pm
by Olliv
@Little John

Yes ! Thank you, but these threads contain folkloric acid, plus ASM routine was absolutely false !! :D

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.

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 9:35 pm
by Little John
Because you wrote
Olliv wrote:maybe write on wishlist to have a native AbsI().
I just wanted to say that this has already been requested (at least) twice. That's all.

Re: Absolute Val for Integers?

Posted: Sun Jan 20, 2019 10:33 pm
by Helle
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)