Sign in to follow this  
Followers 0
malu05

How to find the Number Closest to $X?

9 posts in this topic

I tried looking through the help file but couldnt find the command.

How is it that you compare some numbers and see wich of them that are closest to $x?

Like in a array or so?


[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Share this post


Link to post
Share on other sites



go through the array and do

$x - element value

then use

Abs ( expression )

store the element number if smaller than previous element or continue if larger. hope that made sense

Share this post


Link to post
Share on other sites

I would do this for you but iam feeling lazy. :whistle:

Share this post


Link to post
Share on other sites

go through the array and do

$x - element value

then use

Abs ( expression )

store the element number if smaller than previous element or continue if larger. hope that made sense

Hmm.. what would Abs help here?

isnt there a specified command for this?


[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This is a quickie that returns "1" if the first one is closer, "2" id the second is closer, and "0"if they are equal distance in comparison to the third number

This example will return a 1 for example

$msg = _Compare(100, 200, 0)
MsgBox(0,"", $msg)

;----------
Func _Compare($Number1, $Number2, $NumToComp)
    Local $1 = Abs($NumToComp - $Number1)
    Local $2 = Abs($NumToComp - $Number2)
    Select
        Case $1 = $2
            $value = 0
        Case $1 < $2
            $value = 1
        Case $1 > $2
            $Value = 2
    EndSelect
    Return $Value
EndFunc
Edited by Paulie

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I took the time to write it for you :whistle:

both functions _ArrayMinDiff and _ArrayMaxDiff return an array with 2 values

elment

[0] gives the index at which the min difference or max difference was found and

[1] gives the min or max difference

$Array = StringSplit("4,2,6,8,12,5",",")
$min=_ArrayMinDiff($Array,6,1)
$max=_ArrayMaxDiff($Array,6,1)
ConsoleWrite('min:'&$min[0]&@CRLF&$min[1]&@CRLF&"max:"&$max[0]&@CRLF&$max[1]&@CRLF)

Func _ArrayMinDiff($F_Array,$F_Value,$i_Base=0)

    If Not IsArray($F_Array) Then
        SetError(1)
        Return ""
    EndIf
    
    Local $ArrayMinDiff[2],$i,$i_DiffValue
    $ArrayMinDiff[0]=$i_Base
    Local $i_Upper = UBound($F_Array)-1
    
    For $i= $i_Base To $i_Upper
        
        $i_DiffValue=Abs(Number($F_Array[$i])-$F_Value) 
        if $i_DiffValue<Abs(Number($F_Array[$ArrayMinDiff[0]])-$F_Value) then 
            $ArrayMinDiff[0]=$i
            $ArrayMinDiff[1]=$i_DiffValue
        EndIf
    Next

    SetError(0)
    Return $ArrayMinDiff
EndFunc

Func _ArrayMaxDiff($F_Array,$F_Value,$i_Base=0)

    If Not IsArray($F_Array) Then
        SetError(1)
        Return ""
    EndIf
    
    Local $ArrayMaxDiff[2],$i,$i_DiffValue
    $ArrayMaxDiff[0]=$i_Base
    Local $i_Upper = UBound($F_Array)-1
    
    For $i= $i_Base To $i_Upper
    
        $i_DiffValue=Abs(Number($F_Array[$i])-$F_Value) 
        if $i_DiffValue>Abs(Number($F_Array[$ArrayMaxDiff[0]])-$F_Value) then 
            $ArrayMaxDiff[0]=$i
            $ArrayMaxDiff[1]=$i_DiffValue
        EndIf

    Next

    SetError(0)
    Return $ArrayMaxDiff
EndFunc
Edited by Cue

Share this post


Link to post
Share on other sites

Hmm... just one question what exactly is alpha Numeric

Share this post


Link to post
Share on other sites

I took the time to write it for you :whistle:

both functions _ArrayMinDiff and _ArrayMaxDiff return an array with 2 values

elment

[0] gives the index at which the min difference or max difference was found and

[1] gives the min or max difference

$Array = StringSplit("4,2,6,8,12,5",",")
$min=_ArrayMinDiff($Array,6,1)
$max=_ArrayMaxDiff($Array,6,1)
ConsoleWrite('min:'&$min[0]&@CRLF&$min[1]&@CRLF&"max:"&$max[0]&@CRLF&$max[1]&@CRLF)

Func _ArrayMinDiff($F_Array,$F_Value,$i_Base=0)

    If Not IsArray($F_Array) Then
        SetError(1)
        Return ""
    EndIf
    
    Local $ArrayMinDiff[2],$i,$i_DiffValue
    $ArrayMinDiff[0]=$i_Base
    Local $i_Upper = UBound($F_Array)-1
    
    For $i= $i_Base To $i_Upper
        
        $i_DiffValue=Abs(Number($F_Array[$i])-$F_Value) 
        if $i_DiffValue<Abs(Number($F_Array[$ArrayMinDiff[0]])-$F_Value) then 
            $ArrayMinDiff[0]=$i
            $ArrayMinDiff[1]=$i_DiffValue
        EndIf
    Next

    SetError(0)
    Return $ArrayMinDiff
EndFunc

Func _ArrayMaxDiff($F_Array,$F_Value,$i_Base=0)

    If Not IsArray($F_Array) Then
        SetError(1)
        Return ""
    EndIf
    
    Local $ArrayMaxDiff[2],$i,$i_DiffValue
    $ArrayMaxDiff[0]=$i_Base
    Local $i_Upper = UBound($F_Array)-1
    
    For $i= $i_Base To $i_Upper
    
        $i_DiffValue=Abs(Number($F_Array[$i])-$F_Value) 
        if $i_DiffValue>Abs(Number($F_Array[$ArrayMaxDiff[0]])-$F_Value) then 
            $ArrayMaxDiff[0]=$i
            $ArrayMaxDiff[1]=$i_DiffValue
        EndIf

    Next

    SetError(0)
    Return $ArrayMaxDiff
EndFunc

Ohhh, you didnt really need to do all that for me, since it was what i tried to avoid.

Im kinda a "tiny code hugger".


[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Share this post


Link to post
Share on other sites

Your welcome. it may come in handy for me in the future aswell :whistle:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0