Anonymous user
Kahan summation: Difference between revisions
Implementation in Crystal
(Implementation in Crystal) |
|||
Line 351:
(a + b) + c = 1
Kahan sum = 1</pre>
=={{header|Crystal}}==
{{works with|crystal|0.31.1}}
{{trans|C++}}
<lang crystal>def epsilon
eps = 1.0_f32
while 1.0_f32 + eps != 1.0_f32
eps /= 2.0_f32
end
eps
end
def kahan(nums)
sum = 0.0_f32
c = 0.0_f32
nums.each do |num|
y = num - c
t = sum + y
c = (t - sum) - y
sum = t
end
sum
end
a = 1.0_f32
b = epsilon
c = -b
puts "Epsilon = #{b}"
puts "Sum = #{a + b + c}"
puts "Kahan sum = #{kahan([a, b, c])}"</lang>
{{out}}
<pre>Epsilon = 5.9604645e-8
Sum = 0.99999994
Kahan sum = 1.0
</pre>
=={{header|D}}==
|