Simple Difference

Share your advanced PureBasic knowledge/code with the community.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Simple Difference

Post 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) 
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Why not just use abs?
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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.
%1>>1+1*1/1-1!1|1&1<<$1=1
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

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