Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, A

Percentage Accurate: 69.1% → 99.9%
Time: 11.5s
Alternatives: 9
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ \frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (/ (- (+ (* x x) (* y y)) (* z z)) (* y 2.0)))
double code(double x, double y, double z) {
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (((x * x) + (y * y)) - (z * z)) / (y * 2.0d0)
end function
public static double code(double x, double y, double z) {
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
}
def code(x, y, z):
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z * z)) / Float64(y * 2.0))
end
function tmp = code(x, y, z)
	tmp = (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
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]
\begin{array}{l}

\\
\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2}
\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 69.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (/ (- (+ (* x x) (* y y)) (* z z)) (* y 2.0)))
double code(double x, double y, double z) {
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (((x * x) + (y * y)) - (z * z)) / (y * 2.0d0)
end function
public static double code(double x, double y, double z) {
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
}
def code(x, y, z):
	return (((x * x) + (y * y)) - (z * z)) / (y * 2.0)
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z * z)) / Float64(y * 2.0))
end
function tmp = code(x, y, z)
	tmp = (((x * x) + (y * y)) - (z * z)) / (y * 2.0);
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]
\begin{array}{l}

\\
\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2}
\end{array}

Alternative 1: 99.9% accurate, 1.2× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ 0.5 \cdot \mathsf{fma}\left(x - z\_m, \frac{z\_m + x}{y}, y\right) \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (* 0.5 (fma (- x z_m) (/ (+ z_m x) y) y)))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	return 0.5 * fma((x - z_m), ((z_m + x) / y), y);
}
z_m = abs(z)
function code(x, y, z_m)
	return Float64(0.5 * fma(Float64(x - z_m), Float64(Float64(z_m + x) / y), y))
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := N[(0.5 * N[(N[(x - z$95$m), $MachinePrecision] * N[(N[(z$95$m + x), $MachinePrecision] / y), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
z_m = \left|z\right|

\\
0.5 \cdot \mathsf{fma}\left(x - z\_m, \frac{z\_m + x}{y}, y\right)
\end{array}
Derivation
  1. Initial program 69.1%

    \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
  2. Applied rewrites91.6%

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

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
  4. Applied rewrites99.9%

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

Alternative 2: 54.7% accurate, 0.3× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\ \;\;\;\;0.5 \cdot \left(\left(-z\_m\right) \cdot \frac{z\_m}{y}\right)\\ \mathbf{elif}\;t\_0 \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(x, \frac{x}{y}, y\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(x - z\_m, \frac{z\_m}{y}, y\right)\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (let* ((t_0 (/ (- (+ (* x x) (* y y)) (* z_m z_m)) (* y 2.0))))
   (if (<= t_0 -4e-80)
     (* 0.5 (* (- z_m) (/ z_m y)))
     (if (<= t_0 INFINITY)
       (* (fma x (/ x y) y) 0.5)
       (* 0.5 (fma (- x z_m) (/ z_m y) y))))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = 0.5 * (-z_m * (z_m / y));
	} else if (t_0 <= ((double) INFINITY)) {
		tmp = fma(x, (x / y), y) * 0.5;
	} else {
		tmp = 0.5 * fma((x - z_m), (z_m / y), y);
	}
	return tmp;
}
z_m = abs(z)
function code(x, y, z_m)
	t_0 = Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z_m * z_m)) / Float64(y * 2.0))
	tmp = 0.0
	if (t_0 <= -4e-80)
		tmp = Float64(0.5 * Float64(Float64(-z_m) * Float64(z_m / y)));
	elseif (t_0 <= Inf)
		tmp = Float64(fma(x, Float64(x / y), y) * 0.5);
	else
		tmp = Float64(0.5 * fma(Float64(x - z_m), Float64(z_m / y), y));
	end
	return tmp
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := Block[{t$95$0 = N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-80], N[(0.5 * N[((-z$95$m) * N[(z$95$m / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, Infinity], N[(N[(x * N[(x / y), $MachinePrecision] + y), $MachinePrecision] * 0.5), $MachinePrecision], N[(0.5 * N[(N[(x - z$95$m), $MachinePrecision] * N[(z$95$m / y), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\
\;\;\;\;0.5 \cdot \left(\left(-z\_m\right) \cdot \frac{z\_m}{y}\right)\\

\mathbf{elif}\;t\_0 \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{x}{y}, y\right) \cdot 0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < -3.99999999999999985e-80

    1. Initial program 78.4%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites94.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

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

      \[\leadsto \frac{1}{2} \cdot \left(-1 \cdot \color{blue}{\frac{{z}^{2}}{y}}\right) \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left(\frac{{z}^{2}}{y} \cdot -1\right) \]
      2. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(\frac{{z}^{2}}{y} \cdot \left(\mathsf{neg}\left(1\right)\right)\right) \]
      3. distribute-rgt-neg-outN/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{{z}^{2}}{y} \cdot 1\right)\right) \]
      4. *-rgt-identityN/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{{z}^{2}}{y}\right)\right) \]
      5. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{z \cdot z}{y}\right)\right) \]
      6. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(z \cdot \frac{z}{y}\right)\right) \]
      7. distribute-lft-neg-inN/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\mathsf{neg}\left(z\right)\right) \cdot \frac{z}{\color{blue}{y}}\right) \]
      8. lower-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\mathsf{neg}\left(z\right)\right) \cdot \frac{z}{\color{blue}{y}}\right) \]
      9. lower-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(-z\right) \cdot \frac{z}{y}\right) \]
      10. lower-/.f6432.8

        \[\leadsto 0.5 \cdot \left(\left(-z\right) \cdot \frac{z}{y}\right) \]
    7. Applied rewrites32.8%

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

    if -3.99999999999999985e-80 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < +inf.0

    1. Initial program 76.8%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{y}} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{{x}^{2} + {y}^{2}}{y} \cdot \color{blue}{\frac{1}{2}} \]
      2. lower-*.f64N/A

        \[\leadsto \frac{{x}^{2} + {y}^{2}}{y} \cdot \color{blue}{\frac{1}{2}} \]
    4. Applied rewrites67.7%

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

    if +inf.0 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64)))

    1. Initial program 0.0%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites61.4%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(x - z, \frac{z}{\color{blue}{y}}, y\right) \]
    6. Step-by-step derivation
      1. lower-/.f6482.9

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(x - z, \frac{z}{y}, y\right) \]
    7. Applied rewrites82.9%

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

Alternative 3: 51.4% accurate, 0.6× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} \mathbf{if}\;\frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2} \leq -4 \cdot 10^{-80}:\\ \;\;\;\;0.5 \cdot \left(\left(-z\_m\right) \cdot \frac{z\_m}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, \frac{x}{y}, y\right) \cdot 0.5\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (if (<= (/ (- (+ (* x x) (* y y)) (* z_m z_m)) (* y 2.0)) -4e-80)
   (* 0.5 (* (- z_m) (/ z_m y)))
   (* (fma x (/ x y) y) 0.5)))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double tmp;
	if (((((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0)) <= -4e-80) {
		tmp = 0.5 * (-z_m * (z_m / y));
	} else {
		tmp = fma(x, (x / y), y) * 0.5;
	}
	return tmp;
}
z_m = abs(z)
function code(x, y, z_m)
	tmp = 0.0
	if (Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z_m * z_m)) / Float64(y * 2.0)) <= -4e-80)
		tmp = Float64(0.5 * Float64(Float64(-z_m) * Float64(z_m / y)));
	else
		tmp = Float64(fma(x, Float64(x / y), y) * 0.5);
	end
	return tmp
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := If[LessEqual[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision], -4e-80], N[(0.5 * N[((-z$95$m) * N[(z$95$m / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * N[(x / y), $MachinePrecision] + y), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
\mathbf{if}\;\frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2} \leq -4 \cdot 10^{-80}:\\
\;\;\;\;0.5 \cdot \left(\left(-z\_m\right) \cdot \frac{z\_m}{y}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < -3.99999999999999985e-80

    1. Initial program 78.4%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites94.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

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

      \[\leadsto \frac{1}{2} \cdot \left(-1 \cdot \color{blue}{\frac{{z}^{2}}{y}}\right) \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left(\frac{{z}^{2}}{y} \cdot -1\right) \]
      2. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(\frac{{z}^{2}}{y} \cdot \left(\mathsf{neg}\left(1\right)\right)\right) \]
      3. distribute-rgt-neg-outN/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{{z}^{2}}{y} \cdot 1\right)\right) \]
      4. *-rgt-identityN/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{{z}^{2}}{y}\right)\right) \]
      5. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{z \cdot z}{y}\right)\right) \]
      6. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(z \cdot \frac{z}{y}\right)\right) \]
      7. distribute-lft-neg-inN/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\mathsf{neg}\left(z\right)\right) \cdot \frac{z}{\color{blue}{y}}\right) \]
      8. lower-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\mathsf{neg}\left(z\right)\right) \cdot \frac{z}{\color{blue}{y}}\right) \]
      9. lower-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(-z\right) \cdot \frac{z}{y}\right) \]
      10. lower-/.f6432.8

        \[\leadsto 0.5 \cdot \left(\left(-z\right) \cdot \frac{z}{y}\right) \]
    7. Applied rewrites32.8%

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

    if -3.99999999999999985e-80 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64)))

    1. Initial program 62.3%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{y}} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{{x}^{2} + {y}^{2}}{y} \cdot \color{blue}{\frac{1}{2}} \]
      2. lower-*.f64N/A

        \[\leadsto \frac{{x}^{2} + {y}^{2}}{y} \cdot \color{blue}{\frac{1}{2}} \]
    4. Applied rewrites64.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{x}{y}, y\right) \cdot 0.5} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 42.7% accurate, 0.4× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\ \;\;\;\;0.5 \cdot \left(\left(-z\_m\right) \cdot \frac{z\_m}{y}\right)\\ \mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{x}{y} \cdot x\right) \cdot 0.5\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (let* ((t_0 (/ (- (+ (* x x) (* y y)) (* z_m z_m)) (* y 2.0))))
   (if (<= t_0 -4e-80)
     (* 0.5 (* (- z_m) (/ z_m y)))
     (if (<= t_0 4e+152) (* y 0.5) (* (* (/ x y) x) 0.5)))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = 0.5 * (-z_m * (z_m / y));
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = ((x / y) * x) * 0.5;
	}
	return tmp;
}
z_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0d0)
    if (t_0 <= (-4d-80)) then
        tmp = 0.5d0 * (-z_m * (z_m / y))
    else if (t_0 <= 4d+152) then
        tmp = y * 0.5d0
    else
        tmp = ((x / y) * x) * 0.5d0
    end if
    code = tmp
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = 0.5 * (-z_m * (z_m / y));
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = ((x / y) * x) * 0.5;
	}
	return tmp;
}
z_m = math.fabs(z)
def code(x, y, z_m):
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0)
	tmp = 0
	if t_0 <= -4e-80:
		tmp = 0.5 * (-z_m * (z_m / y))
	elif t_0 <= 4e+152:
		tmp = y * 0.5
	else:
		tmp = ((x / y) * x) * 0.5
	return tmp
z_m = abs(z)
function code(x, y, z_m)
	t_0 = Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z_m * z_m)) / Float64(y * 2.0))
	tmp = 0.0
	if (t_0 <= -4e-80)
		tmp = Float64(0.5 * Float64(Float64(-z_m) * Float64(z_m / y)));
	elseif (t_0 <= 4e+152)
		tmp = Float64(y * 0.5);
	else
		tmp = Float64(Float64(Float64(x / y) * x) * 0.5);
	end
	return tmp
end
z_m = abs(z);
function tmp_2 = code(x, y, z_m)
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	tmp = 0.0;
	if (t_0 <= -4e-80)
		tmp = 0.5 * (-z_m * (z_m / y));
	elseif (t_0 <= 4e+152)
		tmp = y * 0.5;
	else
		tmp = ((x / y) * x) * 0.5;
	end
	tmp_2 = tmp;
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := Block[{t$95$0 = N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-80], N[(0.5 * N[((-z$95$m) * N[(z$95$m / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 4e+152], N[(y * 0.5), $MachinePrecision], N[(N[(N[(x / y), $MachinePrecision] * x), $MachinePrecision] * 0.5), $MachinePrecision]]]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\
\;\;\;\;0.5 \cdot \left(\left(-z\_m\right) \cdot \frac{z\_m}{y}\right)\\

\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\
\;\;\;\;y \cdot 0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < -3.99999999999999985e-80

    1. Initial program 78.4%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites94.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

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

      \[\leadsto \frac{1}{2} \cdot \left(-1 \cdot \color{blue}{\frac{{z}^{2}}{y}}\right) \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left(\frac{{z}^{2}}{y} \cdot -1\right) \]
      2. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(\frac{{z}^{2}}{y} \cdot \left(\mathsf{neg}\left(1\right)\right)\right) \]
      3. distribute-rgt-neg-outN/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{{z}^{2}}{y} \cdot 1\right)\right) \]
      4. *-rgt-identityN/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{{z}^{2}}{y}\right)\right) \]
      5. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(\frac{z \cdot z}{y}\right)\right) \]
      6. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \left(\mathsf{neg}\left(z \cdot \frac{z}{y}\right)\right) \]
      7. distribute-lft-neg-inN/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\mathsf{neg}\left(z\right)\right) \cdot \frac{z}{\color{blue}{y}}\right) \]
      8. lower-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\mathsf{neg}\left(z\right)\right) \cdot \frac{z}{\color{blue}{y}}\right) \]
      9. lower-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(-z\right) \cdot \frac{z}{y}\right) \]
      10. lower-/.f6432.8

        \[\leadsto 0.5 \cdot \left(\left(-z\right) \cdot \frac{z}{y}\right) \]
    7. Applied rewrites32.8%

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

    if -3.99999999999999985e-80 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 4.0000000000000002e152

    1. Initial program 89.1%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot y} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\frac{1}{2}} \]
      2. *-rgt-identityN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      4. distribute-rgt-neg-inN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \frac{1}{2} \]
      7. lower-*.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
      8. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      9. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      10. distribute-rgt-neg-inN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      11. metadata-evalN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      12. *-rgt-identity56.1

        \[\leadsto y \cdot 0.5 \]
    4. Applied rewrites56.1%

      \[\leadsto \color{blue}{y \cdot 0.5} \]

    if 4.0000000000000002e152 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64)))

    1. Initial program 53.8%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites85.7%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites36.3%

      \[\leadsto \color{blue}{\left(\frac{x}{y} \cdot x\right) \cdot 0.5} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 5: 37.6% accurate, 0.4× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\ \;\;\;\;\frac{-0.5}{y} \cdot \left(z\_m \cdot z\_m\right)\\ \mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{x}{y} \cdot x\right) \cdot 0.5\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (let* ((t_0 (/ (- (+ (* x x) (* y y)) (* z_m z_m)) (* y 2.0))))
   (if (<= t_0 -4e-80)
     (* (/ -0.5 y) (* z_m z_m))
     (if (<= t_0 4e+152) (* y 0.5) (* (* (/ x y) x) 0.5)))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = (-0.5 / y) * (z_m * z_m);
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = ((x / y) * x) * 0.5;
	}
	return tmp;
}
z_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0d0)
    if (t_0 <= (-4d-80)) then
        tmp = ((-0.5d0) / y) * (z_m * z_m)
    else if (t_0 <= 4d+152) then
        tmp = y * 0.5d0
    else
        tmp = ((x / y) * x) * 0.5d0
    end if
    code = tmp
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = (-0.5 / y) * (z_m * z_m);
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = ((x / y) * x) * 0.5;
	}
	return tmp;
}
z_m = math.fabs(z)
def code(x, y, z_m):
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0)
	tmp = 0
	if t_0 <= -4e-80:
		tmp = (-0.5 / y) * (z_m * z_m)
	elif t_0 <= 4e+152:
		tmp = y * 0.5
	else:
		tmp = ((x / y) * x) * 0.5
	return tmp
z_m = abs(z)
function code(x, y, z_m)
	t_0 = Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z_m * z_m)) / Float64(y * 2.0))
	tmp = 0.0
	if (t_0 <= -4e-80)
		tmp = Float64(Float64(-0.5 / y) * Float64(z_m * z_m));
	elseif (t_0 <= 4e+152)
		tmp = Float64(y * 0.5);
	else
		tmp = Float64(Float64(Float64(x / y) * x) * 0.5);
	end
	return tmp
end
z_m = abs(z);
function tmp_2 = code(x, y, z_m)
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	tmp = 0.0;
	if (t_0 <= -4e-80)
		tmp = (-0.5 / y) * (z_m * z_m);
	elseif (t_0 <= 4e+152)
		tmp = y * 0.5;
	else
		tmp = ((x / y) * x) * 0.5;
	end
	tmp_2 = tmp;
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := Block[{t$95$0 = N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-80], N[(N[(-0.5 / y), $MachinePrecision] * N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 4e+152], N[(y * 0.5), $MachinePrecision], N[(N[(N[(x / y), $MachinePrecision] * x), $MachinePrecision] * 0.5), $MachinePrecision]]]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\
\;\;\;\;\frac{-0.5}{y} \cdot \left(z\_m \cdot z\_m\right)\\

\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\
\;\;\;\;y \cdot 0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < -3.99999999999999985e-80

    1. Initial program 78.4%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites94.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{0.5 \cdot \mathsf{fma}\left(x - z, \frac{z + x}{y}, y\right)} \]
    5. Applied rewrites81.4%

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

      \[\leadsto {z}^{2} \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{y \cdot \left(-1 \cdot \frac{x}{{y}^{2}} + \frac{x}{{y}^{2}}\right)}{z} - \frac{1}{2} \cdot \frac{1}{y}\right)} \]
    7. Applied rewrites31.5%

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

    if -3.99999999999999985e-80 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 4.0000000000000002e152

    1. Initial program 89.1%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot y} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\frac{1}{2}} \]
      2. *-rgt-identityN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      4. distribute-rgt-neg-inN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \frac{1}{2} \]
      7. lower-*.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
      8. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      9. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      10. distribute-rgt-neg-inN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      11. metadata-evalN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      12. *-rgt-identity56.1

        \[\leadsto y \cdot 0.5 \]
    4. Applied rewrites56.1%

      \[\leadsto \color{blue}{y \cdot 0.5} \]

    if 4.0000000000000002e152 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64)))

    1. Initial program 53.8%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites85.7%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites36.3%

      \[\leadsto \color{blue}{\left(\frac{x}{y} \cdot x\right) \cdot 0.5} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 6: 37.1% accurate, 0.4× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\ \;\;\;\;\frac{-0.5}{y} \cdot \left(z\_m \cdot z\_m\right)\\ \mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{0.5}{y} \cdot x\right) \cdot x\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (let* ((t_0 (/ (- (+ (* x x) (* y y)) (* z_m z_m)) (* y 2.0))))
   (if (<= t_0 -4e-80)
     (* (/ -0.5 y) (* z_m z_m))
     (if (<= t_0 4e+152) (* y 0.5) (* (* (/ 0.5 y) x) x)))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = (-0.5 / y) * (z_m * z_m);
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = ((0.5 / y) * x) * x;
	}
	return tmp;
}
z_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0d0)
    if (t_0 <= (-4d-80)) then
        tmp = ((-0.5d0) / y) * (z_m * z_m)
    else if (t_0 <= 4d+152) then
        tmp = y * 0.5d0
    else
        tmp = ((0.5d0 / y) * x) * x
    end if
    code = tmp
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = (-0.5 / y) * (z_m * z_m);
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = ((0.5 / y) * x) * x;
	}
	return tmp;
}
z_m = math.fabs(z)
def code(x, y, z_m):
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0)
	tmp = 0
	if t_0 <= -4e-80:
		tmp = (-0.5 / y) * (z_m * z_m)
	elif t_0 <= 4e+152:
		tmp = y * 0.5
	else:
		tmp = ((0.5 / y) * x) * x
	return tmp
z_m = abs(z)
function code(x, y, z_m)
	t_0 = Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z_m * z_m)) / Float64(y * 2.0))
	tmp = 0.0
	if (t_0 <= -4e-80)
		tmp = Float64(Float64(-0.5 / y) * Float64(z_m * z_m));
	elseif (t_0 <= 4e+152)
		tmp = Float64(y * 0.5);
	else
		tmp = Float64(Float64(Float64(0.5 / y) * x) * x);
	end
	return tmp
end
z_m = abs(z);
function tmp_2 = code(x, y, z_m)
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	tmp = 0.0;
	if (t_0 <= -4e-80)
		tmp = (-0.5 / y) * (z_m * z_m);
	elseif (t_0 <= 4e+152)
		tmp = y * 0.5;
	else
		tmp = ((0.5 / y) * x) * x;
	end
	tmp_2 = tmp;
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := Block[{t$95$0 = N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-80], N[(N[(-0.5 / y), $MachinePrecision] * N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 4e+152], N[(y * 0.5), $MachinePrecision], N[(N[(N[(0.5 / y), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision]]]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\
\;\;\;\;\frac{-0.5}{y} \cdot \left(z\_m \cdot z\_m\right)\\

\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\
\;\;\;\;y \cdot 0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < -3.99999999999999985e-80

    1. Initial program 78.4%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites94.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{0.5 \cdot \mathsf{fma}\left(x - z, \frac{z + x}{y}, y\right)} \]
    5. Applied rewrites81.4%

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

      \[\leadsto {z}^{2} \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{y \cdot \left(-1 \cdot \frac{x}{{y}^{2}} + \frac{x}{{y}^{2}}\right)}{z} - \frac{1}{2} \cdot \frac{1}{y}\right)} \]
    7. Applied rewrites31.5%

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

    if -3.99999999999999985e-80 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 4.0000000000000002e152

    1. Initial program 89.1%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot y} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\frac{1}{2}} \]
      2. *-rgt-identityN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      4. distribute-rgt-neg-inN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \frac{1}{2} \]
      7. lower-*.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
      8. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      9. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      10. distribute-rgt-neg-inN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      11. metadata-evalN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      12. *-rgt-identity56.1

        \[\leadsto y \cdot 0.5 \]
    4. Applied rewrites56.1%

      \[\leadsto \color{blue}{y \cdot 0.5} \]

    if 4.0000000000000002e152 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64)))

    1. Initial program 53.8%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites85.7%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites36.3%

      \[\leadsto \color{blue}{\left(\frac{x}{y} \cdot x\right) \cdot 0.5} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \left(\frac{x}{y} \cdot x\right) \cdot \color{blue}{\frac{1}{2}} \]
      2. lift-*.f64N/A

        \[\leadsto \left(\frac{x}{y} \cdot x\right) \cdot \frac{1}{2} \]
      3. lift-/.f64N/A

        \[\leadsto \left(\frac{x}{y} \cdot x\right) \cdot \frac{1}{2} \]
      4. associate-*l/N/A

        \[\leadsto \frac{x \cdot x}{y} \cdot \frac{1}{2} \]
      5. pow2N/A

        \[\leadsto \frac{{x}^{2}}{y} \cdot \frac{1}{2} \]
      6. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\frac{{x}^{2}}{y}} \]
      7. associate-*r/N/A

        \[\leadsto \frac{\frac{1}{2} \cdot {x}^{2}}{\color{blue}{y}} \]
      8. associate-*l/N/A

        \[\leadsto \frac{\frac{1}{2}}{y} \cdot \color{blue}{{x}^{2}} \]
      9. metadata-evalN/A

        \[\leadsto \frac{\frac{1}{2} \cdot 1}{y} \cdot {x}^{2} \]
      10. associate-*r/N/A

        \[\leadsto \left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot {\color{blue}{x}}^{2} \]
      11. pow2N/A

        \[\leadsto \left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot \left(x \cdot \color{blue}{x}\right) \]
      12. associate-*r*N/A

        \[\leadsto \left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right) \cdot \color{blue}{x} \]
      13. lower-*.f64N/A

        \[\leadsto \left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right) \cdot \color{blue}{x} \]
      14. lower-*.f64N/A

        \[\leadsto \left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right) \cdot x \]
      15. associate-*r/N/A

        \[\leadsto \left(\frac{\frac{1}{2} \cdot 1}{y} \cdot x\right) \cdot x \]
      16. metadata-evalN/A

        \[\leadsto \left(\frac{\frac{1}{2}}{y} \cdot x\right) \cdot x \]
      17. lower-/.f6436.3

        \[\leadsto \left(\frac{0.5}{y} \cdot x\right) \cdot x \]
    6. Applied rewrites36.3%

      \[\leadsto \left(\frac{0.5}{y} \cdot x\right) \cdot \color{blue}{x} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 7: 37.1% accurate, 0.4× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\ \;\;\;\;\frac{-0.5}{y} \cdot \left(z\_m \cdot z\_m\right)\\ \mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\ \;\;\;\;y \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot x}{y + y}\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (let* ((t_0 (/ (- (+ (* x x) (* y y)) (* z_m z_m)) (* y 2.0))))
   (if (<= t_0 -4e-80)
     (* (/ -0.5 y) (* z_m z_m))
     (if (<= t_0 4e+152) (* y 0.5) (/ (* x x) (+ y y))))))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = (-0.5 / y) * (z_m * z_m);
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = (x * x) / (y + y);
	}
	return tmp;
}
z_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0d0)
    if (t_0 <= (-4d-80)) then
        tmp = ((-0.5d0) / y) * (z_m * z_m)
    else if (t_0 <= 4d+152) then
        tmp = y * 0.5d0
    else
        tmp = (x * x) / (y + y)
    end if
    code = tmp
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
	double t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	double tmp;
	if (t_0 <= -4e-80) {
		tmp = (-0.5 / y) * (z_m * z_m);
	} else if (t_0 <= 4e+152) {
		tmp = y * 0.5;
	} else {
		tmp = (x * x) / (y + y);
	}
	return tmp;
}
z_m = math.fabs(z)
def code(x, y, z_m):
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0)
	tmp = 0
	if t_0 <= -4e-80:
		tmp = (-0.5 / y) * (z_m * z_m)
	elif t_0 <= 4e+152:
		tmp = y * 0.5
	else:
		tmp = (x * x) / (y + y)
	return tmp
z_m = abs(z)
function code(x, y, z_m)
	t_0 = Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) - Float64(z_m * z_m)) / Float64(y * 2.0))
	tmp = 0.0
	if (t_0 <= -4e-80)
		tmp = Float64(Float64(-0.5 / y) * Float64(z_m * z_m));
	elseif (t_0 <= 4e+152)
		tmp = Float64(y * 0.5);
	else
		tmp = Float64(Float64(x * x) / Float64(y + y));
	end
	return tmp
end
z_m = abs(z);
function tmp_2 = code(x, y, z_m)
	t_0 = (((x * x) + (y * y)) - (z_m * z_m)) / (y * 2.0);
	tmp = 0.0;
	if (t_0 <= -4e-80)
		tmp = (-0.5 / y) * (z_m * z_m);
	elseif (t_0 <= 4e+152)
		tmp = y * 0.5;
	else
		tmp = (x * x) / (y + y);
	end
	tmp_2 = tmp;
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := Block[{t$95$0 = N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-80], N[(N[(-0.5 / y), $MachinePrecision] * N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 4e+152], N[(y * 0.5), $MachinePrecision], N[(N[(x * x), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
t_0 := \frac{\left(x \cdot x + y \cdot y\right) - z\_m \cdot z\_m}{y \cdot 2}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-80}:\\
\;\;\;\;\frac{-0.5}{y} \cdot \left(z\_m \cdot z\_m\right)\\

\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+152}:\\
\;\;\;\;y \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{x \cdot x}{y + y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < -3.99999999999999985e-80

    1. Initial program 78.4%

      \[\frac{\left(x \cdot x + y \cdot y\right) - z \cdot z}{y \cdot 2} \]
    2. Applied rewrites94.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(y + -1 \cdot \frac{{z}^{2}}{y}\right) + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{0.5 \cdot \mathsf{fma}\left(x - z, \frac{z + x}{y}, y\right)} \]
    5. Applied rewrites81.4%

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

      \[\leadsto {z}^{2} \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{y \cdot \left(-1 \cdot \frac{x}{{y}^{2}} + \frac{x}{{y}^{2}}\right)}{z} - \frac{1}{2} \cdot \frac{1}{y}\right)} \]
    7. Applied rewrites31.5%

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

    if -3.99999999999999985e-80 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 4.0000000000000002e152

    1. Initial program 89.1%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot y} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\frac{1}{2}} \]
      2. *-rgt-identityN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      4. distribute-rgt-neg-inN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \frac{1}{2} \]
      7. lower-*.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
      8. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      9. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      10. distribute-rgt-neg-inN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      11. metadata-evalN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      12. *-rgt-identity56.1

        \[\leadsto y \cdot 0.5 \]
    4. Applied rewrites56.1%

      \[\leadsto \color{blue}{y \cdot 0.5} \]

    if 4.0000000000000002e152 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64)))

    1. Initial program 53.8%

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

      \[\leadsto \frac{\color{blue}{{x}^{2}}}{y \cdot 2} \]
    3. Step-by-step derivation
      1. pow2N/A

        \[\leadsto \frac{x \cdot \color{blue}{x}}{y \cdot 2} \]
      2. lift-*.f6433.5

        \[\leadsto \frac{x \cdot \color{blue}{x}}{y \cdot 2} \]
    4. Applied rewrites33.5%

      \[\leadsto \frac{\color{blue}{x \cdot x}}{y \cdot 2} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \frac{x \cdot x}{\color{blue}{y \cdot 2}} \]
      2. *-commutativeN/A

        \[\leadsto \frac{x \cdot x}{\color{blue}{2 \cdot y}} \]
      3. count-2-revN/A

        \[\leadsto \frac{x \cdot x}{\color{blue}{y + y}} \]
      4. lower-+.f6433.5

        \[\leadsto \frac{x \cdot x}{\color{blue}{y + y}} \]
    6. Applied rewrites33.5%

      \[\leadsto \frac{x \cdot x}{\color{blue}{y + y}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 8: 35.8% accurate, 1.6× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ \begin{array}{l} \mathbf{if}\;y \leq 2.7 \cdot 10^{+34}:\\ \;\;\;\;\frac{x \cdot x}{y + y}\\ \mathbf{else}:\\ \;\;\;\;y \cdot 0.5\\ \end{array} \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m)
 :precision binary64
 (if (<= y 2.7e+34) (/ (* x x) (+ y y)) (* y 0.5)))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	double tmp;
	if (y <= 2.7e+34) {
		tmp = (x * x) / (y + y);
	} else {
		tmp = y * 0.5;
	}
	return tmp;
}
z_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z_m
    real(8) :: tmp
    if (y <= 2.7d+34) then
        tmp = (x * x) / (y + y)
    else
        tmp = y * 0.5d0
    end if
    code = tmp
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
	double tmp;
	if (y <= 2.7e+34) {
		tmp = (x * x) / (y + y);
	} else {
		tmp = y * 0.5;
	}
	return tmp;
}
z_m = math.fabs(z)
def code(x, y, z_m):
	tmp = 0
	if y <= 2.7e+34:
		tmp = (x * x) / (y + y)
	else:
		tmp = y * 0.5
	return tmp
z_m = abs(z)
function code(x, y, z_m)
	tmp = 0.0
	if (y <= 2.7e+34)
		tmp = Float64(Float64(x * x) / Float64(y + y));
	else
		tmp = Float64(y * 0.5);
	end
	return tmp
end
z_m = abs(z);
function tmp_2 = code(x, y, z_m)
	tmp = 0.0;
	if (y <= 2.7e+34)
		tmp = (x * x) / (y + y);
	else
		tmp = y * 0.5;
	end
	tmp_2 = tmp;
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := If[LessEqual[y, 2.7e+34], N[(N[(x * x), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
z_m = \left|z\right|

\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.7 \cdot 10^{+34}:\\
\;\;\;\;\frac{x \cdot x}{y + y}\\

\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 2.7e34

    1. Initial program 76.7%

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

      \[\leadsto \frac{\color{blue}{{x}^{2}}}{y \cdot 2} \]
    3. Step-by-step derivation
      1. pow2N/A

        \[\leadsto \frac{x \cdot \color{blue}{x}}{y \cdot 2} \]
      2. lift-*.f6436.8

        \[\leadsto \frac{x \cdot \color{blue}{x}}{y \cdot 2} \]
    4. Applied rewrites36.8%

      \[\leadsto \frac{\color{blue}{x \cdot x}}{y \cdot 2} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \frac{x \cdot x}{\color{blue}{y \cdot 2}} \]
      2. *-commutativeN/A

        \[\leadsto \frac{x \cdot x}{\color{blue}{2 \cdot y}} \]
      3. count-2-revN/A

        \[\leadsto \frac{x \cdot x}{\color{blue}{y + y}} \]
      4. lower-+.f6436.8

        \[\leadsto \frac{x \cdot x}{\color{blue}{y + y}} \]
    6. Applied rewrites36.8%

      \[\leadsto \frac{x \cdot x}{\color{blue}{y + y}} \]

    if 2.7e34 < y

    1. Initial program 42.1%

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

      \[\leadsto \color{blue}{\frac{1}{2} \cdot y} \]
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\frac{1}{2}} \]
      2. *-rgt-identityN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      4. distribute-rgt-neg-inN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \frac{1}{2} \]
      7. lower-*.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
      8. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
      9. *-commutativeN/A

        \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
      10. distribute-rgt-neg-inN/A

        \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
      11. metadata-evalN/A

        \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
      12. *-rgt-identity63.6

        \[\leadsto y \cdot 0.5 \]
    4. Applied rewrites63.6%

      \[\leadsto \color{blue}{y \cdot 0.5} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 9: 33.9% accurate, 5.4× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ y \cdot 0.5 \end{array} \]
z_m = (fabs.f64 z)
(FPCore (x y z_m) :precision binary64 (* y 0.5))
z_m = fabs(z);
double code(double x, double y, double z_m) {
	return y * 0.5;
}
z_m =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z_m
    code = y * 0.5d0
end function
z_m = Math.abs(z);
public static double code(double x, double y, double z_m) {
	return y * 0.5;
}
z_m = math.fabs(z)
def code(x, y, z_m):
	return y * 0.5
z_m = abs(z)
function code(x, y, z_m)
	return Float64(y * 0.5)
end
z_m = abs(z);
function tmp = code(x, y, z_m)
	tmp = y * 0.5;
end
z_m = N[Abs[z], $MachinePrecision]
code[x_, y_, z$95$m_] := N[(y * 0.5), $MachinePrecision]
\begin{array}{l}
z_m = \left|z\right|

\\
y \cdot 0.5
\end{array}
Derivation
  1. Initial program 69.1%

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

    \[\leadsto \color{blue}{\frac{1}{2} \cdot y} \]
  3. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto y \cdot \color{blue}{\frac{1}{2}} \]
    2. *-rgt-identityN/A

      \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
    3. metadata-evalN/A

      \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
    4. distribute-rgt-neg-inN/A

      \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
    5. *-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
    6. mul-1-negN/A

      \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \frac{1}{2} \]
    7. lower-*.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
    8. mul-1-negN/A

      \[\leadsto \left(\mathsf{neg}\left(-1 \cdot y\right)\right) \cdot \frac{1}{2} \]
    9. *-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(y \cdot -1\right)\right) \cdot \frac{1}{2} \]
    10. distribute-rgt-neg-inN/A

      \[\leadsto \left(y \cdot \left(\mathsf{neg}\left(-1\right)\right)\right) \cdot \frac{1}{2} \]
    11. metadata-evalN/A

      \[\leadsto \left(y \cdot 1\right) \cdot \frac{1}{2} \]
    12. *-rgt-identity33.9

      \[\leadsto y \cdot 0.5 \]
  4. Applied rewrites33.9%

    \[\leadsto \color{blue}{y \cdot 0.5} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2025130 
(FPCore (x y z)
  :name "Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, A"
  :precision binary64
  (/ (- (+ (* x x) (* y y)) (* z z)) (* y 2.0)))