Kahan summation: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 918:
 
See also http://keiapl.info/anec/#fuzz
 
=={{header|Kotlin}}==
Kotlin does not have a 6 digit decimal type. The closest we have to it is the 4-byte floating point type, Float, which has a precision of 6 to 9 significant decimal digits - about 7 on average. So performing the alternative task:
<lang scala>// version 1.1.1
 
fun kahanSum(vararg fa: Float): Float {
var sum = 0.0f
var c = 0.0f
for (f in fa) {
val y = f - c
val t = sum + y
c = (t - sum) - y
sum = t
}
return sum
}
 
fun epsilon(): Float {
var eps = 1.0f
while (1.0f + eps != 1.0f) eps /= 2.0f
return eps
}
 
fun main(args: Array<String>) {
val a = 1.0f
val b = epsilon()
val c = -b
println("Epsilon = $b")
println("(a + b) + c = ${(a + b) + c}")
println("Kahan sum = ${kahanSum(a, b, c)}")
}</lang>
 
{{out}}
<pre>
Epsilon = 5.9604645E-8
(a + b) + c = 0.99999994
Kahan sum = 1.0
 
</pre>
 
=={{header|PARI/GP}}}==
9,488

edits