Average Error: 28.9 → 7.6
Time: 8.7s
Precision: binary64
Cost: 7176
\[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
\[\begin{array}{l} \mathbf{if}\;z \leq -6.4 \cdot 10^{+53}:\\ \;\;\;\;-0.5 \cdot \left(z \cdot \frac{z}{y} - y\right)\\ \mathbf{elif}\;z \leq 1.2481875845995409 \cdot 10^{-26}:\\ \;\;\;\;0.5 \cdot \left(y + x \cdot \frac{x}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(\frac{z}{y}, -z, y\right)\\ \end{array} \]
(FPCore (x y z)
 :precision binary64
 (/ (- (+ (* x x) (* y y)) (* z z)) (* y 2.0)))
(FPCore (x y z)
 :precision binary64
 (if (<= z -6.4e+53)
   (* -0.5 (- (* z (/ z y)) y))
   (if (<= z 1.2481875845995409e-26)
     (* 0.5 (+ y (* x (/ x y))))
     (* 0.5 (fma (/ z y) (- z) y)))))
double code(double x, double y, double z) {
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
}
double code(double x, double y, double z) {
	double tmp;
	if (z <= -6.4e+53) {
		tmp = -0.5 * ((z * (z / y)) - y);
	} else if (z <= 1.2481875845995409e-26) {
		tmp = 0.5 * (y + (x * (x / y)));
	} else {
		tmp = 0.5 * fma((z / y), -z, y);
	}
	return tmp;
}
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z * z)) / Float64(y * 2.0))
end
function code(x, y, z)
	tmp = 0.0
	if (z <= -6.4e+53)
		tmp = Float64(-0.5 * Float64(Float64(z * Float64(z / y)) - y));
	elseif (z <= 1.2481875845995409e-26)
		tmp = Float64(0.5 * Float64(y + Float64(x * Float64(x / y))));
	else
		tmp = Float64(0.5 * fma(Float64(z / y), Float64(-z), y));
	end
	return tmp
end
code[x_, y_, z_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[z, -6.4e+53], N[(-0.5 * N[(N[(z * N[(z / y), $MachinePrecision]), $MachinePrecision] - y), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 1.2481875845995409e-26], N[(0.5 * N[(y + N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[(z / y), $MachinePrecision] * (-z) + y), $MachinePrecision]), $MachinePrecision]]]
\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2}
\begin{array}{l}
\mathbf{if}\;z \leq -6.4 \cdot 10^{+53}:\\
\;\;\;\;-0.5 \cdot \left(z \cdot \frac{z}{y} - y\right)\\

\mathbf{elif}\;z \leq 1.2481875845995409 \cdot 10^{-26}:\\
\;\;\;\;0.5 \cdot \left(y + x \cdot \frac{x}{y}\right)\\

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


\end{array}

Error

Target

Original28.9
Target0.2
Herbie7.6
\[y \cdot 0.5 - \left(\frac{0.5}{y} \cdot \left(z + x\right)\right) \cdot \left(z - x\right) \]

Derivation

  1. Split input into 3 regimes
  2. if z < -6.4e53

    1. Initial program 43.5

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

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

      \[\leadsto -0.5 \cdot \color{blue}{\left(\frac{x + z}{y} \cdot \left(z - x\right) - y\right)} \]
    4. Applied egg-rr0.2

      \[\leadsto -0.5 \cdot \left(\color{blue}{\frac{z - x}{\frac{y}{x + z}}} - y\right) \]
    5. Taylor expanded in z around inf 29.1

      \[\leadsto -0.5 \cdot \left(\color{blue}{\frac{{z}^{2}}{y}} - y\right) \]
    6. Simplified5.9

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

    if -6.4e53 < z < 1.24818758459954088e-26

    1. Initial program 24.0

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

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

      \[\leadsto -0.5 \cdot \color{blue}{\left(\frac{x + z}{y} \cdot \left(z - x\right) - y\right)} \]
    4. Taylor expanded in x around inf 14.2

      \[\leadsto -0.5 \cdot \left(\color{blue}{-1 \cdot \frac{{x}^{2}}{y}} - y\right) \]
    5. Simplified8.0

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

    if 1.24818758459954088e-26 < z

    1. Initial program 36.0

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Taylor expanded in x around 0 40.1

      \[\leadsto \color{blue}{0.5 \cdot \frac{{y}^{2} - {z}^{2}}{y}} \]
    3. Simplified7.7

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

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

Alternatives

Alternative 1
Error14.9
Cost1356
\[\begin{array}{l} t_0 := -0.5 \cdot \left(z \cdot \frac{z}{y} - y\right)\\ \mathbf{if}\;x \cdot x \leq 1000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{+19}:\\ \;\;\;\;0.5 \cdot \frac{x}{\frac{y}{x}}\\ \mathbf{elif}\;x \cdot x \leq 10^{+266}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{\frac{\frac{y}{x}}{x}}\\ \end{array} \]
Alternative 2
Error10.4
Cost1104
\[\begin{array}{l} t_0 := 0.5 \cdot \left(y + \frac{x \cdot x}{y}\right)\\ \mathbf{if}\;x \leq -1 \cdot 10^{+152}:\\ \;\;\;\;\frac{0.5}{\frac{\frac{y}{x}}{x}}\\ \mathbf{elif}\;x \leq -6.195987470923665 \cdot 10^{-110}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 5.208812741092519 \cdot 10^{-25}:\\ \;\;\;\;-0.5 \cdot \left(z \cdot \frac{z}{y} - y\right)\\ \mathbf{elif}\;x \leq 3.5 \cdot 10^{+153}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;y \cdot 0.5\\ \end{array} \]
Alternative 3
Error7.6
Cost840
\[\begin{array}{l} t_0 := -0.5 \cdot \left(z \cdot \frac{z}{y} - y\right)\\ \mathbf{if}\;z \leq -6.4 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 1.2481875845995409 \cdot 10^{-26}:\\ \;\;\;\;0.5 \cdot \left(y + x \cdot \frac{x}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error23.6
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -6.1 \cdot 10^{-30}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{elif}\;y \leq 1.8 \cdot 10^{-84}:\\ \;\;\;\;\frac{0.5}{\frac{y}{x \cdot x}}\\ \mathbf{else}:\\ \;\;\;\;y \cdot 0.5\\ \end{array} \]
Alternative 5
Error23.0
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -6.1 \cdot 10^{-30}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{elif}\;y \leq 1.8 \cdot 10^{-84}:\\ \;\;\;\;\frac{0.5}{\frac{\frac{y}{x}}{x}}\\ \mathbf{else}:\\ \;\;\;\;y \cdot 0.5\\ \end{array} \]
Alternative 6
Error23.0
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -6.1 \cdot 10^{-30}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{elif}\;y \leq 1.8 \cdot 10^{-84}:\\ \;\;\;\;0.5 \cdot \frac{x}{\frac{y}{x}}\\ \mathbf{else}:\\ \;\;\;\;y \cdot 0.5\\ \end{array} \]
Alternative 7
Error27.3
Cost192
\[y \cdot 0.5 \]

Error

Reproduce

herbie shell --seed 2022228 
(FPCore (x y z)
  :name "Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, A"
  :precision binary64

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

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