Xerix Posted February 18, 2014 Share Posted February 18, 2014 Hello In a program, I have to do the addition of several numbers. But instead of getting 32.04, I get 32.0400000000001 I can reduce this result to 2 digits after the decimal point but why this result, while there are always 2 digits after the decimal point ? I do not know from where comes the problem, so I've put all the numbers in addition to test. Thank you $cal = 0.19+0.11+0.4+0.35+0.43+0.27+1.22+0.07+0.04+0.2+6.8+0.04+0.03+0.24+0.62+0.75+0.77+0.18+0.77+0.1+1.35+1.05+0.01+0.01+2.46+5.94+0.19+0.04+2.27+0.02+0.03+0.03+0.03+0.03+0.02+0.02+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.02+0.03+0.03+0.03+0.03+0.03+0.02+0.03+0.03+0.03+0.03+0.03+0.03+0.04+0.03+0.03+0.03+0.03+0.03+0.02+0.02+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.32+0.17+1.35+0.45+1.05 MsgBox(4096,"",$cal) Link to comment Share on other sites More sharing options...
Danyfirex Posted February 18, 2014 Share Posted February 18, 2014 Hi. use StringFormat $cal = 0.19+0.11+0.4+0.35+0.43+0.27+1.22+0.07+0.04+0.2+6.8+0.04+0.03+0.24+0.62+0.75+0.77+0.18+0.77+0.1+1.35+1.05+0.01+0.01+2.46+5.94+0.19+0.04+2.27+0.02+0.03+0.03+0.03+0.03+0.02+0.02+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.02+0.03+0.03+0.03+0.03+0.03+0.02+0.03+0.03+0.03+0.03+0.03+0.03+0.04+0.03+0.03+0.03+0.03+0.03+0.02+0.02+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.03+0.32+0.17+1.35+0.45+1.05 MsgBox(4096,"",StringFormat("%.2f",$cal)) Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Solution jchd Posted February 18, 2014 Solution Share Posted February 18, 2014 Floating-point uses binary representation internally, not decimal. Hence rounding errors can accumulate very easily. This is inherent to IEEE754 floating-point, so AutoIt by itself can do nothing to help. Use Round(), StringFormat() or shift all all your inputs by 10n and perform calculation in integers. Danyfirex 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Xerix Posted February 18, 2014 Author Share Posted February 18, 2014 (edited) Thank you Danyfirex Yes it works, we can also use ROUND but that I wanted to know is if it was normal or if it was a bug. Jchd Thank you for the explanation Edited February 18, 2014 by Xerix Link to comment Share on other sites More sharing options...
jchd Posted February 18, 2014 Share Posted February 18, 2014 (edited) You can see the phenomenom by yourself by running this simple script: ConsoleWrite(Hex(0.75) & @LF) ; 3FE8000000000000 ConsoleWrite(Hex(0.3) & @LF) ; 3FD3333333333333 Point your browser to this page to better see the breakout of those values and see that 0.3 has an infinite fractional binary expansion which can only be approximated within a 64-bit FP value. EDIT: while I'm at it, keep in mind that FP has two distinct representations of zero: +0.0 and -0.0 Fortunately AutoIt compares them equal, despite their hex representation differ: ConsoleWrite("0.0 = " & Hex(0.0) & @LF) ConsoleWrite("-0.0 = " & Hex(-0.0) & @LF) ConsoleWrite("0.0 = -0.0 " & (0.0 = -0.0) & @LF) ConsoleWrite("-0.0 > 0 " & (-0.0 > 0) & @LF) ConsoleWrite("-0.0 < 0 " & (-0.0 < 0) & @LF) Edited February 18, 2014 by jchd Danyfirex 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
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