Select the one of two variables with highest value

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Select the one of two variables with highest value

Post by marcoagpinto »

Hello!

I am calculating the distance between two points with the code:

Code: Select all

  If distance_metric=1 
    
    distance_xx=Abs(x1-Int(x2))
    distance_yy=Abs(y1-Int(y2))
    distance=distance_xx
    If distance_yy>distance_xx : distance=distance_yy : EndIf
    
    ; Return the distance
    ProcedureReturn distance    
    
  EndIf

Code: Select all

If distance_yy>distance_xx : distance=distance_yy : EndIf
Is there some kind of PB command for:
distance=maximumvalue(distance_xx,distance_yy)
?

Thanks!
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Select the one of two variables with highest value

Post by kenmo »

No, there is no included function for Max / Min (which has always been strange because it includes other basic math functions).

Code: Select all

Procedure.d Max(x.d, y.d)
  If (x > y)
    ProcedureReturn (x)
  EndIf
  ProcedureReturn (y)
EndProcedure

Procedure.d Max3(x.d, y.d, z.d)
  ProcedureReturn (Max(Max(x, y), z))
EndProcedure

Debug Max(3.6, 4.5)
Debug Max3(3.6, 4.5, 4.7)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Select the one of two variables with highest value

Post by wilbert »

kenmo is right, there is not such function.

Here's an alternative procedure

Code: Select all

Procedure.d Min(n1.d, n2.d)
  !movsd xmm0, [p.v_n1]
  !minsd xmm0, [p.v_n2]
  !movsd [p.v_n1], xmm0
  ProcedureReturn n1
EndProcedure

Procedure.d Min3(n1.d, n2.d, n3.d)
  !movsd xmm0, [p.v_n1]
  !minsd xmm0, [p.v_n2]
  !minsd xmm0, [p.v_n3]
  !movsd [p.v_n1], xmm0
  ProcedureReturn n1
EndProcedure

Procedure.d Max(n1.d, n2.d)
  !movsd xmm0, [p.v_n1]
  !maxsd xmm0, [p.v_n2]
  !movsd [p.v_n1], xmm0
  ProcedureReturn n1
EndProcedure

Procedure.d Max3(n1.d, n2.d, n3.d)
  !movsd xmm0, [p.v_n1]
  !maxsd xmm0, [p.v_n2]
  !maxsd xmm0, [p.v_n3]
  !movsd [p.v_n1], xmm0
  ProcedureReturn n1
EndProcedure

Debug Min(4, 5)
Debug Max(3, 5)

Debug Min3(5, 3, 7)
Debug Max3(3, 7, 2)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Select the one of two variables with highest value

Post by skywalk »

I use a Macro to support any data type except String.

Code: Select all

; Min() and Max() do not support Strings.
Macro Min(x, y)
  (Bool((x) <= (y)) * (x) + Bool((y) < (x)) * (y))
EndMacro
Macro Max(x, y)
  (Bool((x) >= (y)) * (x) + Bool((y) > (x)) * (y))
EndMacro
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Select the one of two variables with highest value

Post by IdeasVacuum »

When there are several values to compare, just put them in a List, then Sort the List. :D
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Select the one of two variables with highest value

Post by marcoagpinto »

Thank you, guys,

I have created two functions for my code:

Code: Select all

  
  ; Returns the maximum of two values, uses natural numbers
  ;
  ; V1.0 - 27/NOV/2019
  ;
  Procedure Max(MARCOAGPINTO_value1,MARCOAGPINTO_value2)
    
    If MARCOAGPINTO_value1>MARCOAGPINTO_value2 : ProcedureReturn MARCOAGPINTO_value1 : EndIf
    
    ProcedureReturn MARCOAGPINTO_value2
    
  EndProcedure
  
  
  
  
  ; Returns the minimum of two values, uses natural numbers
  ;
  ; V1.0 - 27/NOV/2019
  ;
  Procedure Min(MARCOAGPINTO_value1,MARCOAGPINTO_value2)
    
    If MARCOAGPINTO_value1<MARCOAGPINTO_value2 : ProcedureReturn MARCOAGPINTO_value1 : EndIf
    
    ProcedureReturn MARCOAGPINTO_value2
    
  EndProcedure
Post Reply