Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Kahan summation algorithm
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Possible invalidation by compiler optimization== In principle, a sufficiently aggressive [[Compiler optimization|optimizing compiler]] could destroy the effectiveness of Kahan summation: for example, if the compiler simplified expressions according to the [[associativity]] rules of real arithmetic, it might "simplify" the second step in the sequence : <code>t = sum + y;</code> : <code>c = (t - sum) - y;</code> to : <code>c = ((sum + y) - sum) - y;</code> and then to : <code>c = 0;</code> thus eliminating the error compensation.<ref name=Goldberg91>{{Citation | title=What every computer scientist should know about floating-point arithmetic |first1=David | last1=Goldberg |journal=[[ACM Computing Surveys]] | volume=23 | issue=1 | pages=5β48 | date=March 1991 |doi=10.1145/103162.103163 |s2cid=222008826 |url=http://www.validlab.com/goldberg/paper.pdf }}.</ref> In practice, many compilers do not use associativity rules (which are only approximate in floating-point arithmetic) in simplifications, unless explicitly directed to do so by compiler options enabling "unsafe" optimizations,<ref>[[GNU Compiler Collection]] manual, version 4.4.3: [https://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Optimize-Options.html 3.10 Options That Control Optimization], ''-fassociative-math'' (Jan. 21, 2010).</ref><ref>''[http://h21007.www2.hp.com/portal/download/files/unprot/Fortran/docs/unix-um/dfumperf.htm Compaq Fortran User Manual for Tru64 UNIX and Linux Alpha Systems] {{Webarchive|url=https://web.archive.org/web/20110607140000/http://h21007.www2.hp.com/portal/download/files/unprot/Fortran/docs/unix-um/dfumperf.htm |date=2011-06-07 }}'', section 5.9.7 Arithmetic Reordering Optimizations (retrieved March 2010).</ref><ref>BΓΆrje Lindh, [http://www.sun.com/blueprints/0302/optimize.pdf Application Performance Optimization], ''Sun BluePrints OnLine'' (March 2002).</ref><ref>Eric Fleegal, "[http://msdn.microsoft.com/en-us/library/aa289157%28VS.71%29.aspx Microsoft Visual C++ Floating-Point Optimization]", ''Microsoft Visual Studio Technical Articles'' (June 2004).</ref> although the [[Intel C++ Compiler]] is one example that allows associativity-based transformations by default.<ref>Martyn J. Corden, "[http://software.intel.com/en-us/articles/consistency-of-floating-point-results-using-the-intel-compiler/ Consistency of floating-point results using the Intel compiler]", ''Intel technical report'' (Sep. 18, 2009).</ref> The original [[K&R C]] version of the [[C programming language]] allowed the compiler to re-order floating-point expressions according to real-arithmetic associativity rules, but the subsequent [[ANSI C]] standard prohibited re-ordering in order to make C better suited for numerical applications (and more similar to [[Fortran]], which also prohibits re-ordering),<ref>{{cite journal |first=Tom |last=MacDonald |title=C for Numerical Computing |journal=Journal of Supercomputing |volume=5 |issue=1 |pages=31β48 |year=1991 |doi=10.1007/BF00155856|s2cid=27876900 }}</ref> although in practice compiler options can re-enable re-ordering, as mentioned above. A portable way to inhibit such optimizations locally is to break one of the lines in the original formulation into two statements, and make two of the intermediate products [[Volatile variable|volatile]]: '''function''' KahanSum(input) '''var''' sum = 0.0 '''var''' c = 0.0 '''for''' i = 1 '''to''' input.length '''do''' '''var''' y = input[i] - c '''volatile var''' t = sum + y '''volatile var''' z = t - sum c = z - y sum = t '''next''' i '''return''' sum
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)