Page 1 of 1

Simple Difference

Posted: Wed Dec 19, 2007 10:12 pm
by Baldrick
A very simple little procedure to give you a result using the negation operator when a number is below 0.
Saves the need to search left to right or right to left depending which number is bigger so you can get on with other more important things...

Code: Select all

Procedure.l Difference(a.l,b.l) 
   Result.l=a-b ;e.g a=1,b=2 Result=-1
    If Result<0 
     ProcedureReturn -Result ;e.g. --1 = +1
    EndIf 
 ProcedureReturn Result 
EndProcedure 

; Test Difference() 
Debug Difference(1,2) 

Posted: Wed Dec 19, 2007 11:48 pm
by Matt
Why not just use abs?

Posted: Thu Dec 20, 2007 9:49 am
by Froggerprogger
As Matt intended: You wrote a procedure that should be called AbsL(), because it does the same as Abs() but on integers instead of floats.

Posted: Thu Dec 20, 2007 10:25 am
by Baldrick
Matt is quite correct. I wrote this little bit to use in a project for creating Master keyed lock systems. 1 of the requirements is a rule to stop physical overrun between pin chambers when 1 cut is very low & the other very high & I am using this little proc to delete any keys that fail this test.
Abs() will do this just as simply. It just wasn't coming to mind when I needed it...... :roll: