Average Error: 10.2 → 1.9
Time: 3.7s
Precision: binary64
\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \leq 2 \cdot 10^{+297}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{-1 - y}{z \cdot \sqrt[3]{-1}} - x\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z)
 :precision binary64
 (if (<= (/ (* x (+ (- y z) 1.0)) z) 2e+297)
   (- (/ (fma x y x) z) x)
   (- (* x (/ (- -1.0 y) (* z (cbrt -1.0)))) x)))
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 (((x * ((y - z) + 1.0)) / z) <= 2e+297) {
		tmp = (fma(x, y, x) / z) - x;
	} else {
		tmp = (x * ((-1.0 - y) / (z * cbrt(-1.0)))) - x;
	}
	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 (Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z) <= 2e+297)
		tmp = Float64(Float64(fma(x, y, x) / z) - x);
	else
		tmp = Float64(Float64(x * Float64(Float64(-1.0 - y) / Float64(z * cbrt(-1.0)))) - x);
	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[N[(N[(x * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision], 2e+297], N[(N[(N[(x * y + x), $MachinePrecision] / z), $MachinePrecision] - x), $MachinePrecision], N[(N[(x * N[(N[(-1.0 - y), $MachinePrecision] / N[(z * N[Power[-1.0, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision]]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \leq 2 \cdot 10^{+297}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\

\mathbf{else}:\\
\;\;\;\;x \cdot \frac{-1 - y}{z \cdot \sqrt[3]{-1}} - x\\


\end{array}

Error

Target

Original10.2
Target0.4
Herbie1.9
\[\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 2 regimes
  2. if (/.f64 (*.f64 x (+.f64 (-.f64 y z) 1)) z) < 2e297

    1. Initial program 5.8

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

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

    if 2e297 < (/.f64 (*.f64 x (+.f64 (-.f64 y z) 1)) z)

    1. Initial program 58.4

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x} \]
    3. Applied egg-rr18.0

      \[\leadsto \color{blue}{\frac{1}{{\left(\sqrt[3]{z}\right)}^{2}} \cdot \frac{\mathsf{fma}\left(x, y, x\right)}{\sqrt[3]{z}}} - x \]
    4. Taylor expanded in x around 0 40.9

      \[\leadsto \frac{1}{{\left(\sqrt[3]{z}\right)}^{2}} \cdot \color{blue}{\left(\left(\left(1 + y\right) \cdot x\right) \cdot {\left(\frac{1}{z}\right)}^{0.3333333333333333}\right)} - x \]
    5. Simplified18.0

      \[\leadsto \frac{1}{{\left(\sqrt[3]{z}\right)}^{2}} \cdot \color{blue}{\left(\mathsf{fma}\left(y, x, x\right) \cdot \sqrt[3]{\frac{1}{z}}\right)} - x \]
    6. Taylor expanded in z around -inf 17.9

      \[\leadsto \color{blue}{-1 \cdot \frac{y \cdot x + x}{\sqrt[3]{-1} \cdot z}} - x \]
    7. Simplified1.5

      \[\leadsto \color{blue}{x \cdot \frac{-1 - y}{z \cdot \sqrt[3]{-1}}} - x \]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \leq 2 \cdot 10^{+297}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{-1 - y}{z \cdot \sqrt[3]{-1}} - x\\ \end{array} \]

Reproduce

herbie shell --seed 2022210 
(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))