checking for maximum allowable integer value Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
joefiesta
Posts: 394
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

checking for maximum allowable integer value

Post by joefiesta » 24 Jan 2021, 14:58

I want to check if a value (input by a user) is a positive integer and within the allowable AHK range of integers, how do I do it? The following does not work.

Code: Select all

n =  1234567890123456789012345
;   n = -1234567890123456789012345
;   n =  9423456789012345678
;   n =  9123372036854775807
maxnum := 2**63 - 1     ; 9223372036854775807
   if n is not integer
      a := Error(4,"001E", n " is not an integer.")
   if (n < 0)
      a := Error(4,"002E","Invalid number:" n ". value is negative.")
   if (n = 0)
      a := Error(4,"003E","Invalid number:" n ". value is 0.")
   if (n > maxnum)
      a := Error(4,"004E","Unallowable integer " n ".  Value exceeds 2**63 - 1 ("maxnum ")")
   a := Error(0,"000I", "Acceptable positive integer < 2**63")

error(errorrc=0,errnum="666E",errtext="",log=1,msgbox=1)
{
;  if log
;     Write_log(A_scriptname,errnum,errtext)
   if msgbox
      Msgbox,,%A_ScriptName%, %errorrc% %errnum% %errtext%
   exitapp, %errorrc%
}

I can do as follows, but this is VERY ugly. Can anyone come up with a more elegant solution?

Code: Select all

n =  1234567890123456789012345
;n = -1234567890123456789012345
;n =  9423456789012345678
;n =  9123372036854775807
maxnum := 2**63 - 1     ; 9223372036854775807
   if n is not integer
      a := Error(4,"001E", n " is not an integer.")
   if (n < 0)
      a := Error(4,"002E","Invalid number:" n ". value is negative.")
   if (n = 0)
      a := Error(4,"003E","Invalid number:" n ". value is 0.")
   if (strlen(n) > 19)
      a := Error(4,"004E","Unallowable integer " n ".  Value exceeds 2**63 - 1 ("maxnum ")")
   if (strlen(n) = 19 and substr(n,1,1) = 9 and substr(n,2) > substr(maxnum,2))
      a := Error(4,"005E","Unallowable integer " n ".  Value exceeds 2**63 - 1 ("maxnum ")")
   a := Error(0,"000I", "Acceptable positive integer < 2**63")
   exitapp

error(errorrc=0,errnum="666E",errtext="",log=1,msgbox=1)
{
;  if log
;     Write_log(A_scriptname,errnum,errtext)
   if msgbox
      Msgbox,,%A_ScriptName%, %errorrc% %errnum% %errtext%
   exitapp, %errorrc%
}
dobbelina
Posts: 39
Joined: 03 Sep 2019, 02:45
GitHub: dobbelina

Re: checking for maximum allowable integer value

Post by dobbelina » 24 Jan 2021, 18:19

Code: Select all

if (n >= maxnum) || if (n <= "0")
MsgBox, Not allowed range %n% 
else
MsgBox, Allowed range %n%
teadrinker
Posts: 2172
Joined: 29 Mar 2015, 09:41
Contact:

Re: checking for maximum allowable integer value

Post by teadrinker » 25 Jan 2021, 02:31

@dobbelina
Incorrect syntax, but it almost works! :)
There is a flaw: you can't include 9223372036854775807, although it's allowed.
dobbelina
Posts: 39
Joined: 03 Sep 2019, 02:45
GitHub: dobbelina

Re: checking for maximum allowable integer value

Post by dobbelina » 25 Jan 2021, 02:56

Aha, that's the max integer number :lol:
Then this wont work.....

Code: Select all

n = 50
maxnum = 50

if (n >= maxnum + 1) || if (n <= "0")
MsgBox, Not allowed
else
MsgBox, allowed
teadrinker
Posts: 2172
Joined: 29 Mar 2015, 09:41
Contact:

Re: checking for maximum allowable integer value

Post by teadrinker » 25 Jan 2021, 03:07

dobbelina wrote:

Code: Select all

if (n >= maxnum) || if (n <= "0")
More correct like this:
if (n >= maxnum || n <= 0)
dobbelina
Posts: 39
Joined: 03 Sep 2019, 02:45
GitHub: dobbelina

Re: checking for maximum allowable integer value

Post by dobbelina » 25 Jan 2021, 03:59

What about this:

Code: Select all

n = 9223372036854775806

maxnum = 9223372036854775807  
minnum = -9223372036854775808

if (n >= maxnum || n <= minnum){
MsgBox, Outside allowed range
return
}
if (n >= maxnum || n <= "0")
MsgBox, Not allowed
else
MsgBox, allowed
teadrinker
Posts: 2172
Joined: 29 Mar 2015, 09:41
Contact:

Re: checking for maximum allowable integer value

Post by teadrinker » 25 Jan 2021, 05:00

Still doesn't work with 9223372036854775807.
Rohwedder
Posts: 3952
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: checking for maximum allowable integer value

Post by Rohwedder » 25 Jan 2021, 05:02

Hallo,
try:

Code: Select all

n := 2**63-1 ;integer
; n := 2**63 ;not integer
n += 0
IF n is digit
	MsgBox, n is empty or a positive integer
Else
	MsgBox, n is anything else
teadrinker
Posts: 2172
Joined: 29 Mar 2015, 09:41
Contact:

Re: checking for maximum allowable integer value

Post by teadrinker » 25 Jan 2021, 05:22

Rohwedder wrote: n is empty or a positive integer
But you need to distinguish empty value and positive integer. :)
teadrinker
Posts: 2172
Joined: 29 Mar 2015, 09:41
Contact:

Re: checking for maximum allowable integer value

Post by teadrinker » 25 Jan 2021, 06:14

Yes, but IMO too hard.

Code: Select all

MsgBox, % IsValidIntRange("-9223372036854775808")

IsValidIntRange(value)
{
   strX := "x" . value . ""
   value += 0
   Return strX == "x" . value
}
teadrinker
Posts: 2172
Joined: 29 Mar 2015, 09:41
Contact:

Re: checking for maximum allowable integer value

Post by teadrinker » 25 Jan 2021, 06:23

Or like this:

Code: Select all

MsgBox, % IsValidIntRange("-9223372036854775808")

IsValidIntRange(value)
{
   Return "x" . value = "x" . (value + 0)
}
:)
joefiesta
Posts: 394
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: checking for maximum allowable integer value  Topic is solved

Post by joefiesta » 25 Jan 2021, 11:59

thanks for the tries, but Lexico's answer is the best. Thanks for pointing it out @dobbelina
Post Reply

Return to “Ask For Help”