Jump to content

Why can't AutoIt perform basic maths?


timbo
 Share

Recommended Posts

Hello Everyone,

Sorry for the topic title; I figured it would get a little more attention if I put it that way. I love AutoIt; but still, this is a problem with basic maths.

I sure I'm not the first person to experience this so I have tried searching for the solution, but I don't know the best keywords to use? "+float +addition +loop"...?

Anyway, I have the most basic loop (using a float instead of and integer for the loop increment), but it keeps producing incorrect values.

See code below:

CODE
Local $temp_file=FileOpen(@ScriptDir & "\test.txt", 10)

For $x = 0.001 to 1.50 Step 0.001

FileWrite($temp_file, $x & " " & 0.001 & @CRLF)

Next

FileClose($temp_file)

Now it's pretty easy to imagine what the above would output, but for some reason it occasionally throws the float value out by a 0.0000000000000001 (+ or -).

See output below:

0.068 0.001

0.069 0.001

0.07 0.001

0.071 0.001

0.0720000000000001 0.001

0.0730000000000001 0.001

0.0740000000000001 0.001

0.0750000000000001 0.001

0.0760000000000001 0.001

0.0770000000000001 0.001

0.0780000000000001 0.001

0.0790000000000001 0.001

0.0800000000000001 0.001

0.0810000000000001 0.001

0.0820000000000001 0.001

0.0830000000000001 0.001

0.0840000000000001 0.001

0.0850000000000001 0.001

0.0860000000000001 0.001

0.0870000000000001 0.001

0.0880000000000001 0.001

0.0890000000000001 0.001

0.0900000000000001 0.001

0.0910000000000001 0.001

0.0920000000000001 0.001

0.0930000000000001 0.001

0.0940000000000001 0.001

0.0950000000000001 0.001

0.0960000000000001 0.001

0.0970000000000001 0.001

0.0980000000000001 0.001

0.0990000000000001 0.001

0.1 0.001

0.105 0.005

0.11 0.005

0.115 0.005

Now this could be corrected by using "Round()", but I need to round to both 3 decimal places and 2 decimal places (dependant on the situation). Anyway, even if this was an option, I'd still like to know why this isn't calculating properly.

Thanks in advance for any assistance provided.

-Timbo

Link to comment
Share on other sites

I imagine Valik will be in soon to lambast you for not exhausting the search function of the forum before posting. Floats and their behavior has been discussed several times before.

Lar.

Maybe Valik can suggest what search string I could use exactly?

Link to comment
Share on other sites

I just found the best thing ever while Googling. It explains everything that I've always wanted to say but never could describe in detail.

http://support.microsoft.com/kb/42980

Ah, I see.

My apologies to the AutoIt team; it seems they are bound by the laws of Binary Number Systems. So am I right to presume this would happen in other programming languages?

Anyway, I'll program around it by splitting that values either side of the floating point into integers.

@Volly: Are you insinuating that I'm stupid? I have began searching like mad to find a similar post (as I feel silly now for wasting everyones time), but I still cannot find anything related. My search skills must need some significant improvement. I did say in my original post that I did attempt to search, but my attempts didn't yield any results.

Link to comment
Share on other sites

Yes - its the same for all languages. Floating point is not accurate.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

My apologies to the AutoIt team; it seems they are bound by the laws of Binary Number Systems. So am I right to presume this would happen in other programming languages?

Yes. We've even experimented with it so if it is actually the same in other languages. In C++ you will ( often or always ) get the same results as in AutoIt when doing math operations.
Link to comment
Share on other sites

Yes. We've even experimented with it so if it is actually the same in other languages. In C++ you will ( often or always ) get the same results as in AutoIt when doing math operations.

In C++, with floats you get the same results. For decimal point precision in C++/Java you use doubles. I'm not sure how AutoIt implements numbers or if you can make it use doubles instead of floats.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Well thanks to Manadar & bo8ster, the only two with an actual answer to my question.

I am yet to be proven stupid (Valik pending) with an example of what I should have searched for, and I'm unimpressed with Volly's contribution especially as it seems he didn't even read my initial post stating that I had attempted searching for the solution.

As you may have noticed I am new to this community, and I am yet to find my place in it. I always do my best to read help files, guides and to search the forums, but regardless of this it seems I have still managed to upset some people. (I think I may have brought it on myself with the "Basic Maths" quip... :) )

Again, many thanks!

-Timbo

Link to comment
Share on other sites

Ah, I see.

My apologies to the AutoIt team; it seems they are bound by the laws of Binary Number Systems. So am I right to presume this would happen in other programming languages?

Anyway, I'll program around it by splitting that values either side of the floating point into integers.

@Volly: Are you insinuating that I'm stupid? I have began searching like mad to find a similar post (as I feel silly now for wasting everyones time), but I still cannot find anything related. My search skills must need some significant improvement. I did say in my original post that I did attempt to search, but my attempts didn't yield any results.

I'm not insulting you at all. I'm simply telling you how I believe Valik will react to you.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...