September 14, 2014 [Issue 13474] New: 32 bit DMD optimizer FP arithmetic bug | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=13474 Issue ID: 13474 Summary: 32 bit DMD optimizer FP arithmetic bug Product: D Version: D2 Hardware: x86 OS: All Status: NEW Severity: critical Priority: P1 Component: DMD Assignee: nobody@puremagic.com Reporter: ilyayaroshenko@gmail.com KBN summation return wrong result when compiled with dmd -m32 -O With -m64 dmd works correctly LDC works correctly both m32 and m64 +++++++++++++++++++++++++++++++++ /** Kahan-Babuška-Neumaier summation algorithm +++++++++ s := x[1] c := 0 FOR i := 2 TO n DO t := s + x[i] IF ABS(s) >= ABS(x[i]) THEN c := c + ((s-t)+x[i]) ELSE c := c + ((x[i]-t)+s) END IF s := t END DO s := s + c +++++++++ */ F sumKBN(Range, F = Unqual!(ForeachType!Range))(Range r, F s = 0.0) { F c = 0.0; foreach(F x; r) { F t = s + x; if(s.fabs >= x.fabs) { F y = s-t; c += y+x; } else { F y = x-t; c += y+s; } s = t; } return s + c; } unittest { import std.algorithm : map; auto ar = [1, 1e100, 1, -1e100].map!(a => a*10000); double r = 20000; assert(r == ar.sumKBN); // fails with dmd -m32 -O } +++++++++++++++++++++++++++++++++ Git Reference: https://github.com/D-Programming-Language/phobos/pull/2513 -- |
Copyright © 1999-2021 by the D Language Foundation