Skip to content

fix(optimizer): add sum([...]) elements left to right - #999

Open
waw666waw666 wants to merge 1 commit into
expr-lang:masterfrom
waw666waw666:fix-sum-array-float-order
Open

waw666waw666 wants to merge 1 commit into
expr-lang:masterfrom
waw666waw666:fix-sum-array-float-order

Conversation

@waw666waw666

Copy link
Copy Markdown

The sum([...]) optimization rewrites sum([a, b, c]) as a + (b + c). The sum builtin adds the elements in order, so the optimized program groups float additions differently and can return a different value than the same expression with expr.Optimize(false):

env := map[string]any{"a": 1e16, "b": -1e16, "c": 1.0, "x": 0.1, "y": 0.2, "z": 0.3}

for _, code := range []string{`sum([a, b, c])`, `sum([x, y, z])`, `sum([x, y, z]) == x + y + z`} {
	program, _ := expr.Compile(code, expr.Env(env))
	out, _ := expr.Run(program, env)
	fmt.Println(code, out)
}
// sum([a, b, c]) 0                       (with expr.Optimize(false): 1)
// sum([x, y, z]) 0.6                     (with expr.Optimize(false), and sum() over a []float64: 0.6000000000000001)
// sum([x, y, z]) == x + y + z false      (with expr.Optimize(false): true)

Literal arrays are folded into a constant before this pass, so the rewrite only applies when the elements are variables or other expressions.

Cause: sumArrayFold built the + chain recursively from the right. Float addition is not associative, so a + (b + c) and (a + b) + c can differ.

Fix: build the chain from the left, ((a + b) + c) + ..., which is the order the sum loop adds in. For ints nothing changes. Wrap-around on overflow gives the same result in any grouping.

Tests:

  • TestOptimize_sum_array_3 now expects (a + b) + c.
  • New TestOptimize_sum_array_float_order checks the three expressions above against the expected values and against the same program compiled with expr.Optimize(false). It fails on master and passes with this change.
  • go test ./..., go vet ./... and gofmt all pass. The expr_debug debugger test also passes.

The sum array optimization rewrote sum([a, b, c]) as a + (b + c), while
the sum builtin adds the elements in order. Float addition is not
associative, so the optimized program could return a different value:
with a = 1e16, b = -1e16, c = 1 it returned 0 instead of 1, and
sum([x, y, z]) == x + y + z was false for 0.1, 0.2, 0.3.

Build ((a + b) + c) instead, which matches the runtime order.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant