MrCreatoR Posted January 26, 2021 Share Posted January 26, 2021 (edited) $i = 2 While $i < 3 $i += 0.01 If $i == 2.23 Then ConsoleWrite($i + 0.01 & @CRLF) ConsoleWrite(2.23 + 0.01 & @CRLF) EndIf WEnd Is it normal? Edited January 26, 2021 by MrCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 26, 2021 Moderators Share Posted January 26, 2021 MrCreatoR, Quote Is it normal? Absolutely - exactly what one would expect with floating number arithmetic. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Nine Posted January 26, 2021 Share Posted January 26, 2021 1 hour ago, MrCreatoR said: If $i == 2.23 Then And using case-sensitive strings operator is not going to help either TheDcoder 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Musashi Posted January 26, 2021 Share Posted January 26, 2021 (edited) 1 hour ago, MrCreatoR said: Is it normal? I am sure you are looking for an explanation, not a solution . However (for other readers) : $i = 2 While $i < 3 $i = Round($i + 0.01, 2) If $i = 2.23 Then ConsoleWrite($i + 0.01 & @CRLF) ConsoleWrite(2.23 + 0.01 & @CRLF) EndIf WEnd Edited January 26, 2021 by Musashi "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
argumentum Posted January 27, 2021 Share Posted January 27, 2021 4 hours ago, MrCreatoR said: Is it normal? the ^$$@#$^ floating points In 2 decimal ( money ) stuff, I move everything to integer. Do the whatever calculations, and then move it back to decimal. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
MrCreatoR Posted January 27, 2021 Author Share Posted January 27, 2021 (edited) 4 hours ago, Melba23 said: Absolutely Then how you explain the fact that this happens starting from 2.23 and not earlier? Check this: $i = 2 While $i < 3 $i += 0.01 ConsoleWrite($i & @CRLF) WEnd and then this: $i = 1 While $i < 2 $i += 0.01 ConsoleWrite($i & @CRLF) WEnd with the second example this behavior not occurred. 3 hours ago, Musashi said: I am sure you are looking for an explanation, not a solution . However (for other readers) : Another solution(s)... $i = 2 While $i < 3 $i = String($i) + 0.01 If $i = 2.23 Then ConsoleWrite($i + 0.01 & @CRLF) ConsoleWrite(2.23 + 0.01 & @CRLF) EndIf WEnd Edited January 27, 2021 by MrCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
AspirinJunkie Posted January 27, 2021 Share Posted January 27, 2021 (edited) 5 hours ago, MrCreatoR said: Then how you explain the fact that this happens starting from 2.23 and not earlier? It is not started at 2.23 it already starts before. You just don't see it because of the internal rounding rules for the string presentation of the numbers: $i = 1 While $i < 3 $i += 0.01 ConsoleWrite(StringFormat("%16s\t%.17f\n", $i, $i)) WEnd So you have several ways to keep the effect of the round-off-error as small as possible. One solution is to avoid ther error sum up by recalculating every time: $i = 1 For $x = 1 To 300 ConsoleWrite(StringFormat("%16s\t%.16f\n", 1 + 0.01 * $x, 1 + 0.01 * $x)) Next Another one is (as @argumentum already stated) to calculate in a number expression without rounding error (e.g. integer) as long as possible: $i = 100 While $i < 300 $i += 1 ConsoleWrite(StringFormat("%16s\t%.16f\n", $i / 100, $i / 100)) WEnd Edited January 27, 2021 by AspirinJunkie Link to comment Share on other sites More sharing options...
Musashi Posted January 27, 2021 Share Posted January 27, 2021 (edited) @MrCreatoR : As @Melba23 , @argumentum and @AspirinJunkie have pointed out, this is not an internal problem of AutoIt. For a deeper understanding, the following treatise may be helpful : This is an edited reprint of the paper "What Every Computer Scientist Should Know About Floating-Point Arithmetic", by David Goldberg, published in the March, 1991 issue of Computing Surveys. Copyright 1991, Association for Computing Machinery, Inc. : https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html Not necessarily light fare , but often referenced in discussions regarding 'Floating-Point Arithmetic'. Edited January 27, 2021 by Musashi typo "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
jchd Posted January 27, 2021 Share Posted January 27, 2021 A more low-level illustration of what's under the hood: $n = 0.01 is represented internally by the exact value +0.0099840000000000002078337502098293043673038482666015625 After performing this loop: Local $n = 2 For $i = 1 To 23 $n += 0.01 Next $n has the exact internal representation +2.2299999999999950972551232553087174892425537109375 while the value 2.23 is exactly represented internally as +2.229999999999999982236431605997495353221893310546875 which shows that equality can't be achieved. Each value shown above results from exactly computing and composing the components (sign, scale, mantissa) of the low-level FP representation. Musashi 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