Jump to content

Round


Recommended Posts

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 ~ getENVvars
My 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 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 by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Something to do with the 2nd decimal spot if it is an 8.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My 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

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 by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Definitely seems odd, maybe a lesson in floating-point math is upcoming. 

Seems this might be the case.  :lol:

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My 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

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
Link to comment
Share on other sites

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

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 here
RegExp tutorial: enough to get started
PCRE 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

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

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...