Absolute Val for Integers?

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Absolute Val for Integers?

Post by collectordave »

I notice that ABS(number) is only for floating point is there such a function for integers?

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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Absolute Val for Integers?

Post 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
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Absolute Val for Integers?

Post 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
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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Absolute Val for Integers?

Post 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)
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
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Absolute Val for Integers?

Post 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.
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.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Absolute Val for Integers?

Post 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.
Last edited by Josh on Sun Jan 20, 2019 12:11 pm, edited 1 time in total.
sorry for my bad english
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Absolute Val for Integers?

Post 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
Last edited by Olliv on Sun Jan 20, 2019 1:26 pm, edited 3 times in total.
RobertSF
User
User
Posts: 61
Joined: Thu May 03, 2018 4:24 pm

Re: Absolute Val for Integers?

Post 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:
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Absolute Val for Integers?

Post 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().
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Absolute Val for Integers?

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Absolute Val for Integers?

Post by Olliv »

I correct my super mistake above...

maybe write on wishlist to have a native AbsI().
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Absolute Val for Integers?

Post 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
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Absolute Val for Integers?

Post 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.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Absolute Val for Integers?

Post 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.
Helle
Enthusiast
Enthusiast
Posts: 178
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Re: Absolute Val for Integers?

Post 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)
Post Reply