Average Error: 10.2 → 3.0
Time: 3.6s
Precision: binary64
\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;y \leq 1.63726535631151 \cdot 10^{+93}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, x, x\right)}{z} - x\\ \mathbf{elif}\;y \leq 4.786042295204834 \cdot 10^{+237}:\\ \;\;\;\;y \cdot \frac{x}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, y - z, x\right) \cdot \frac{1}{z}\\ \end{array} \]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;y \leq 1.63726535631151 \cdot 10^{+93}:\\
\;\;\;\;\frac{\mathsf{fma}\left(y, x, x\right)}{z} - x\\

\mathbf{elif}\;y \leq 4.786042295204834 \cdot 10^{+237}:\\
\;\;\;\;y \cdot \frac{x}{z} - x\\

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


\end{array}
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z)
 :precision binary64
 (if (<= y 1.63726535631151e+93)
   (- (/ (fma y x x) z) x)
   (if (<= y 4.786042295204834e+237)
     (- (* y (/ x z)) x)
     (* (fma x (- y z) x) (/ 1.0 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 (y <= 1.63726535631151e+93) {
		tmp = (fma(y, x, x) / z) - x;
	} else if (y <= 4.786042295204834e+237) {
		tmp = (y * (x / z)) - x;
	} else {
		tmp = fma(x, (y - z), x) * (1.0 / z);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original10.2
Target0.5
Herbie3.0
\[\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 y < 1.63726535631151e93

    1. Initial program 9.9

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

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

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

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

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

    if 1.63726535631151e93 < y < 4.78604229520483372e237

    1. Initial program 11.5

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{y \cdot x}{z}} - x \]
    7. Simplified4.6

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

    if 4.78604229520483372e237 < y

    1. Initial program 13.6

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

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

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

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

Reproduce

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