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

Percentage Accurate: 67.1% → 99.5%
Time: 4.1s
Alternatives: 4
Speedup: 18.0×

Specification

?
\[\begin{array}{l} \\ x \cdot \sqrt{y \cdot y - z \cdot z} \end{array} \]
(FPCore (x y z) :precision binary64 (* x (sqrt (- (* y y) (* z z)))))
double code(double x, double y, double z) {
	return x * sqrt(((y * y) - (z * z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * sqrt(((y * y) - (z * z)))
end function
public static double code(double x, double y, double z) {
	return x * Math.sqrt(((y * y) - (z * z)));
}
def code(x, y, z):
	return x * math.sqrt(((y * y) - (z * z)))
function code(x, y, z)
	return Float64(x * sqrt(Float64(Float64(y * y) - Float64(z * z))))
end
function tmp = code(x, y, z)
	tmp = x * sqrt(((y * y) - (z * z)));
end
code[x_, y_, z_] := N[(x * N[Sqrt[N[(N[(y * y), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \sqrt{y \cdot y - z \cdot z}
\end{array}

Sampling outcomes in binary64 precision:

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 4 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: 67.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \sqrt{y \cdot y - z \cdot z} \end{array} \]
(FPCore (x y z) :precision binary64 (* x (sqrt (- (* y y) (* z z)))))
double code(double x, double y, double z) {
	return x * sqrt(((y * y) - (z * z)));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * sqrt(((y * y) - (z * z)))
end function
public static double code(double x, double y, double z) {
	return x * Math.sqrt(((y * y) - (z * z)));
}
def code(x, y, z):
	return x * math.sqrt(((y * y) - (z * z)))
function code(x, y, z)
	return Float64(x * sqrt(Float64(Float64(y * y) - Float64(z * z))))
end
function tmp = code(x, y, z)
	tmp = x * sqrt(((y * y) - (z * z)));
end
code[x_, y_, z_] := N[(x * N[Sqrt[N[(N[(y * y), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \sqrt{y \cdot y - z \cdot z}
\end{array}

Alternative 1: 99.5% accurate, 8.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{z}{\frac{y}{z}}\\ \mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\ \;\;\;\;x \cdot \left(0.5 \cdot t_0 - y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + t_0 \cdot -0.5\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (/ z (/ y z))))
   (if (<= y -1e-262) (* x (- (* 0.5 t_0) y)) (* x (+ y (* t_0 -0.5))))))
double code(double x, double y, double z) {
	double t_0 = z / (y / z);
	double tmp;
	if (y <= -1e-262) {
		tmp = x * ((0.5 * t_0) - y);
	} else {
		tmp = x * (y + (t_0 * -0.5));
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = z / (y / z)
    if (y <= (-1d-262)) then
        tmp = x * ((0.5d0 * t_0) - y)
    else
        tmp = x * (y + (t_0 * (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = z / (y / z);
	double tmp;
	if (y <= -1e-262) {
		tmp = x * ((0.5 * t_0) - y);
	} else {
		tmp = x * (y + (t_0 * -0.5));
	}
	return tmp;
}
def code(x, y, z):
	t_0 = z / (y / z)
	tmp = 0
	if y <= -1e-262:
		tmp = x * ((0.5 * t_0) - y)
	else:
		tmp = x * (y + (t_0 * -0.5))
	return tmp
function code(x, y, z)
	t_0 = Float64(z / Float64(y / z))
	tmp = 0.0
	if (y <= -1e-262)
		tmp = Float64(x * Float64(Float64(0.5 * t_0) - y));
	else
		tmp = Float64(x * Float64(y + Float64(t_0 * -0.5)));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = z / (y / z);
	tmp = 0.0;
	if (y <= -1e-262)
		tmp = x * ((0.5 * t_0) - y);
	else
		tmp = x * (y + (t_0 * -0.5));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(z / N[(y / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1e-262], N[(x * N[(N[(0.5 * t$95$0), $MachinePrecision] - y), $MachinePrecision]), $MachinePrecision], N[(x * N[(y + N[(t$95$0 * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{z}{\frac{y}{z}}\\
\mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\
\;\;\;\;x \cdot \left(0.5 \cdot t_0 - y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.00000000000000001e-262

    1. Initial program 67.8%

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

      \[\leadsto x \cdot \color{blue}{\left(0.5 \cdot \frac{{z}^{2}}{y} + -1 \cdot y\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg92.8%

        \[\leadsto x \cdot \left(0.5 \cdot \frac{{z}^{2}}{y} + \color{blue}{\left(-y\right)}\right) \]
      2. unsub-neg92.8%

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

        \[\leadsto x \cdot \left(0.5 \cdot \frac{\color{blue}{z \cdot z}}{y} - y\right) \]
      4. associate-/l*99.8%

        \[\leadsto x \cdot \left(0.5 \cdot \color{blue}{\frac{z}{\frac{y}{z}}} - y\right) \]
    4. Simplified99.8%

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

    if -1.00000000000000001e-262 < y

    1. Initial program 63.4%

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

      \[\leadsto x \cdot \color{blue}{\left(y + -0.5 \cdot \frac{{z}^{2}}{y}\right)} \]
    3. Step-by-step derivation
      1. unpow292.1%

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

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

      \[\leadsto x \cdot \left(y + \color{blue}{-0.5 \cdot \frac{{z}^{2}}{y}}\right) \]
    6. Step-by-step derivation
      1. *-commutative92.1%

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

        \[\leadsto x \cdot \left(y + \frac{\color{blue}{z \cdot z}}{y} \cdot -0.5\right) \]
      3. associate-/l*99.5%

        \[\leadsto x \cdot \left(y + \color{blue}{\frac{z}{\frac{y}{z}}} \cdot -0.5\right) \]
    7. Simplified99.5%

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

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

Alternative 2: 99.2% accurate, 8.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + \frac{z}{\frac{y}{z}} \cdot -0.5\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -1e-262) (* y (- x)) (* x (+ y (* (/ z (/ y z)) -0.5)))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -1e-262) {
		tmp = y * -x;
	} else {
		tmp = x * (y + ((z / (y / z)) * -0.5));
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y <= (-1d-262)) then
        tmp = y * -x
    else
        tmp = x * (y + ((z / (y / z)) * (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -1e-262) {
		tmp = y * -x;
	} else {
		tmp = x * (y + ((z / (y / z)) * -0.5));
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -1e-262:
		tmp = y * -x
	else:
		tmp = x * (y + ((z / (y / z)) * -0.5))
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -1e-262)
		tmp = Float64(y * Float64(-x));
	else
		tmp = Float64(x * Float64(y + Float64(Float64(z / Float64(y / z)) * -0.5)));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -1e-262)
		tmp = y * -x;
	else
		tmp = x * (y + ((z / (y / z)) * -0.5));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -1e-262], N[(y * (-x)), $MachinePrecision], N[(x * N[(y + N[(N[(z / N[(y / z), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\
\;\;\;\;y \cdot \left(-x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.00000000000000001e-262

    1. Initial program 67.8%

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    3. Step-by-step derivation
      1. associate-*r*99.6%

        \[\leadsto \color{blue}{\left(-1 \cdot y\right) \cdot x} \]
      2. mul-1-neg99.6%

        \[\leadsto \color{blue}{\left(-y\right)} \cdot x \]
    4. Simplified99.6%

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

    if -1.00000000000000001e-262 < y

    1. Initial program 63.4%

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

      \[\leadsto x \cdot \color{blue}{\left(y + -0.5 \cdot \frac{{z}^{2}}{y}\right)} \]
    3. Step-by-step derivation
      1. unpow292.1%

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

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

      \[\leadsto x \cdot \left(y + \color{blue}{-0.5 \cdot \frac{{z}^{2}}{y}}\right) \]
    6. Step-by-step derivation
      1. *-commutative92.1%

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

        \[\leadsto x \cdot \left(y + \frac{\color{blue}{z \cdot z}}{y} \cdot -0.5\right) \]
      3. associate-/l*99.5%

        \[\leadsto x \cdot \left(y + \color{blue}{\frac{z}{\frac{y}{z}}} \cdot -0.5\right) \]
    7. Simplified99.5%

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

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

Alternative 3: 99.1% accurate, 18.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
(FPCore (x y z) :precision binary64 (if (<= y -1e-262) (* y (- x)) (* y x)))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -1e-262) {
		tmp = y * -x;
	} else {
		tmp = y * x;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y <= (-1d-262)) then
        tmp = y * -x
    else
        tmp = y * x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -1e-262) {
		tmp = y * -x;
	} else {
		tmp = y * x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -1e-262:
		tmp = y * -x
	else:
		tmp = y * x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -1e-262)
		tmp = Float64(y * Float64(-x));
	else
		tmp = Float64(y * x);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -1e-262)
		tmp = y * -x;
	else
		tmp = y * x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -1e-262], N[(y * (-x)), $MachinePrecision], N[(y * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\
\;\;\;\;y \cdot \left(-x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.00000000000000001e-262

    1. Initial program 67.8%

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    3. Step-by-step derivation
      1. associate-*r*99.6%

        \[\leadsto \color{blue}{\left(-1 \cdot y\right) \cdot x} \]
      2. mul-1-neg99.6%

        \[\leadsto \color{blue}{\left(-y\right)} \cdot x \]
    4. Simplified99.6%

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

    if -1.00000000000000001e-262 < y

    1. Initial program 63.4%

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

      \[\leadsto \color{blue}{y \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-262}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]

Alternative 4: 50.6% accurate, 36.3× speedup?

\[\begin{array}{l} \\ y \cdot x \end{array} \]
(FPCore (x y z) :precision binary64 (* y x))
double code(double x, double y, double z) {
	return y * x;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = y * x
end function
public static double code(double x, double y, double z) {
	return y * x;
}
def code(x, y, z):
	return y * x
function code(x, y, z)
	return Float64(y * x)
end
function tmp = code(x, y, z)
	tmp = y * x;
end
code[x_, y_, z_] := N[(y * x), $MachinePrecision]
\begin{array}{l}

\\
y \cdot x
\end{array}
Derivation
  1. Initial program 65.7%

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

    \[\leadsto \color{blue}{y \cdot x} \]
  3. Final simplification51.9%

    \[\leadsto y \cdot x \]

Developer target: 99.3% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \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} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (< y 2.5816096488251695e-278)
   (- (* x y))
   (* x (* (sqrt (+ y z)) (sqrt (- y z))))))
double code(double x, double y, double z) {
	double tmp;
	if (y < 2.5816096488251695e-278) {
		tmp = -(x * y);
	} else {
		tmp = x * (sqrt((y + z)) * sqrt((y - z)));
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y < 2.5816096488251695d-278) then
        tmp = -(x * y)
    else
        tmp = x * (sqrt((y + z)) * sqrt((y - z)))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y < 2.5816096488251695e-278) {
		tmp = -(x * y);
	} else {
		tmp = x * (Math.sqrt((y + z)) * Math.sqrt((y - z)));
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y < 2.5816096488251695e-278:
		tmp = -(x * y)
	else:
		tmp = x * (math.sqrt((y + z)) * math.sqrt((y - z)))
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y < 2.5816096488251695e-278)
		tmp = Float64(-Float64(x * y));
	else
		tmp = Float64(x * Float64(sqrt(Float64(y + z)) * sqrt(Float64(y - z))));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y < 2.5816096488251695e-278)
		tmp = -(x * y);
	else
		tmp = x * (sqrt((y + z)) * sqrt((y - z)));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Less[y, 2.5816096488251695e-278], (-N[(x * y), $MachinePrecision]), N[(x * N[(N[Sqrt[N[(y + z), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(y - z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\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}
\end{array}

Reproduce

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