Kahan summation: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Added Ruby) |
|||
Line 2,012: | Line 2,012: | ||
ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª</pre> |
ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª</pre> |
||
=={{header|Ruby}}== |
|||
Arrays provide a "sum" method, which uses a Kahan-Babuska balancing compensated summation algorithm, according to the C-source. |
|||
<lang ruby>epsilon = 1.0 |
|||
epsilon /= 2 until 1.0 + epsilon == 1.0 |
|||
a = 1.0 |
|||
b = epsilon |
|||
c = -b |
|||
puts "epsilon : #{epsilon}" |
|||
puts "(a+b)+c : #{(a+b)+c}" |
|||
puts "[a,b,c].sum: #{[a,b,c].sum}" |
|||
</lang> |
|||
{{out}} |
|||
<pre>epsilon : 1.1102230246251565e-16 |
|||
(a+b)+c : 0.9999999999999999 |
|||
[a,b,c].sum: 1.0 |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===IEEE 754 Single precision 32-bit (JavaScript defaults to Double precision.)=== |
===IEEE 754 Single precision 32-bit (JavaScript defaults to Double precision.)=== |
||
Line 2,045: | Line 2,063: | ||
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/lWI0SJC/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/CI7dNAKQSlupivKAnYcGJg Scastie (remote JVM)]. |
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/lWI0SJC/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/CI7dNAKQSlupivKAnYcGJg Scastie (remote JVM)]. |
||
Note: JVM float is IEEE-754 32 bit floating point while JavaScript always default to the IEEE 754 standard 64-bit. |
Note: JVM float is IEEE-754 32 bit floating point while JavaScript always default to the IEEE 754 standard 64-bit. |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
===Tcl: Floats=== |
===Tcl: Floats=== |