check if number is within range problem

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nikiwonno
Posts: 8
Joined: 29 Jun 2022, 10:44

check if number is within range problem

Post by Nikiwonno » 15 Aug 2022, 14:51

The following if statement goes through even though 3.3456 is bigger than 3, it should have been false, can any kind soul explain why the check is not working correctly?

Code: Select all


r::

if((3.3456 between 1 and 3)){
debug := 3.456 " between " 1 " and " 3 
			
		Send {Enter}
		Send %debug%
		}

gregster
Posts: 9056
Joined: 30 Sep 2013, 06:48

Re: check if number is within range problem

Post by gregster » 15 Aug 2022, 14:54

If with parentheses uses expression syntax, but the If...between docs say:
https://www.autohotkey.com/docs/commands/IfBetween.htm#Remarks wrote:The "between" operator is not supported in expressions. Instead, use If statements such as if (Var >= LowerBound and Var <= UpperBound) to simulate the behavior of this operator.
Alternatively, follow the If...between syntax shown in the examples in the docs. (Hint: the first parameter needs to be a variable name - a literal number wouldn't work, afaics (at least not as you'd probably expect )).
Using expression syntax would be more future-proof, though, if we look at AHK v2, and is generally recommended.

PS: One-True-Brace style is also not supported by If... between. So you can't have an opening brace ({) on the same line. No problems with this, if you use if (expression) syntax.

edited

User avatar
Chunjee
Posts: 1433
Joined: 18 Apr 2014, 19:05
Contact:

Re: check if number is within range problem

Post by Chunjee » 17 Aug 2022, 11:52

@Nikiwonno

https://biga-ahk.github.io/biga.ahk/#/?id=inrange may help

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

A.inRange(3.3456, 1, 3)
; => false

Post Reply

Return to “Ask for Help (v1)”