Average Error: 10.5 → 0.1
Time: 3.0s
Precision: binary64
\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;z \leq -2.8830491714104415 \cdot 10^{-21}:\\ \;\;\;\;\left(\left(\frac{y}{z} + \frac{1}{z}\right) + -1\right) \cdot x\\ \mathbf{elif}\;z \leq 4.689163988824187 \cdot 10^{-21}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{z}, x, \frac{x}{z}\right) - x\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z)
 :precision binary64
 (if (<= z -2.8830491714104415e-21)
   (* (+ (+ (/ y z) (/ 1.0 z)) -1.0) x)
   (if (<= z 4.689163988824187e-21)
     (/ (fma x y x) z)
     (- (fma (/ y z) x (/ x z)) 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 (z <= -2.8830491714104415e-21) {
		tmp = (((y / z) + (1.0 / z)) + -1.0) * x;
	} else if (z <= 4.689163988824187e-21) {
		tmp = fma(x, y, x) / z;
	} else {
		tmp = fma((y / z), x, (x / z)) - 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 (z <= -2.8830491714104415e-21)
		tmp = Float64(Float64(Float64(Float64(y / z) + Float64(1.0 / z)) + -1.0) * x);
	elseif (z <= 4.689163988824187e-21)
		tmp = Float64(fma(x, y, x) / z);
	else
		tmp = Float64(fma(Float64(y / z), x, Float64(x / z)) - 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[z, -2.8830491714104415e-21], N[(N[(N[(N[(y / z), $MachinePrecision] + N[(1.0 / z), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[z, 4.689163988824187e-21], N[(N[(x * y + x), $MachinePrecision] / z), $MachinePrecision], N[(N[(N[(y / z), $MachinePrecision] * x + N[(x / z), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision]]]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;z \leq -2.8830491714104415 \cdot 10^{-21}:\\
\;\;\;\;\left(\left(\frac{y}{z} + \frac{1}{z}\right) + -1\right) \cdot x\\

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

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{z}, x, \frac{x}{z}\right) - x\\


\end{array}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original10.5
Target0.5
Herbie0.1
\[\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 < -2.88304917141044147e-21

    1. Initial program 16.4

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

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

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

    if -2.88304917141044147e-21 < z < 4.6891639888241872e-21

    1. Initial program 0.1

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

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

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

      \[\leadsto \color{blue}{\frac{y \cdot x + x}{z}} \]
    5. Simplified0.1

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

    if 4.6891639888241872e-21 < z

    1. Initial program 16.1

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

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

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

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

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

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

Reproduce

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