Float Sum

lua-users home
wiki

Accurate floating point summation. Like Python's fsum.


local fsum = function(...)

  local pa = {}

  local paSize = 0

  for a = 1, select("#", ...) do

    local i, v = 1, select(a, ...)

    for j = 1, paSize do
      local p = pa[j]

      if math.abs(v) < math.abs(p) then v, p = p, v end

      local hi = v + p
      local lo = p - (hi - v)

      if lo ~= 0.0 then pa[i] = lo; i = i + 1 end
      v = hi
    end

    if i <= paSize then pa[i] = v; paSize = i
                   else paSize = paSize + 1; pa[paSize] = v end

  end

  local sum = 0; for j = 1, paSize do sum = sum + pa[j] end; return sum

end



local sum = function(...)

  local sum = 0

  for a = 1, select("#", ...) do
    local v = select(a, ...)

    sum = sum + v
  end

  return sum

end


assert(
   sum(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
  == 0.9999999999999999
)

assert(
  fsum(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
  == 1.0
)

RecentChanges · preferences
edit · history
Last edited September 21, 2013 6:11 am GMT (diff)