Simple question on integers and math with AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ricky_JRG3
Posts: 2
Joined: 10 May 2020, 23:20

Simple question on integers and math with AHK

10 May 2020, 23:37

Code: Select all

^M::
x := 20
value := x/10
if(%value% = integer){
MsgBox, value is an integer
}else{
MsgBox, value is not an integer
}
So this is my testing code for integers, everytime I go to run the code i get an error message saying that %value% has an "Illegal character, "2.000000"... I thought it would send a message box saying it is an integer but I can't seem to figure out why it is doing this:/ Please help
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Simple question on integers and math with AHK

11 May 2020, 00:45

But note:
https://www.autohotkey.com/docs/Variables.htm#MulDiv wrote:True divide (/): Unlike EnvDiv, true division yields a floating point result even when both inputs are integers. For example, 3/2 yields 1.5 rather than 1, and 4/2 yields 2.0 rather than 2.
So, some number formatting might be required after using /, to get an integer.
Ricky_JRG3
Posts: 2
Joined: 10 May 2020, 23:20

Re: Simple question on integers and math with AHK

11 May 2020, 00:45

I've tried just using "is integer", but it replied the same thing
error.png
error.png (12.71 KiB) Viewed 3344 times
This is the error it displays^

And yes, in my more advanced code, i have a count variable that keeps track of what loop number it's on and i only want it to display that number if it's divisible by 100
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 04:08

[Deleted, as incorrect advice.]
Last edited by ahketype on 11 May 2020, 12:27, edited 1 time in total.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Simple question on integers and math with AHK

11 May 2020, 05:30

is type is not supported in expressions --> don't use parentheses around it.

The example should show the usage pretty good: https://www.autohotkey.com/docs/commands/IfIs.htm#ExBasic

Code: Select all

if var is float
    MsgBox, %var% is a floating point number.
else if var is integer
    MsgBox, %var% is an integer.
if var is time
    MsgBox, %var% is also a valid date-time.

Still, by using true division, that means /, you will create a float number (see quote above and check with msgbox), which would need to be re-formatted to be seen as an integer, afaik.

Or you need to find another method... something with Mod() ?

Code: Select all

if (mod(20, 7) != 0)
	msgbox % 20 / 7 " is not an integer."

if (mod(20, 5) = 0)
	msgbox % 20 // 5 " is an integer."
You could make a function out of it.
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Simple question on integers and math with AHK

11 May 2020, 05:33

example for calculations

Code: Select all

a:=5
b:=10
c:=(a+b)
msgbox,C=%c%

Code: Select all

;- integer=True if Var is non-empty and contains a purely numeric string (decimal or hexadecimal) without a decimal point. 
;- Leading and trailing spaces and tabs are allowed. The string may start with a plus or minus sign.

;SetFormat, float, 0.0
SetFormat, float, 0.2
x1:=15.25
x2:=floor(x1)
x3:=20
msgbox,X1=%x1%`nX2=%x2%`nX3=%x3%
if x1 is integer
  msgbox,X1=%x1% is integer
if x2 is integer
  msgbox,X2=%x2% is integer  ;- without a decimal point
if x3 is integer
  msgbox,X3=%x3% is integer  ;- without a decimal point
return
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 09:06

[Deleted, as incorrect advice.]
Last edited by ahketype on 11 May 2020, 12:27, edited 3 times in total.
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 09:23

[Deleted, as incorrect advice.]
Last edited by ahketype on 11 May 2020, 12:28, edited 1 time in total.
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Simple question on integers and math with AHK

11 May 2020, 10:31

Code: Select all

x := 20
value := x/10
problem , value see as 2.000000 instead of 2 , so it says it's not integer = without a decimal point

Code: Select all

a:=20
b:=10
c:=(a*b)  ;- 200
d:=(a/b)  ;- 2.000000    ( how can see = 2 ( ? ) ) , maybe use floor if 000000 after decimal point 
msgbox,C=%c%`nD=%d%
return

Code: Select all

x1:=315.55
;----------
if (x1 is integer)               ;- don't use bracket
	MsgBox,X1a=%x1% IS an integer (WRONG)
  else
	MsgBox,X1a=%x1% NOT an integer

if x1 is integer                 ;- correct
	MsgBox,X1b=%x1% IS an integer    
  else    
	MsgBox,X1b=%x1% NOT an integer (CORRECT)
return
Last edited by garry on 11 May 2020, 10:59, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Simple question on integers and math with AHK

11 May 2020, 10:39

@ahketype, (value is integer) is concatenation of three variables. As was mentioned, is is not supported in expressions.

Cheers.
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 10:47

[Deleted, as incorrect advice.]
Last edited by ahketype on 11 May 2020, 12:28, edited 1 time in total.
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 10:51

Helgef wrote:
11 May 2020, 10:39
@ahketype, (value is integer) is concatenation of three variables. As was mentioned, is is not supported in expressions.

Cheers.
Oh, drat, my bad. I've messed up this whole thread! Should I just delete my posts here, do you think?

Edited to add: I've deleted my earlier attempts - AHK keeps surprising me!

At the risk of putting my foot in it again, let me offer this, which I've tested a little more carefully and takes account of the above correction I've received:

Code: Select all

Loop
{
	x := A_Index
	value := x/10
	flor := x//10
	if (value = flor)
	MsgBox, %value% is an integer
}
until x=21
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Simple question on integers and math with AHK

11 May 2020, 13:36

;- integer=True if Var is non-empty and contains a purely numeric string (decimal or hexadecimal) without a decimal point.
;- Leading and trailing spaces and tabs are allowed. The string may start with a plus or minus sign.

If division , I see result >> 2.000000 ( I want see = 2 ) , so it's never integer ( because decimal point )
How can remove decimal point and zeros , so the variable 'd' is integer ?

Code: Select all

;- if division , I see result >> 2.000000  ( I want see = 2 )
a:=20,b:=10
c:=(a*b)  ;- 200
d:=(a/b)  ;- 2.000000    ( how I can see = 2 ( ? ) ) , maybe use floor if 000000 after decimal point 
stringsplit,k,d,`.
if (k2="000000")
  d:=k1   ;- 2  , so this is an integer
msgbox,C=%c%`nD=%d%


a:=20,b:=7
c:=(a*b)  ;- 140
d:=(a/b)  ;- 2.857143  << this NOT integer but the correct result
e:=(a//b) ;- 2         << this IS  integer but not correct result
msgbox,C=%c%`nD=%d%`nE=%e%
return
Last edited by garry on 11 May 2020, 15:30, edited 1 time in total.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Simple question on integers and math with AHK

11 May 2020, 13:50

ahketype wrote:
11 May 2020, 10:51
Oh, drat, my bad. I've messed up this whole thread! Should I just delete my posts here, do you think?
No, please don't delete/empty them in the future! That's against the forum rules.
Others might have reacted to your posts and now the context is broken.
People are here to learn - you are allowed to make mistakes, please allow others to learn from them... thank you!
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 16:54

gregster wrote:
11 May 2020, 13:50
ahketype wrote:
11 May 2020, 10:51
Oh, drat, my bad. I've messed up this whole thread! Should I just delete my posts here, do you think?
No, please don't delete/empty them in the future! That's against the forum rules.
Others might have reacted to your posts and now the context is broken.
People are here to learn - you are allowed to make mistakes, please allow others to learn from them... thank you!
OK, thanks. Too late! I didn't think to check the rules. Even so, I think the potential confusion my attempts might have caused readers were probably worse than any interruption of flow. All that's left is people quoting my mistakes and correcting them. And (hopefully) my correct solution. :morebeard:
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Simple question on integers and math with AHK

11 May 2020, 17:12

garry wrote:
11 May 2020, 13:36
;- integer=True if Var is non-empty and contains a purely numeric string (decimal or hexadecimal) without a decimal point.
;- Leading and trailing spaces and tabs are allowed. The string may start with a plus or minus sign.

If division , I see result >> 2.000000 ( I want see = 2 ) , so it's never integer ( because decimal point )
How can remove decimal point and zeros , so the variable 'd' is integer ?

Code: Select all

;- if division , I see result >> 2.000000  ( I want see = 2 )
a:=20,b:=10
c:=(a*b)  ;- 200
d:=(a/b)  ;- 2.000000    ( how I can see = 2 ( ? ) ) , maybe use floor if 000000 after decimal point 
stringsplit,k,d,`.
if (k2="000000")
  d:=k1   ;- 2  , so this is an integer
msgbox,C=%c%`nD=%d%


a:=20,b:=7
c:=(a*b)  ;- 140
d:=(a/b)  ;- 2.857143  << this NOT integer but the correct result
e:=(a//b) ;- 2         << this IS  integer but not correct result
msgbox,C=%c%`nD=%d%`nE=%e%
return
Yes. Therefore
if (d = e)
d is an integer. No need to split the string, the solution is to do a floor division as well as the true division required in the original post, and if they're equal, it's an integer.

Code: Select all

Loop
{
	x := A_Index
	value := x/7
	flor := x//7
	if (value = flor)
		MsgBox, %value% is an integer
	else
		MsgBox, %value% is not an integer
}
until x=21
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Simple question on integers and math with AHK

12 May 2020, 01:45

@ahketype , yes , thankx for the solution

Code: Select all

a:=20 , b:=10
;a:=20 , b:=7
c:=a/b
d:=a//b
msgbox,C=%c%`nD=%d%
if (c=d)                        ;- C= 2.000000  and D= 2 ( they are equal ) in first example it's an integer
  MsgBox, D=%d% IS  an integer ( equal to C )
else
  MsgBox, C=%c% NOT an integer
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750, septrinus and 256 guests