?

Average Error: 19.3 → 0.8
Time: 7.0s
Precision: binary64
Cost: 32640

?

\[-1000 < x \land x < 1000\]
\[\sin x - x \]
\[\mathsf{fma}\left(-0.16666666666666666, {x}^{3}, \mathsf{fma}\left(0.008333333333333333, {x}^{5}, -0.0001984126984126984 \cdot {x}^{7}\right)\right) \]
(FPCore (x) :precision binary64 (- (sin x) x))
(FPCore (x)
 :precision binary64
 (fma
  -0.16666666666666666
  (pow x 3.0)
  (fma
   0.008333333333333333
   (pow x 5.0)
   (* -0.0001984126984126984 (pow x 7.0)))))
double code(double x) {
	return sin(x) - x;
}
double code(double x) {
	return fma(-0.16666666666666666, pow(x, 3.0), fma(0.008333333333333333, pow(x, 5.0), (-0.0001984126984126984 * pow(x, 7.0))));
}
function code(x)
	return Float64(sin(x) - x)
end
function code(x)
	return fma(-0.16666666666666666, (x ^ 3.0), fma(0.008333333333333333, (x ^ 5.0), Float64(-0.0001984126984126984 * (x ^ 7.0))))
end
code[x_] := N[(N[Sin[x], $MachinePrecision] - x), $MachinePrecision]
code[x_] := N[(-0.16666666666666666 * N[Power[x, 3.0], $MachinePrecision] + N[(0.008333333333333333 * N[Power[x, 5.0], $MachinePrecision] + N[(-0.0001984126984126984 * N[Power[x, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\sin x - x
\mathsf{fma}\left(-0.16666666666666666, {x}^{3}, \mathsf{fma}\left(0.008333333333333333, {x}^{5}, -0.0001984126984126984 \cdot {x}^{7}\right)\right)

Error?

Target

Original19.3
Target0.1
Herbie0.8
\[\begin{array}{l} \mathbf{if}\;\left|x\right| < 0.07:\\ \;\;\;\;-\left(\left(\frac{{x}^{3}}{6} - \frac{{x}^{5}}{120}\right) + \frac{{x}^{7}}{5040}\right)\\ \mathbf{else}:\\ \;\;\;\;\sin x - x\\ \end{array} \]

Derivation?

  1. Initial program 19.3

    \[\sin x - x \]
  2. Taylor expanded in x around 0 0.8

    \[\leadsto \color{blue}{-0.16666666666666666 \cdot {x}^{3} + \left(0.008333333333333333 \cdot {x}^{5} + -0.0001984126984126984 \cdot {x}^{7}\right)} \]
  3. Simplified0.8

    \[\leadsto \color{blue}{\mathsf{fma}\left(-0.16666666666666666, {x}^{3}, \mathsf{fma}\left(0.008333333333333333, {x}^{5}, -0.0001984126984126984 \cdot {x}^{7}\right)\right)} \]
    Proof

    [Start]0.8

    \[ -0.16666666666666666 \cdot {x}^{3} + \left(0.008333333333333333 \cdot {x}^{5} + -0.0001984126984126984 \cdot {x}^{7}\right) \]

    fma-def [=>]0.8

    \[ \color{blue}{\mathsf{fma}\left(-0.16666666666666666, {x}^{3}, 0.008333333333333333 \cdot {x}^{5} + -0.0001984126984126984 \cdot {x}^{7}\right)} \]

    fma-def [=>]0.8

    \[ \mathsf{fma}\left(-0.16666666666666666, {x}^{3}, \color{blue}{\mathsf{fma}\left(0.008333333333333333, {x}^{5}, -0.0001984126984126984 \cdot {x}^{7}\right)}\right) \]
  4. Final simplification0.8

    \[\leadsto \mathsf{fma}\left(-0.16666666666666666, {x}^{3}, \mathsf{fma}\left(0.008333333333333333, {x}^{5}, -0.0001984126984126984 \cdot {x}^{7}\right)\right) \]

Alternatives

Alternative 1
Error0.8
Cost20096
\[-0.16666666666666666 \cdot {x}^{3} + \left(-0.0001984126984126984 \cdot {x}^{7} + 0.008333333333333333 \cdot {x}^{5}\right) \]
Alternative 2
Error0.8
Cost20096
\[-0.0001984126984126984 \cdot {x}^{7} + \left(-0.16666666666666666 \cdot {x}^{3} + 0.008333333333333333 \cdot {x}^{5}\right) \]
Alternative 3
Error0.9
Cost19648
\[\mathsf{fma}\left(-0.16666666666666666, {x}^{3}, 0.008333333333333333 \cdot {x}^{5}\right) \]
Alternative 4
Error0.9
Cost13376
\[-0.16666666666666666 \cdot {x}^{3} + 0.008333333333333333 \cdot {x}^{5} \]
Alternative 5
Error1.2
Cost6656
\[-0.16666666666666666 \cdot {x}^{3} \]
Alternative 6
Error1.3
Cost448
\[x \cdot \left(-0.16666666666666666 \cdot \left(x \cdot x\right)\right) \]
Alternative 7
Error1.3
Cost448
\[\left(x \cdot x\right) \cdot \left(-0.16666666666666666 \cdot x\right) \]
Alternative 8
Error59.8
Cost128
\[-x \]

Error

Reproduce?

herbie shell --seed 2023187 
(FPCore (x)
  :name "bug500 (missed optimization)"
  :precision binary64
  :pre (and (< -1000.0 x) (< x 1000.0))

  :herbie-target
  (if (< (fabs x) 0.07) (- (+ (- (/ (pow x 3.0) 6.0) (/ (pow x 5.0) 120.0)) (/ (pow x 7.0) 5040.0))) (- (sin x) x))

  (- (sin x) x))