Average Error: 10.1 → 0.3
Time: 5.4s
Precision: binary64
\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -4.6985478038612766 \cdot 10^{-117}:\\ \;\;\;\;\left(y + 1\right) \cdot \frac{x}{z} - x\\ \mathbf{elif}\;x \leq 1.5045753576773437 \cdot 10^{-82}:\\ \;\;\;\;\frac{x \cdot \left(y + 1\right)}{z} - x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{z}, x, \frac{x}{z}\right) - x\\ \end{array} \]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;x \leq -4.6985478038612766 \cdot 10^{-117}:\\
\;\;\;\;\left(y + 1\right) \cdot \frac{x}{z} - x\\

\mathbf{elif}\;x \leq 1.5045753576773437 \cdot 10^{-82}:\\
\;\;\;\;\frac{x \cdot \left(y + 1\right)}{z} - x\\

\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 (<= x -4.6985478038612766e-117)
   (- (* (+ y 1.0) (/ x z)) x)
   (if (<= x 1.5045753576773437e-82)
     (- (/ (* x (+ y 1.0)) z) x)
     (- (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 (x <= -4.6985478038612766e-117) {
		tmp = ((y + 1.0) * (x / z)) - x;
	} else if (x <= 1.5045753576773437e-82) {
		tmp = ((x * (y + 1.0)) / z) - x;
	} else {
		tmp = fma((y / z), x, (x / z)) - x;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original10.1
Target0.4
Herbie0.3
\[\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 x < -4.69854780386127662e-117

    1. Initial program 16.7

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

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

      \[\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} \]
    5. Taylor expanded in x around 0 1.1

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

      \[\leadsto \left(\color{blue}{y \cdot \frac{1}{z}} + \frac{1}{z}\right) \cdot x - x \]
    7. Applied distribute-lft1-in_binary641.1

      \[\leadsto \color{blue}{\left(\left(y + 1\right) \cdot \frac{1}{z}\right)} \cdot x - x \]
    8. Applied associate-*l*_binary640.2

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

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

    if -4.69854780386127662e-117 < x < 1.5045753576773437e-82

    1. Initial program 0.2

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

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

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

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

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

      \[\leadsto \left(\frac{y}{\color{blue}{1 \cdot z}} + \frac{1}{z}\right) \cdot x - x \]
    7. Applied add-cube-cbrt_binary648.2

      \[\leadsto \left(\frac{\color{blue}{\left(\sqrt[3]{y} \cdot \sqrt[3]{y}\right) \cdot \sqrt[3]{y}}}{1 \cdot z} + \frac{1}{z}\right) \cdot x - x \]
    8. Applied times-frac_binary648.2

      \[\leadsto \left(\color{blue}{\frac{\sqrt[3]{y} \cdot \sqrt[3]{y}}{1} \cdot \frac{\sqrt[3]{y}}{z}} + \frac{1}{z}\right) \cdot x - x \]
    9. Applied fma-def_binary648.2

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

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

    if 1.5045753576773437e-82 < x

    1. Initial program 18.4

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

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

      \[\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} \]
    5. Taylor expanded in x around 0 0.7

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

      \[\leadsto \left(\frac{y}{\color{blue}{1 \cdot z}} + \frac{1}{z}\right) \cdot x - x \]
    7. Applied add-cube-cbrt_binary641.0

      \[\leadsto \left(\frac{\color{blue}{\left(\sqrt[3]{y} \cdot \sqrt[3]{y}\right) \cdot \sqrt[3]{y}}}{1 \cdot z} + \frac{1}{z}\right) \cdot x - x \]
    8. Applied times-frac_binary641.0

      \[\leadsto \left(\color{blue}{\frac{\sqrt[3]{y} \cdot \sqrt[3]{y}}{1} \cdot \frac{\sqrt[3]{y}}{z}} + \frac{1}{z}\right) \cdot x - x \]
    9. Applied fma-def_binary641.0

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

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

      \[\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.3

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

Reproduce

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