Page 1 of 1

Select the one of two variables with highest value

Posted: Wed Nov 27, 2019 2:15 pm
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!

Re: Select the one of two variables with highest value

Posted: Wed Nov 27, 2019 2:37 pm
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)

Re: Select the one of two variables with highest value

Posted: Wed Nov 27, 2019 3:36 pm
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)

Re: Select the one of two variables with highest value

Posted: Wed Nov 27, 2019 5:54 pm
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

Re: Select the one of two variables with highest value

Posted: Wed Nov 27, 2019 7:15 pm
by IdeasVacuum
When there are several values to compare, just put them in a List, then Sort the List. :D

Re: Select the one of two variables with highest value

Posted: Wed Nov 27, 2019 7:20 pm
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