AutoHotkey Community

It is currently May 27th, 2012, 12:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: August 23rd, 2011, 12:20 pm 
Offline

Joined: May 5th, 2011, 7:42 am
Posts: 6
I've just stumpled upon the following behaviour using AHK_L v1.1.02.03 running on Win7 Ultimate. When executing the following code it produces 0 (false). I would expect the variables to be equal.

Code:
#NoEnv
#Warn

Test()

Test() {
   global Test
   t1 := new Test()
   t2 := new Test()
   
   t1._n := 0.10 + 0.20
   t2._n := 0.30

   ; Displays 0
   MsgBox, % t1._n == t2._n
}

class Test {
   _n := 0
}

Changing the values slightly produces the correct result:

Code:
   ...

   t1._n := 0.11 + 0.20
   t2._n := 0.31

   ; Displays 1
   MsgBox, % t1._n == t2._n
   
   ...



Running Win7 Ultimate with AHK_L v1.1.02.03 (using any one of the three .bin basefiles).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 23rd, 2011, 1:59 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Floating-point arithmetic is by nature imprecise. It just happens that AutoHotkey's default float formatting only lets you see part of the number. Also note that in v1.x, literal floating-point numbers are basically unquoted strings. This is why in the example below, t2 shows as "0.30".
Code:
SetFormat FloatFast, .17
t1 := 0.10 + 0.20
t2 := 0.30
MsgBox, % t1 "`n" t2 "`n" (t1 == t2)
. "`n" (Abs(t1-t2) < 0.00000000001)

There's a bit of discussion about floating-point imprecision in the following thread:
http://www.autohotkey.com/forum/topic43329.html


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 3 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group