grzesiek Posted April 15, 2015 Share Posted April 15, 2015 Round(36.285, 2) returns 36.28 Round(36.275, 2) returns also 36.28 Can someone confirm this? Link to comment Share on other sites More sharing options...
MikahS Posted April 15, 2015 Share Posted April 15, 2015 Local $val1 = Round(36.285, 2) Local $val2 = Round(36.275, 2) MsgBox(0, "", "val1: " & $val1 & @CRLF & "val2: " & $val2) Yes, I get the same result. Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ Link to comment Share on other sites More sharing options...
iamtheky Posted April 15, 2015 Share Posted April 15, 2015 (edited) more fun msgbox(0, '.275' , Round(number(.275), 2)) ; right msgbox(0, '.285' , Round(number(.285), 2)) ; wrong msgbox(0, '1.275' , Round(number(1.275), 2)) ; wrong msgbox(0, '1.285' , Round(number(1.285), 2)) ; right msgbox(0, '36.275' , Round(number(36.275), 2)) ;right msgbox(0, '36.285' , Round(number(36.285), 2)) ;wrong msgbox(0, '.555' , Round(number(.555), 2)) ;right msgbox(0, '.565' , Round(number(.565), 2)) ;wrong Edited April 15, 2015 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
MikahS Posted April 15, 2015 Share Posted April 15, 2015 (edited) Something to do with the 2nd decimal spot if it is an 8. Edited April 15, 2015 by MikahS Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ Link to comment Share on other sites More sharing options...
iamtheky Posted April 15, 2015 Share Posted April 15, 2015 (edited) but it works as expected for every other attempt (of x.275 and x.285), those are the only ones I have been able to break it with so far. Definitely seems odd, maybe a lesson in floating-point math is upcoming. ***added an example where .555 rounds correctly but .565 does not. Edited April 15, 2015 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
MikahS Posted April 15, 2015 Share Posted April 15, 2015 Definitely seems odd, maybe a lesson in floating-point math is upcoming. Seems this might be the case. Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ Link to comment Share on other sites More sharing options...
Spandex Posted April 15, 2015 Share Posted April 15, 2015 FWIW, Excel gives the same results: =MROUND(36.275,0.01) results in 36.28 =MROUND(36.285,0.01) results in 36.28 Link to comment Share on other sites More sharing options...
AspirinJunkie Posted April 15, 2015 Share Posted April 15, 2015 I think it's related to the binary rounding error: ConsoleWrite(StringFormat("%.3f = %.18f\n%.3f = %.18f\n", 0.555, 0.555, 0.565, 0.565)) Link to comment Share on other sites More sharing options...
Yashied Posted April 15, 2015 Share Posted April 15, 2015 The problem is that if the internal representation of the floating point number is 0.2849... instead of 0.285, the rounding algorithm will incorrectly assume the last digit is a 4 and not a 5 and then return the lower number instead of the higher one. ConsoleWrite(StringFormat('%.20f', 0.285) & @CRLF) ;==> 0.28499999999999998000 My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
iamtheky Posted April 15, 2015 Share Posted April 15, 2015 does this work?msgbox(0, '.275' , _Round(number(.275), 2)) ; right msgbox(0, '.285' , _Round(number(.285), 2)) ; wrong msgbox(0, '1.275' , _Round(number(1.275), 2)) ; wrong msgbox(0, '1.285' , _Round(number(1.285), 2)) ; right msgbox(0, '36.275' , _Round(number(36.275), 2)) ;right msgbox(0, '36.285' , _Round(number(36.285), 2)) ;wrong msgbox(0, '.555' , _Round(number(.555), 2)) ;right msgbox(0, '.565' , _Round(number(.565), 2)) ;wrong Func _Round($number , $nPlaces) $x = round(number($number) , $nPlaces) If number($x) < number($number) Then $sPlusOne = "." & stringformat("%0" & $nPlaces & "s" , "1") $x = number($x) + number($sPlusOne) EndIf return $x EndFunc ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
jchd Posted April 15, 2015 Share Posted April 15, 2015 Of course this would need to cope with negative values as well, but even then I wouldn't be surprised if counter-examples exist. BTW I think the help of Number is misleading: if flag = 0 means flag = Default, then we should get an integer which isn't the case. 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...
SadBunny Posted April 16, 2015 Share Posted April 16, 2015 ConsoleWrite(StringFormat("%i", 4.6 * 100) & @crlf) ConsoleWrite(StringFormat("%0.0f", 4.6 * 100) & @crlf) You would say this gives the same result twice. But it doesn't: 459 460 So apparently 4.6 * 100 = 459 if you expect an integer (yes, those are some very low values of 4.6 and 100) but 460 if you expect a float. Roses are FF0000, violets are 0000FF... All my base are belong to you. 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