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.
Newbie question Difference between 2 numbers
Re: Newbie question Difference between 2 numbers
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.
"PureBasic won't be object oriented, period" - Fred.
- Hroudtwolf
- Addict
- Posts: 803
- Joined: Sat Feb 12, 2005 3:35 am
- Location: Germany(Hessen)
- Contact:
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
Thanks Heaps Guys
The ABS seems to woork great.
Best of all it is easier than I thought.
Thanks Again.
Best of all it is easier than I thought.
Thanks Again.

-
- PureBasic Expert
- Posts: 4229
- Joined: Sat Apr 26, 2003 8:27 am
- Location: Strasbourg / France
- Contact:
Re: Thanks Heaps Guys
Please note that Abs() should only be used for floats, not integers !PeterGams wrote:The ABS seems to woork great.
Best of all it is easier than I thought.
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).
@ PeterGams:
Here is a very simple procedure:
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!
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)
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!

PB 4.30
Code: Select all
onErrorGoto(?Fred)
just for fun

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
This sort of look familiar Pete?
http://www.purebasic.fr/english/viewtop ... difference
http://www.purebasic.fr/english/viewtop ... difference