maniootek

Numbers dot separated sum up bug?

2 posts in this topic

Guys I have  no idea what's wrong so I think it's a bug in autoit or I am lack of some knowledge.

Please take a look:

#include <Array.au3>
local $array[0]
_ArrayAdd($array, "54.83")
_ArrayAdd($array, "48.99")
_ArrayAdd($array, "25.58")
_ArrayAdd($array, "26.99")
_ArrayAdd($array, "43.99")
_ArrayAdd($array, "40")
_ArrayAdd($array, "29.94")
_ArrayAdd($array, "183.68")
_ArrayAdd($array, "20.99")
_ArrayAdd($array, "35.98")
_ArrayAdd($array, "26.99")
_ArrayAdd($array, "80.99")
_ArrayAdd($array, "35.89")
_ArrayAdd($array, "87")
_ArrayAdd($array, "60.99")
_ArrayAdd($array, "60.99")
_ArrayAdd($array, "29.39")
_ArrayAdd($array, "382")
_ArrayAdd($array, "47.98")
_ArrayAdd($array, "42.99")
_ArrayAdd($array, "40.99")
_ArrayAdd($array, "44.98")
_ArrayAdd($array, "86.99")
_ArrayAdd($array, "31.49")
_ArrayAdd($array, "169.99")
_ArrayAdd($array, "40.98")
_ArrayAdd($array, "37.88")
_ArrayAdd($array, "47.99")
_ArrayAdd($array, "35.89")
_ArrayAdd($array, "257.21")
_ArrayAdd($array, "29.39")
_ArrayAdd($array, "37.99")
_ArrayAdd($array, "83.99")
_ArrayAdd($array, "26.99")
_ArrayAdd($array, "40.99")
_ArrayAdd($array, "23.98")
_ArrayAdd($array, "117.4")
_ArrayAdd($array, "37.99")
_ArrayAdd($array, "525.4")
_ArrayAdd($array, "40.99")
_ArrayAdd($array, "26.99")
_ArrayAdd($array, "35.88")
_ArrayAdd($array, "29.99")
_ArrayAdd($array, "50.98")
_ArrayAdd($array, "53.66")
_ArrayAdd($array, "43.99")
_ArrayAdd($array, "54")
_ArrayAdd($array, "40.99")
_ArrayAdd($array, "26.99")
_ArrayAdd($array, "24.49")
_ArrayAdd($array, "49.49")
_ArrayAdd($array, "42.99")
_ArrayAdd($array, "46.99")
_ArrayAdd($array, "97")
_ArrayAdd($array, "21.48")
_ArrayAdd($array, "80")
_ArrayAdd($array, "69")
_ArrayAdd($array, "167.7")
_ArrayAdd($array, "103.93")
_ArrayAdd($array, "319.2")
_ArrayAdd($array, "112.96")
_ArrayAdd($array, "55.99")
_ArrayAdd($array, "32.89")
_ArrayAdd($array, "43.99")
_ArrayAdd($array, "83.99")
_ArrayAdd($array, "86.99")
_ArrayAdd($array, "32.89")
_ArrayAdd($array, "34.98")
_ArrayAdd($array, "40.99")
_ArrayAdd($array, "33")
_ArrayAdd($array, "35.49")
_ArrayAdd($array, "26.78")
_ArrayAdd($array, "150.7")
_ArrayAdd($array, "87")
;when you sum up this all number it should gives result 5370.6 but it gives 5370.05999999999

_ArrayDisplay($array)
$Result = "0.0"
for $i=0 to Ubound($array) - 1
    $Result += $array[$i]
Next
msgbox(0,0,$Result)

 

Share this post


Link to post
Share on other sites



maniootek,

No bug. Look up "floating point arithmetic" and you will see it is a result of how computers store doubles.

The solution? Convert to integers and then reconvert the result:

$Result = 0
For $i = 0 To UBound($array) - 1
    $Result += 100 * $array[$i]
Next
MsgBox(0, 0, $Result / 100)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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