## [Solved] How can I determine if integer is greater then Max Integer Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

### [Solved] How can I determine if integer is greater then Max Integer

How Can I determine if a var is greater than the max integer value of 9223372036854775807
or less then the min integer value of -9223372036854775808

Code: Select all

var := "9223372036854775807" . "1"
MsgBox % var > 9223372036854775807
; result is false
I am working on a script that will do a procedure if the value of var is greater then 9223372036854775807 but I need help on how to check.

Any Ideas?
Last edited by AmourSpirit on 13 Mar 2017, 22:22, edited 1 time in total.
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
lexikos
Posts: 7085
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

### Re: How can I determine if integer is greater then Max Integer

You can't perform a numeric comparison on a value greater than the maximum. That's what makes it the maximum.

One way to check if a number is valid and in range is to attempt a round-trip conversion (see if you can convert it to integer and back to the same string).

Code: Select all

var := "92233720368547758071"
MsgBox % format("x{:i}", var) = "x" var
However, it depends on the original number being in a particular format (decimal and with no leading 0 or whitespace, in this case).

I think there have been scripts written for dealing with "big numbers" (performing math on them, etc.). You can try searching.
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

### Re: How can I determine if integer is greater then Max Integer  Topic is solved

Thanks lexikos. Seems to work fine.
I do not need to check for whitespace so this works fine for me.
Works for negative values as well.

Code: Select all

; value must be passed in as a string
; "9223372036854775808" will report false
; 9223372036854775808 will report true as without
; quotes AutoHotkey will convert to 9223372036854775807 (Integer Max)
;
; value can have leading signe of - or + but must be a string
IsValidIntRange(value)
{
strX := value . ""
; maximum length of Integer Max or Min with sign is 20 characters
If (StrLen(strX) > 20)
return false
; positive sign will be dropped by format
; negative sign will not be droped
x1 := format("x{:i}", value)

; check for a positive sign and remove it if exist
if (strX ~= "\+[0-9]+")
{
x2 := "x" . SubStr(strX, 2)
}
else
{
x2 := "x" . strX
}
if (x1 == x2)
return true
return false
}
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut