argumentum

(+n) + (-n) + (-n) = baloney ???? [SOLVED]

13 posts in this topic

#1 ·  Posted (edited)

Local $a  = 1600 + -1384.92 + -215.08
ConsoleWrite( $a &@CRLF)

my result is "-8.5265128291212e-014" !!!!.

What am I doing wrong ?, how should I handle this ?.

- - - - - - - - - - - - - - - - - - - - 

Edit: I can not say I solved it but going from real to integer and back does it:

Local $a  = ((1600*100) + (-1384.92*100) + (-215.08*100)) /100
ConsoleWrite( "--- >"&$a&"<" &@CRLF)

Is there a more elegant way ?

Edited by argumentum

Share this post


Link to post
Share on other sites



This is the result of the internal floating point representation of the numbers -> see https://en.wikipedia.org/wiki/Floating_point

As a workaround you can use Round()

ConsoleWrite( Round($a, 10) & @CRLF)

 

1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

One of these might be what you are looking for. Its all due to IEEE binary math on decimal numbers

Local $a  = 1600 + -1384.92 + -215.08
ConsoleWrite( $a &@CRLF)
ConsoleWrite( StringFormat("%0.20f",$a) &@CRLF)
ConsoleWrite( StringFormat("%0.12f",$a) &@CRLF)
ConsoleWrite( round($a,12) &@CRLF)

 

 


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

argumentum,

Quote

What am I doing wrong ?

Nothing - search the forum for floating point arithmetic and all will become clear. If faced with the same problem I would do something like this:

Local $a[]  = [1600, -1384.92, -215.08]
$nTotal = _Total($a)
ConsoleWrite($nTotal &@CRLF)

Func _Total($aElements)

    $nTotal = 0
    For $i = 0 To UBound($aElements) - 1
        $nTotal += (100 * $aElements[$i])
    Next
    Return $nTotal / 100

EndFunc

Which is basically what you were doing.

M23

Edit: Guten abend, UEZ.

Edited by Melba23
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

argumentum,

I get

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\Tom\Desktop\ss.au3" /UserParams    
+>14:28:37 Starting AutoIt3Wrapper v.15.920.938.0 SciTE v.3.6.0.0   Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64    Environment(Language:0409)
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\Tom\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Tom\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.2)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\Tom\Desktop\ss.au3
+>14:28:37 AU3Check ended.rc:0
>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\Tom\Desktop\ss.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
--- >0<
+>14:28:37 AutoIt3.exe ended.rc:0
+>14:28:37 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.5785

when I run your code...

kylomas


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

4 minutes ago, Melba23 said:

Edit: Guten abend, UEZ.

Guten Abend Melba23. :)

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Oh boy, FP arithmetic is so unstable. I see no reason why these very small numbers cannot be added together using FP arithmetic. I would go as far as to say it's absolutely shameful to see this result. It seems you can never rely (100%) on FP for programming or anything else, which kind of defeats the objective.

Edit : Baloney is absolutely the right word.

Edited by czardas

Share this post


Link to post
Share on other sites

I beg to radically differ.

The current IEEE binary FP specifications are very clever and implement the most advanced techniques available for all kind of (binary) FP computations. You really should [try to] read that document to get a feeling of how sophisticated it is.

Binary FP has inherent limitations and it make as much sense to blame your FP unit or any language using it for binary FP rounding errors than to blame your CPU register for using 2's complement integer representation, where max (positive) integer + 1 = min (negative) integer.

If you really want to get rid of binary FP rounding errors, use a radix-10 IEEE 754-2008 library or hardware (e.g. a Power machine), that or a good arbitrary-precision arithmetic library.


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)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

You are right I probably should read it; but this appears, for all intents and purposes, to be much more than a rounding error. Even the exponent is way out - so much so that it scares the hell out of me.

Edit : I take it back. I only looked at the exponent and didn't do the maths. Since the answer is meant to be zero, it's close enough. DUH! :whistle:

I actually thought it was out by a ratio of billions to one. At least I can laugh at my mistake. :rolleyes:

Edited by czardas

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

For this blasphem, your punishment will be to write an AutoIt program to compute(*) the value of r  (expressed in yards, feet, inches and rational fraction down to 1/128") in the case where the radius R of the field is 405.3 m in the following problem: https://en.wikipedia.org/wiki/Goat_problem

Estimate yourself lucky that the solution is shown in full!

As a side bonus it should also make you feel how practical are imperial units.

(*) without just mutiplicating the numeric value for r=1 by R, please!

Edited by jchd
1 person likes this

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)

Share this post


Link to post
Share on other sites

#12 ·  Posted

No problem. I'm going to do pounds shillings and pence conversion too! :D

Share this post


Link to post
Share on other sites

#13 ·  Posted

"Pence Conversion" is a VERY different thing in the US! :P

3 people like this

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