timbo Posted February 23, 2009 Posted February 23, 2009 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: CODELocal $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
LarryDalooza Posted February 23, 2009 Posted February 23, 2009 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. AutoIt has helped make me wealthy
timbo Posted February 23, 2009 Author Posted February 23, 2009 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?
jvanegmond Posted February 23, 2009 Posted February 23, 2009 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 github.com/jvanegmond
Bert Posted February 23, 2009 Posted February 23, 2009 (edited) Maybe Valik can suggest what search string I could use exactly?He will treat you as a fool for not searching first. He deplores stupidity. Edited February 23, 2009 by Volly The Vollatran project My blog: http://www.vollysinterestingshit.com/
timbo Posted February 23, 2009 Author Posted February 23, 2009 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/42980Ah, 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.
bo8ster Posted February 23, 2009 Posted February 23, 2009 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]
jvanegmond Posted February 23, 2009 Posted February 23, 2009 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. github.com/jvanegmond
bo8ster Posted February 23, 2009 Posted February 23, 2009 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]
timbo Posted February 23, 2009 Author Posted February 23, 2009 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
Bert Posted February 23, 2009 Posted February 23, 2009 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. The Vollatran project My blog: http://www.vollysinterestingshit.com/
LarryDalooza Posted February 23, 2009 Posted February 23, 2009 I wasn't hoping to derail the topic. It is just that Valik quotes are a bit of a novelty here, and I was anticipating one.In particular... when searching the forum... prefix words with a plus... like...+decimal +floathttp://www.autoitscript.com/forum/index.ph...=decimal++float AutoIt has helped make me wealthy
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now