Jump to content

Kahan summation: Difference between revisions

removing the artificial option setting
(removing the artificial option setting)
Line 372:
 
# 1. Do all arithmetic in decimal to a precision of six digits.
 
options(digits=6)
# R can only limit the number of digits being displayed, not the ones used in calculation, all operations are floating point
 
# 2. Write a function/method/procedure/subroutine/... to perform Kahan summation
Line 394 ⟶ 395:
 
# 4. Show that the simple left-to-right summation, equivalent to (a + b) + c gives an answer of 10005.8
(a + b) + c
# [1] 10005.986
 
# 5. Show that the Kahan function applied to the sequence of values a, b, c results in the more precise answer of 10005.9
input <- c(a, b, c)
kahansum(input)
# [1] 10005.986
 
options(digits=6)
 
# as there is no difference let's try the subtask
Line 412 ⟶ 411:
# [1] 1.11022e-16
 
a <- 1.00000000000000110
b <- epsilon
c <- -epsilon
Line 430 ⟶ 429:
# ok, then what is the difference?
((a + b) + c) - kahansum(c(a, b, c))
# [1] 2-1.22045e110223e-16
</lang>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.