?

Average Error: 15.7% → 0.23%
Time: 7.7s
Precision: binary64
Cost: 7112

?

\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;z \leq -1.5 \cdot 10^{+42}:\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{elif}\;z \leq 5 \cdot 10^{-37}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{y + \left(1 - z\right)}{z}\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z)
 :precision binary64
 (if (<= z -1.5e+42)
   (- (* (/ y z) x) x)
   (if (<= z 5e-37) (- (/ (fma x y x) z) x) (* x (/ (+ y (- 1.0 z)) z)))))
double code(double x, double y, double z) {
	return (x * ((y - z) + 1.0)) / z;
}
double code(double x, double y, double z) {
	double tmp;
	if (z <= -1.5e+42) {
		tmp = ((y / z) * x) - x;
	} else if (z <= 5e-37) {
		tmp = (fma(x, y, x) / z) - x;
	} else {
		tmp = x * ((y + (1.0 - z)) / z);
	}
	return tmp;
}
function code(x, y, z)
	return Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z)
end
function code(x, y, z)
	tmp = 0.0
	if (z <= -1.5e+42)
		tmp = Float64(Float64(Float64(y / z) * x) - x);
	elseif (z <= 5e-37)
		tmp = Float64(Float64(fma(x, y, x) / z) - x);
	else
		tmp = Float64(x * Float64(Float64(y + Float64(1.0 - z)) / z));
	end
	return tmp
end
code[x_, y_, z_] := N[(N[(x * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[z, -1.5e+42], N[(N[(N[(y / z), $MachinePrecision] * x), $MachinePrecision] - x), $MachinePrecision], If[LessEqual[z, 5e-37], N[(N[(N[(x * y + x), $MachinePrecision] / z), $MachinePrecision] - x), $MachinePrecision], N[(x * N[(N[(y + N[(1.0 - z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]]]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;z \leq -1.5 \cdot 10^{+42}:\\
\;\;\;\;\frac{y}{z} \cdot x - x\\

\mathbf{elif}\;z \leq 5 \cdot 10^{-37}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\

\mathbf{else}:\\
\;\;\;\;x \cdot \frac{y + \left(1 - z\right)}{z}\\


\end{array}

Error?

Target

Original15.7%
Target0.65%
Herbie0.23%
\[\begin{array}{l} \mathbf{if}\;x < -2.71483106713436 \cdot 10^{-162}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \mathbf{elif}\;x < 3.874108816439546 \cdot 10^{-197}:\\ \;\;\;\;\left(x \cdot \left(\left(y - z\right) + 1\right)\right) \cdot \frac{1}{z}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \end{array} \]

Derivation?

  1. Split input into 3 regimes
  2. if z < -1.50000000000000014e42

    1. Initial program 30.13

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Simplified9.95

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

      [Start]30.13

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

      associate-*r/ [<=]0.1

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

      +-commutative [=>]0.1

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

      associate-+r- [=>]0.1

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

      div-sub [=>]0.09

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

      *-inverses [=>]0.09

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

      distribute-rgt-out-- [<=]0.08

      \[ \color{blue}{\frac{1 + y}{z} \cdot x - 1 \cdot x} \]

      *-lft-identity [=>]0.08

      \[ \frac{1 + y}{z} \cdot x - \color{blue}{x} \]

      *-commutative [=>]0.08

      \[ \color{blue}{x \cdot \frac{1 + y}{z}} - x \]

      associate-*r/ [=>]9.95

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

      *-commutative [=>]9.95

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

      +-commutative [=>]9.95

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

      distribute-lft1-in [<=]9.95

      \[ \frac{\color{blue}{y \cdot x + x}}{z} - x \]

      *-commutative [=>]9.95

      \[ \frac{\color{blue}{x \cdot y} + x}{z} - x \]

      fma-def [=>]9.95

      \[ \frac{\color{blue}{\mathsf{fma}\left(x, y, x\right)}}{z} - x \]
    3. Taylor expanded in y around inf 9.95

      \[\leadsto \color{blue}{\frac{y \cdot x}{z}} - x \]
    4. Simplified0.08

      \[\leadsto \color{blue}{\frac{y}{z} \cdot x} - x \]
      Proof

      [Start]9.95

      \[ \frac{y \cdot x}{z} - x \]

      associate-*l/ [<=]0.08

      \[ \color{blue}{\frac{y}{z} \cdot x} - x \]

    if -1.50000000000000014e42 < z < 4.9999999999999997e-37

    1. Initial program 0.51

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Simplified0.36

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

      [Start]0.51

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

      associate-*r/ [<=]13.35

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

      +-commutative [=>]13.35

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

      associate-+r- [=>]13.35

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

      div-sub [=>]13.34

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

      *-inverses [=>]13.34

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

      distribute-rgt-out-- [<=]13.34

      \[ \color{blue}{\frac{1 + y}{z} \cdot x - 1 \cdot x} \]

      *-lft-identity [=>]13.34

      \[ \frac{1 + y}{z} \cdot x - \color{blue}{x} \]

      *-commutative [=>]13.34

      \[ \color{blue}{x \cdot \frac{1 + y}{z}} - x \]

      associate-*r/ [=>]0.37

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

      *-commutative [=>]0.37

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

      +-commutative [=>]0.37

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

      distribute-lft1-in [<=]0.37

      \[ \frac{\color{blue}{y \cdot x + x}}{z} - x \]

      *-commutative [=>]0.37

      \[ \frac{\color{blue}{x \cdot y} + x}{z} - x \]

      fma-def [=>]0.36

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

    if 4.9999999999999997e-37 < z

    1. Initial program 23.02

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
    2. Applied egg-rr0.18

      \[\leadsto \color{blue}{\frac{y - \left(z + -1\right)}{z} \cdot x} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.23

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1.5 \cdot 10^{+42}:\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{elif}\;z \leq 5 \cdot 10^{-37}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{y + \left(1 - z\right)}{z}\\ \end{array} \]

Alternatives

Alternative 1
Error31.75%
Cost984
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;z \leq -19:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -9.5 \cdot 10^{-224}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq -1.58 \cdot 10^{-291}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 10^{-101}:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{elif}\;z \leq 3.5 \cdot 10^{-59}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 1:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 2
Error17.94%
Cost849
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;y \leq -1 \cdot 10^{+166}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -4 \cdot 10^{+134}:\\ \;\;\;\;-x\\ \mathbf{elif}\;y \leq -180000000000 \lor \neg \left(y \leq 7.6 \cdot 10^{+90}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} - x\\ \end{array} \]
Alternative 3
Error17.97%
Cost848
\[\begin{array}{l} t_0 := y \cdot \frac{x}{z}\\ \mathbf{if}\;y \leq -1.25 \cdot 10^{+166}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -3.5 \cdot 10^{+134}:\\ \;\;\;\;-x\\ \mathbf{elif}\;y \leq -140000000000:\\ \;\;\;\;\frac{y}{\frac{z}{x}}\\ \mathbf{elif}\;y \leq 10^{+91}:\\ \;\;\;\;\frac{x}{z} - x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error0.24%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -4 \cdot 10^{-10} \lor \neg \left(z \leq 5 \cdot 10^{-37}\right):\\ \;\;\;\;x \cdot \frac{y + \left(1 - z\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x + y \cdot x}{z}\\ \end{array} \]
Alternative 5
Error0.18%
Cost841
\[\begin{array}{l} \mathbf{if}\;z \leq -3800000000000 \lor \neg \left(z \leq 2 \cdot 10^{-37}\right):\\ \;\;\;\;x \cdot \frac{y + \left(1 - z\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}\\ \end{array} \]
Alternative 6
Error14.18%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -820 \lor \neg \left(z \leq 108000000\right):\\ \;\;\;\;\frac{x}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot \left(y + 1\right)\\ \end{array} \]
Alternative 7
Error1.46%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -19 \lor \neg \left(z \leq 1\right):\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{z} \cdot \left(y + 1\right)\\ \end{array} \]
Alternative 8
Error1.45%
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -19 \lor \neg \left(z \leq 1\right):\\ \;\;\;\;\frac{y}{z} \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\frac{x + y \cdot x}{z}\\ \end{array} \]
Alternative 9
Error30.61%
Cost456
\[\begin{array}{l} \mathbf{if}\;z \leq -19:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 1:\\ \;\;\;\;\frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
Alternative 10
Error52.75%
Cost128
\[-x \]

Error

Reproduce?

herbie shell --seed 2023102 
(FPCore (x y z)
  :name "Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< x -2.71483106713436e-162) (- (* (+ 1.0 y) (/ x z)) x) (if (< x 3.874108816439546e-197) (* (* x (+ (- y z) 1.0)) (/ 1.0 z)) (- (* (+ 1.0 y) (/ x z)) x)))

  (/ (* x (+ (- y z) 1.0)) z))