expression question

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

expression question

12 Jan 2016, 06:01

Why this expression works:

Code: Select all

a := 1.2 - 0.7
if (a = 0.5)
   msgbox 
But it does not work:

Code: Select all

a := 1.2 - 0.8
if (a = 0.4)
   msgbox 
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: expression question

12 Jan 2016, 06:18

You need to define the number of decimal places or round the result

Code: Select all

setformat, float, 0.1
See https://autohotkey.com/docs/commands/SetFormat.htm

Code: Select all

a := round((1.2 - 0.7),1)
if (a = 0.5)
   msgbox 0x40000,, % a

a := round((1.2 - 0.8),1)
if (a = 0.4)
   msgbox 0x40000,, % a
Donec Perficiam
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: expression question

12 Jan 2016, 06:27

Why the first example works without setting format.
I think that it is not logical.
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: expression question

12 Jan 2016, 08:15

If you want to see what's actually stored in the floating-point variable:

Code: Select all

SetFormat, FloatFast, 0.16
A := 1.2 - 0.7
B := 1.2 - 0.8
MsgBox, %A% - %B% ; 0.5000000000000000 - 0.3999999999999999
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: expression question

12 Jan 2016, 14:23

But why is that situation?
May be it is bug?

Code: Select all

SetFormat, FloatFast, 0.16
A := 1.3 - 0.9
B := 1.2 - 0.8
if (A != B)
   MsgBox, %A% - %B% ; 0.4000000000000000 - 0.3999999999999999
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: expression question

12 Jan 2016, 14:39

It's not a bug, and this behavior is not unique to AHK. It's just how floating point numbers work. Bottom line: don't use "=" to compare floating point numbers.
compare floating point
Floating Point Numbers - Computerphile - YouTube
wdmodlin
Posts: 150
Joined: 16 Dec 2015, 02:42

Re: expression question

12 Jan 2016, 17:48

I sense that you may not be satisfied with the answer, still feeling that it is "not logical" for one to work and the other not.

Let's see if I can explain in a way that will make you feel better about it.

Since these numbers involve fractional values, the computations are done with floating point numbers, which internally are a long string of bits that represent numbers APPROXIMATELY, to many decimal places.

When the system does the subtraction in the first case, it just happens to work out that all the low order bits were the same, so that is was equal to the internal representation of 0.5.

When it does the subtraction in the second place, it happened that some low order digits in the two representations were not the same, so even though the result was VERY close to 0.4, it was not exactly the same as 0.4. So the comparison fails.

This is how floating point arithmetic works, in any system, or on paper, or with a calculator. Answers are approximate to the number of decimal places used, and you cannot depend on EXACT comparisons.

Make sense?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: expression question

13 Jan 2016, 04:36

Thank to All!
Now I understand.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: expression question

13 Jan 2016, 05:15

An alternative for comparing floating point values is something like the following:

Code: Select all

tolerance := 0.00000000001
var := 0.5

if ((var - 0.5) < tolerance){
***insert code ***
} 
The tolerance value might need to be made larger, but as long as it has a few more decimal places than the values you're dealing with, it should work.

There may be a better way to do it, but this is usually how I do comparisons with floating point numbers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, filipemb, Rohwedder, silelunu and 313 guests