Jump to content

range


Recommended Posts

Try this.

Local $x1 = 0, $y1 = 0
Local $x2 = 9, $y2 = 11
Local $iRange = 10

If (($x1 > $x2 - $iRange) And ($x1 < $x2 + $iRange)) And _
        (($y1 > $y2 - $iRange) And ($y1 < $y2 + $iRange)) Then
    MsgBox(0, "Test 1", "$x1, $y1 is within the range of $x2, $y2", 2)
Else
    MsgBox(0, "Test 1", "Out of range.", 2)
EndIf

;== OR using  WithinRange() function =======================


If WithinRange($x1, $x2, $iRange) And WithinRange($y1, $y2, $iRange) Then
    MsgBox(0, "Test 2", "$x1, $y1 is within the range of $x2, $y2", 2)
Else
    MsgBox(0, "Test 2", "Out of range.", 2)
EndIf


Func  WithinRange($a, $b, $range)
If (($a > $b - $range) And ($a < $b + $range)) Then
        return True
Else
    return False
EndIf
EndFunc

@JoHanatCent

If $x1 < $x2 + $range Or $x1 > $x2 - $range And $y1 < $y2 + $range Or $y1 > $y2 - $range Then

will always be true for $range values > zero.

Edited by Malkey
Link to comment
Share on other sites

i could be mistaken, but doesn't this work?

if $min <= $var <= $max then

endif

Link to comment
Share on other sites

  • Moderators

CodyBarrett,

but doesn't this work?

No - it is just luck that it seems to work in this case. :P

Explanation:

- 1. Remember AutoIt evaluates expressions of the same precedence in left to right order.

- 2. Remember some operators (like >= and <=) return True/False.

- 3. True is interpreted as 1 and False as 0.

So let us look at the operations AutoIt is carrying out:

$min = 3
$var = 3
$max = 3

; Your suggestion
If $min <= $var <= $max Then
    ConsoleWrite("Yes 1" & @CRLF)
Else
    ConsoleWrite("No 1" & @CRLF)
EndIf

; Which is actually processed like this
If ($min <= $var) <= $max Then
    ConsoleWrite("Yes 2" & @CRLF)
Else
    ConsoleWrite("No 2" & @CRLF)
EndIf
;   (3 <= 3) <= 3
;   True <= 3
;   1 <= 3
;   True

; But does not work if processed like this
If $min <= ($var <= $max) Then
    ConsoleWrite("Yes 3" & @CRLF)
Else
    ConsoleWrite("No 3" & @CRLF)
EndIf
;   3 <= (3 <= 3)
;   3 <= True
;   3 <= 1
;   False

; And your original fails if you reverse the order of the comparisons!
If $max >= $var >= $min Then
    ConsoleWrite("Yes 4" & @CRLF)
Else
    ConsoleWrite("No 4" & @CRLF)
EndIf
;   (3 >= 3) >= 3
;   True >= 3
;   1 >= 3
;   False

Remember the warning in the Help file:

"False / True: These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable."

I hope that is all clear. :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

CodyBarrett,

No - it is just luck that it seems to work in this case. :shifty:

Explanation:

- 1. Remember AutoIt evaluates expressions of the same precedence in left to right order.

- 2. Remember some operators (like >= and <=) return True/False.

- 3. True is interpreted as 1 and False as 0.

So let us look at the operations AutoIt is carrying out:

$min = 3
$var = 3
$max = 3

; Your suggestion
If $min <= $var <= $max Then
    ConsoleWrite("Yes 1" & @CRLF)
Else
    ConsoleWrite("No 1" & @CRLF)
EndIf

; Which is actually processed like this
If ($min <= $var) <= $max Then
    ConsoleWrite("Yes 2" & @CRLF)
Else
    ConsoleWrite("No 2" & @CRLF)
EndIf
;   (3 <= 3) <= 3
;   True <= 3
;   1 <= 3
;   True

; But does not work if processed like this
If $min <= ($var <= $max) Then
    ConsoleWrite("Yes 3" & @CRLF)
Else
    ConsoleWrite("No 3" & @CRLF)
EndIf
;   3 <= (3 <= 3)
;   3 <= True
;   3 <= 1
;   False

; And your original fails if you reverse the order of the comparisons!
If $max >= $var >= $min Then
    ConsoleWrite("Yes 4" & @CRLF)
Else
    ConsoleWrite("No 4" & @CRLF)
EndIf
;   (3 >= 3) >= 3
;   True >= 3
;   1 >= 3
;   False

Remember the warning in the Help file:

"False / True: These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable."

I hope that is all clear. :x

M23

oh!

okay... i thought it would work because (by luck now you explained it) it has worked in several functions of mine. :P thanks for explaining why it wouldn't.

Link to comment
Share on other sites

  • Moderators

CodyBarrett,

i thought it would work

Well, of course, it does work in some cases where the values are suitable - but it is best not to rely on it in more general coding for the reasons I explained above. :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...