Average Error: 12.3 → 3.5
Time: 2.4s
Precision: binary64
\[\frac{x \cdot \left(y + z\right)}{z} \]
\[\begin{array}{l} \mathbf{if}\;\frac{x \cdot \left(y + z\right)}{z} \leq 10^{-32}:\\ \;\;\;\;\mathsf{fma}\left(x, \frac{y}{z}, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, \frac{x}{z}, x\right)\\ \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (+ y z)) z))
(FPCore (x y z)
 :precision binary64
 (if (<= (/ (* x (+ y z)) z) 1e-32) (fma x (/ y z) x) (fma y (/ x z) x)))
double code(double x, double y, double z) {
	return (x * (y + z)) / z;
}
double code(double x, double y, double z) {
	double tmp;
	if (((x * (y + z)) / z) <= 1e-32) {
		tmp = fma(x, (y / z), x);
	} else {
		tmp = fma(y, (x / z), x);
	}
	return tmp;
}
function code(x, y, z)
	return Float64(Float64(x * Float64(y + z)) / z)
end
function code(x, y, z)
	tmp = 0.0
	if (Float64(Float64(x * Float64(y + z)) / z) <= 1e-32)
		tmp = fma(x, Float64(y / z), x);
	else
		tmp = fma(y, Float64(x / z), x);
	end
	return tmp
end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision], 1e-32], N[(x * N[(y / z), $MachinePrecision] + x), $MachinePrecision], N[(y * N[(x / z), $MachinePrecision] + x), $MachinePrecision]]
\frac{x \cdot \left(y + z\right)}{z}
\begin{array}{l}
\mathbf{if}\;\frac{x \cdot \left(y + z\right)}{z} \leq 10^{-32}:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{y}{z}, x\right)\\

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


\end{array}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original12.3
Target3.2
Herbie3.5
\[\frac{x}{\frac{z}{y + z}} \]

Derivation

  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 x (+.f64 y z)) z) < 1.00000000000000006e-32

    1. Initial program 11.0

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

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

    if 1.00000000000000006e-32 < (/.f64 (*.f64 x (+.f64 y z)) z)

    1. Initial program 15.3

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

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

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

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

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

Reproduce

herbie shell --seed 2022170 
(FPCore (x y z)
  :name "Numeric.SpecFunctions:choose from math-functions-0.1.5.2"
  :precision binary64

  :herbie-target
  (/ x (/ z (+ y z)))

  (/ (* x (+ y z)) z))