Newbie question Difference between 2 numbers

Just starting out? Need help? Post your questions and find answers here.
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Newbie question Difference between 2 numbers

Post by PeterGams »

How can I find the difference between to numbers?
I need to make sure there is no greater difference then 7 between two numbers.
Sometimes the first number will be less then the first. eg a=1 b=9.
Other times the reverse. eg a=9 b =1.
I don't want the result to be a negative one.

Sorry for the newbie question but I keep getting negative numbers.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Newbie question Difference between 2 numbers

Post by PB »

Use the Abs() command to prevent negatives:

Code: Select all

a=1 : b=9 : Debug Abs(a-b)
a=9 : b=1 : Debug Abs(a-b)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Hi,

Code: Select all

Procedure Greater7 (a,b)
   If (a > b)
      If (a - b) > 7
         ProcedureReturn #True
      EndIf
   Else
      If (b - a) > 7
         ProcedureReturn #True
      EndIf
   EndIf
   ProcedureReturn #False
EndProcedure

If Greater7 (10,17)
   Debug "Alert: Greater difference than 7."
EndIf

If Greater7 (10,18)
   Debug "Alert: Greater difference than 7."
EndIf

If Greater7 (2,10)
   Debug "Alert: Greater difference than 7."
EndIf
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Thanks Heaps Guys

Post by PeterGams »

The ABS seems to woork great.
Best of all it is easier than I thought.

Thanks Again. :D
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: Thanks Heaps Guys

Post by gnozal »

PeterGams wrote:The ABS seems to woork great.
Best of all it is easier than I thought.
Please note that Abs() should only be used for floats, not integers !
From the manual : "This function works correctly only with float numbers. With integer it will fail if the integer is too big (loss of precision)."
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

@ PeterGams:
Here is a very simple procedure:

Code: Select all

Procedure Difference(no1, no2, maxDifference=7)
	If no1 < no2
		Swap no1, no2
	EndIf
	If no1-no2 > maxDifference
		ProcedureReturn #True
	EndIf
EndProcedure

Debug Difference(4, 9)
Debug Difference(9, 1)
Debug Difference(7, 5, 1)
You can easily define the 'maxDifference' on-the-fly (leave the 3rd parameter blank if you want it to be 7).

With this code I want to show you some commands that might be useful for you. You can increase the performance and readability of you code, if you compare my code to Hroudtwolf's one. So don't be afraid when being confronted with optional parameters or PureBasic keywords like Swap (quickly swaps the content of two elements).
Happy coding! :D
PB 4.30

Code: Select all

onErrorGoto(?Fred)
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

just for fun 8)

Code: Select all

Macro diff(a,b)
  (  ((a)-(b)) + (0 Or (b)>(a))*-2*((a)-(b))  )
EndMacro

Debug diff(8,8)  ; 0
Debug diff( 4,6) ; 2
Debug diff(13,9) ; 4
Debug diff(-3,2) ; 5
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

Post Reply