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

\mathbf{else}:\\
\;\;\;\;x \cdot \left(\left(z \cdot \frac{z}{y}\right) \cdot -0.5\right) + y \cdot x\\


\end{array}

Error

Target

Original24.6
Target0.6
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;y < 2.5816096488251695 \cdot 10^{-278}:\\ \;\;\;\;-x \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\sqrt{y + z} \cdot \sqrt{y - z}\right)\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if y < -3.04999999999999987e-307

    1. Initial program 24.5

      \[x \cdot \sqrt{y \cdot y - z \cdot z} \]
    2. Taylor expanded in y around -inf 3.2

      \[\leadsto x \cdot \color{blue}{\left(0.5 \cdot \frac{{z}^{2}}{y} + -1 \cdot y\right)} \]
    3. Simplified3.2

      \[\leadsto x \cdot \color{blue}{\left(\frac{0.5}{y} \cdot \left(z \cdot z\right) - y\right)} \]
      Proof
      (-.f64 (*.f64 (/.f64 1/2 y) (*.f64 z z)) y): 0 points increase in error, 0 points decrease in error
      (-.f64 (*.f64 (/.f64 1/2 y) (Rewrite<= unpow2_binary64 (pow.f64 z 2))) y): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= associate-/r/_binary64 (/.f64 1/2 (/.f64 y (pow.f64 z 2)))) y): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 1/2 (pow.f64 z 2)) y)) y): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= associate-*r/_binary64 (*.f64 1/2 (/.f64 (pow.f64 z 2) y))) y): 0 points increase in error, 0 points decrease in error
      (Rewrite<= unsub-neg_binary64 (+.f64 (*.f64 1/2 (/.f64 (pow.f64 z 2) y)) (neg.f64 y))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 1/2 (/.f64 (pow.f64 z 2) y)) (Rewrite<= mul-1-neg_binary64 (*.f64 -1 y))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr30.5

      \[\leadsto \color{blue}{{\left(\sqrt{x \cdot \mathsf{fma}\left(z, \frac{0.5}{y} \cdot z, -y\right)}\right)}^{2}} \]
    5. Taylor expanded in z around 0 64.0

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

      \[\leadsto \color{blue}{\frac{z}{\frac{y}{z}} \cdot \left(x \cdot 0.5\right) - y \cdot x} \]
      Proof
      (-.f64 (*.f64 (/.f64 z (/.f64 y z)) (*.f64 x 1/2)) (*.f64 y x)): 0 points increase in error, 0 points decrease in error
      (-.f64 (*.f64 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 z z) y)) (*.f64 x 1/2)) (*.f64 y x)): 20 points increase in error, 0 points decrease in error
      (-.f64 (*.f64 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 z 2)) y) (*.f64 x 1/2)) (*.f64 y x)): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 (/.f64 (pow.f64 z 2) y) x) 1/2)) (*.f64 y x)): 0 points increase in error, 0 points decrease in error
      (-.f64 (*.f64 (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 (pow.f64 z 2) x) y)) 1/2) (*.f64 y x)): 2 points increase in error, 0 points decrease in error
      (Rewrite=> fma-neg_binary64 (fma.f64 (/.f64 (*.f64 (pow.f64 z 2) x) y) 1/2 (neg.f64 (*.f64 y x)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (*.f64 (pow.f64 z 2) x) y) 1/2 (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (*.f64 y x)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (*.f64 (pow.f64 z 2) x) y) 1/2 (*.f64 (Rewrite<= rem-square-sqrt_binary64 (*.f64 (sqrt.f64 -1) (sqrt.f64 -1))) (*.f64 y x))): 237 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (*.f64 (pow.f64 z 2) x) y) 1/2 (*.f64 (Rewrite<= unpow2_binary64 (pow.f64 (sqrt.f64 -1) 2)) (*.f64 y x))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (/.f64 (*.f64 (pow.f64 z 2) x) y) 1/2) (*.f64 (pow.f64 (sqrt.f64 -1) 2) (*.f64 y x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/2 (/.f64 (*.f64 (pow.f64 z 2) x) y))) (*.f64 (pow.f64 (sqrt.f64 -1) 2) (*.f64 y x))): 0 points increase in error, 0 points decrease in error
    7. Applied egg-rr0.4

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

    if -3.04999999999999987e-307 < y

    1. Initial program 24.8

      \[x \cdot \sqrt{y \cdot y - z \cdot z} \]
    2. Taylor expanded in y around inf 3.3

      \[\leadsto x \cdot \color{blue}{\left(y + -0.5 \cdot \frac{{z}^{2}}{y}\right)} \]
    3. Simplified0.2

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(-0.5, \frac{z}{y} \cdot z, y\right)} \]
      Proof
      (fma.f64 -1/2 (*.f64 (/.f64 z y) z) y): 0 points increase in error, 0 points decrease in error
      (fma.f64 -1/2 (Rewrite<= associate-/r/_binary64 (/.f64 z (/.f64 y z))) y): 0 points increase in error, 0 points decrease in error
      (fma.f64 -1/2 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 z z) y)) y): 19 points increase in error, 2 points decrease in error
      (fma.f64 -1/2 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 z 2)) y) y): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 -1/2 (/.f64 (pow.f64 z 2) y)) y)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 y (*.f64 -1/2 (/.f64 (pow.f64 z 2) y)))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr0.2

      \[\leadsto \color{blue}{\left(\left(z \cdot \frac{z}{y}\right) \cdot -0.5\right) \cdot x + y \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

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

Alternatives

Alternative 1
Error0.5
Cost964
\[\begin{array}{l} \mathbf{if}\;y \leq -1.05 \cdot 10^{-298}:\\ \;\;\;\;\left(x \cdot 0.5\right) \cdot \frac{z}{\frac{y}{z}} - y \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]
Alternative 2
Error0.3
Cost964
\[\begin{array}{l} \mathbf{if}\;y \leq 0:\\ \;\;\;\;\left(x \cdot 0.5\right) \cdot \frac{z}{\frac{y}{z}} - y \cdot x\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\left(z \cdot \frac{z}{y}\right) \cdot -0.5\right) + y \cdot x\\ \end{array} \]
Alternative 3
Error0.5
Cost836
\[\begin{array}{l} \mathbf{if}\;y \leq -1.05 \cdot 10^{-298}:\\ \;\;\;\;x \cdot \left(\frac{z}{\frac{y}{z \cdot 0.5}} - y\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]
Alternative 4
Error0.6
Cost388
\[\begin{array}{l} \mathbf{if}\;y \leq 5.4 \cdot 10^{-291}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]
Alternative 5
Error29.6
Cost192
\[y \cdot x \]

Error

Reproduce

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

  :herbie-target
  (if (< y 2.5816096488251695e-278) (- (* x y)) (* x (* (sqrt (+ y z)) (sqrt (- y z)))))

  (* x (sqrt (- (* y y) (* z z)))))