Average Error: 10.7 → 1.8
Time: 2.8s
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 1.7035116322509249 \cdot 10^{-37}:\\ \;\;\;\;x \cdot \left(-1 + \frac{y + 1}{z}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{z}, y, \frac{x}{z}\right) - x\\ \end{array} \]
\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 1.7035116322509249 \cdot 10^{-37}:\\
\;\;\;\;x \cdot \left(-1 + \frac{y + 1}{z}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, y, \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 (<= (/ (* x (+ (- y z) 1.0)) z) 1.7035116322509249e-37)
   (* x (+ -1.0 (/ (+ y 1.0) z)))
   (- (fma (/ x z) y (/ 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 (((x * ((y - z) + 1.0)) / z) <= 1.7035116322509249e-37) {
		tmp = x * (-1.0 + ((y + 1.0) / z));
	} else {
		tmp = fma((x / z), y, (x / z)) - x;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original10.7
Target0.5
Herbie1.8
\[\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) < 1.70351163225092485e-37

    1. Initial program 8.0

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

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

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

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

      \[\leadsto \color{blue}{\left(\left(\frac{y}{z} + \frac{1}{z}\right) - 1\right) \cdot x} \]
    6. Applied *-un-lft-identity_binary642.7

      \[\leadsto \left(\left(\frac{y}{z} + \frac{1}{z}\right) - 1\right) \cdot \color{blue}{\left(1 \cdot x\right)} \]
    7. Applied associate-*r*_binary642.7

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

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

    if 1.70351163225092485e-37 < (/.f64 (*.f64 x (+.f64 (-.f64 y z) 1)) z)

    1. Initial program 15.9

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

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

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

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

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

Reproduce

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