?

Average Error: 0.03% → 0.01%
Time: 4.5s
Precision: binary64
Cost: 6720

?

\[x \cdot y + \left(1 - x\right) \cdot z \]
\[\mathsf{fma}\left(x, y - z, z\right) \]
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
(FPCore (x y z) :precision binary64 (fma x (- y z) z))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
double code(double x, double y, double z) {
	return fma(x, (y - z), z);
}
function code(x, y, z)
	return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z))
end
function code(x, y, z)
	return fma(x, Float64(y - z), z)
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_] := N[(x * N[(y - z), $MachinePrecision] + z), $MachinePrecision]
x \cdot y + \left(1 - x\right) \cdot z
\mathsf{fma}\left(x, y - z, z\right)

Error?

Derivation?

  1. Initial program 0.03

    \[x \cdot y + \left(1 - x\right) \cdot z \]
  2. Simplified0.01

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
    Proof

    [Start]0.03

    \[ x \cdot y + \left(1 - x\right) \cdot z \]

    +-commutative [=>]0.03

    \[ \color{blue}{\left(1 - x\right) \cdot z + x \cdot y} \]

    *-commutative [=>]0.03

    \[ \color{blue}{z \cdot \left(1 - x\right)} + x \cdot y \]

    sub-neg [=>]0.03

    \[ z \cdot \color{blue}{\left(1 + \left(-x\right)\right)} + x \cdot y \]

    distribute-rgt-in [=>]0.02

    \[ \color{blue}{\left(1 \cdot z + \left(-x\right) \cdot z\right)} + x \cdot y \]

    associate-+l+ [=>]0.02

    \[ \color{blue}{1 \cdot z + \left(\left(-x\right) \cdot z + x \cdot y\right)} \]

    *-lft-identity [=>]0.02

    \[ \color{blue}{z} + \left(\left(-x\right) \cdot z + x \cdot y\right) \]

    +-commutative [=>]0.02

    \[ \color{blue}{\left(\left(-x\right) \cdot z + x \cdot y\right) + z} \]

    *-commutative [=>]0.02

    \[ \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]

    neg-mul-1 [=>]0.02

    \[ \left(z \cdot \color{blue}{\left(-1 \cdot x\right)} + x \cdot y\right) + z \]

    associate-*r* [=>]0.02

    \[ \left(\color{blue}{\left(z \cdot -1\right) \cdot x} + x \cdot y\right) + z \]

    *-commutative [=>]0.02

    \[ \left(\left(z \cdot -1\right) \cdot x + \color{blue}{y \cdot x}\right) + z \]

    distribute-rgt-out [=>]0.03

    \[ \color{blue}{x \cdot \left(z \cdot -1 + y\right)} + z \]

    fma-def [=>]0.01

    \[ \color{blue}{\mathsf{fma}\left(x, z \cdot -1 + y, z\right)} \]

    +-commutative [=>]0.01

    \[ \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]

    *-commutative [=>]0.01

    \[ \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]

    metadata-eval [<=]0.01

    \[ \mathsf{fma}\left(x, y + \color{blue}{\left(-1\right)} \cdot z, z\right) \]

    cancel-sign-sub-inv [<=]0.01

    \[ \mathsf{fma}\left(x, \color{blue}{y - 1 \cdot z}, z\right) \]

    *-lft-identity [=>]0.01

    \[ \mathsf{fma}\left(x, y - \color{blue}{z}, z\right) \]
  3. Final simplification0.01

    \[\leadsto \mathsf{fma}\left(x, y - z, z\right) \]

Alternatives

Alternative 1
Error40.06%
Cost784
\[\begin{array}{l} \mathbf{if}\;z \leq -2.45 \cdot 10^{-123}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{-91}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;z \leq 1.1 \cdot 10^{+137}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 5.2 \cdot 10^{+162}:\\ \;\;\;\;z \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 2
Error24.86%
Cost585
\[\begin{array}{l} \mathbf{if}\;z \leq -9.2 \cdot 10^{-131} \lor \neg \left(z \leq 1.8 \cdot 10^{-98}\right):\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 3
Error21.26%
Cost585
\[\begin{array}{l} \mathbf{if}\;z \leq -8 \cdot 10^{-127} \lor \neg \left(z \leq 2.5 \cdot 10^{-96}\right):\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y - z\right)\\ \end{array} \]
Alternative 4
Error39.49%
Cost456
\[\begin{array}{l} \mathbf{if}\;z \leq -2.7 \cdot 10^{-123}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 5.9 \cdot 10^{-92}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 5
Error0.03%
Cost448
\[z + x \cdot \left(y - z\right) \]
Alternative 6
Error54.57%
Cost64
\[z \]

Error

Reproduce?

herbie shell --seed 2023090 
(FPCore (x y z)
  :name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
  :precision binary64
  (+ (* x y) (* (- 1.0 x) z)))