(var1 = var2) inconsistent on different scripts Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

(var1 = var2) inconsistent on different scripts

06 Jul 2017, 12:25

I've tried running the same bit of code on 2 different scripts, using the same version of AHK, but am getting different results:

Code: Select all

vNum1 := 0.3, vNum2 := 0.1+0.2
MsgBox, % A_FormatFloat " " A_FormatInteger " " (vNum1 = vNum2) "`r`n" Format("{:.17f}", vNum2)
in one script (just the code above):
0.6 D 0
0.30000000000000004

in another script (a massive script with all sorts of things in it):
0.6 D 1
0.29999999999999999

Thanks for reading.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6962
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: (var1 = var2) inconsistent on different scripts  Topic is solved

06 Jul 2017, 18:12

This is not a bug, but a result of using SetFormat Float without Fast somewhere in the second script.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: (var1 = var2) inconsistent on different scripts

06 Jul 2017, 18:29

Many thanks, much appreciated.

So there is no way to detect that that 'mode' is in force, and no way to turn it off/on? ...

Code: Select all

vNum1 := 0.3, vNum2 := 0.1+0.2
MsgBox, % A_FormatFloat " " A_FormatInteger " " (vNum1 = vNum2) "`r`n" Format("{:.17f}", vNum2)

SetFormat, Float, 0.6
vNum1 := 0.3, vNum2 := 0.1+0.2
MsgBox, % A_FormatFloat " " A_FormatInteger " " (vNum1 = vNum2) "`r`n" Format("{:.17f}", vNum2)

SetFormat, FloatFast, 0.6
vNum1 := 0.3, vNum2 := 0.1+0.2
MsgBox, % A_FormatFloat " " A_FormatInteger " " (vNum1 = vNum2) "`r`n" Format("{:.17f}", vNum2)
I've now found this, although to be honest, I did not appreciate its significance or effective meaning at the time.
SetFormat
https://autohotkey.com/docs/commands/SetFormat.htm
If the slow mode "Integer" or "Float" is used anywhere in the script, even if that SetFormat line is never executed, the caching of integers or floating point numbers (respectively) is disabled the moment the script launches.
==================================================

It's quite interesting that the default behaviour is that this returns false:

Code: Select all

vNum1 := 0.3, vNum2 := 0.1+0.2
MsgBox, % (vNum1 = vNum2) ;0
That this should return false is more understandable:

Code: Select all

MsgBox, % (0.3 = 0.1+0.2) ;0
If anyone has any links to any threads discussing this that would be interesting. It makes me think that for certain mathematical scripts, I should be forcing 0.300000 to become 0.300000 etc, to maintain consistency when performing arithmetic.

I think many users would be surprised at this behaviour, so apologies if I haven't spotted a clear warning in the documentation.

==================================================

[EDIT:] So far the only good way I've found to get the desired behaviour is like this:

Code: Select all

vNum1 := 0.3, vNum2 := 0.1+0.2
MsgBox, % vNum1 " " vNum2 " " (vNum1 = vNum2) ;0.3 0.300000 0
MsgBox, % Format("{:.17f}", vNum1) "`r`n" Format("{:.17f}", vNum2)

vNum1 := "" vNum1
vNum2 := "" vNum2
MsgBox, % vNum1 " " vNum2 " " (vNum1 = vNum2) ;0.3 0.300000 1
MsgBox, % Format("{:.17f}", vNum1) "`r`n" Format("{:.17f}", vNum2)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6962
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: (var1 = var2) inconsistent on different scripts

06 Jul 2017, 19:04

That this should return false is more understandable:
I disagree. Why should you expect the result to be different after assigning each value to a variable?

Assigning numbers to a variable does not (or should not) change their values. Formatting them as strings changes their values.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: (var1 = var2) inconsistent on different scripts

06 Jul 2017, 19:18

I used the words 'more understandable', (not necessarily that I agreed or disagreed,) in terms of a function/command retrieving a number live v. numbers that were retrieved from a variable. There is a potential for a difference in behaviour there. Although I think AutoHotkey does the right thing in this case, it treats them the same by default. However, in the 'mode' I alluded to earlier, it does actually treat the two cases differently.

(AutoHotkey seems to remember whether a number was numeric/a string in all kinds of circumstances, which are important in relation to creating functions/classes for objects. I wasn't sure what the behaviour would be until I tested.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6962
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: (var1 = var2) inconsistent on different scripts

06 Jul 2017, 21:55

I used the words 'more understandable'
Yes, and I used the words 'I disagree' to contradict your statement.
iPhilip
Posts: 460
Joined: 02 Oct 2013, 12:21

Re: (var1 = var2) inconsistent on different scripts

07 Jul 2017, 00:08

You may find this post useful.
Windows 10 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Ask For Help”

Who is online

Users browsing this forum: CarPiotr, erikals, Google [Bot], k4rkarin, Kellyzkorner_NJ and 37 guests