
(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);
}
real(8) function code(x, y, z)
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}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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);
}
real(8) function code(x, y, z)
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}
y\_m = (fabs.f64 y)
y\_s = (copysign.f64 #s(literal 1 binary64) y)
(FPCore (y_s x y_m z)
:precision binary64
(let* ((t_0 (* 0.5 (- y_m (* (/ z y_m) z))))
(t_1 (/ (- (+ (* y_m y_m) (* x x)) (* z z)) (* 2.0 y_m))))
(*
y_s
(if (<= t_1 0.0)
t_0
(if (<= t_1 INFINITY) (* (fma (/ x y_m) x y_m) 0.5) t_0)))))y\_m = fabs(y);
y\_s = copysign(1.0, y);
double code(double y_s, double x, double y_m, double z) {
double t_0 = 0.5 * (y_m - ((z / y_m) * z));
double t_1 = (((y_m * y_m) + (x * x)) - (z * z)) / (2.0 * y_m);
double tmp;
if (t_1 <= 0.0) {
tmp = t_0;
} else if (t_1 <= ((double) INFINITY)) {
tmp = fma((x / y_m), x, y_m) * 0.5;
} else {
tmp = t_0;
}
return y_s * tmp;
}
y\_m = abs(y) y\_s = copysign(1.0, y) function code(y_s, x, y_m, z) t_0 = Float64(0.5 * Float64(y_m - Float64(Float64(z / y_m) * z))) t_1 = Float64(Float64(Float64(Float64(y_m * y_m) + Float64(x * x)) - Float64(z * z)) / Float64(2.0 * y_m)) tmp = 0.0 if (t_1 <= 0.0) tmp = t_0; elseif (t_1 <= Inf) tmp = Float64(fma(Float64(x / y_m), x, y_m) * 0.5); else tmp = t_0; end return Float64(y_s * tmp) end
y\_m = N[Abs[y], $MachinePrecision]
y\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[y$95$s_, x_, y$95$m_, z_] := Block[{t$95$0 = N[(0.5 * N[(y$95$m - N[(N[(z / y$95$m), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(y$95$m * y$95$m), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision] / N[(2.0 * y$95$m), $MachinePrecision]), $MachinePrecision]}, N[(y$95$s * If[LessEqual[t$95$1, 0.0], t$95$0, If[LessEqual[t$95$1, Infinity], N[(N[(N[(x / y$95$m), $MachinePrecision] * x + y$95$m), $MachinePrecision] * 0.5), $MachinePrecision], t$95$0]]), $MachinePrecision]]]
\begin{array}{l}
y\_m = \left|y\right|
\\
y\_s = \mathsf{copysign}\left(1, y\right)
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(y\_m - \frac{z}{y\_m} \cdot z\right)\\
t_1 := \frac{\left(y\_m \cdot y\_m + x \cdot x\right) - z \cdot z}{2 \cdot y\_m}\\
y\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq 0:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{y\_m}, x, y\_m\right) \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
\end{array}
if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 0.0 or +inf.0 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) Initial program 78.8%
Taylor expanded in x around 0
*-commutativeN/A
div-subN/A
unpow2N/A
associate-/l*N/A
*-inversesN/A
*-rgt-identityN/A
lower-*.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-fracN/A
unpow2N/A
distribute-rgt-neg-inN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-neg.f6495.0
Applied rewrites95.0%
Applied rewrites95.0%
if 0.0 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < +inf.0Initial program 72.5%
Taylor expanded in z around 0
*-commutativeN/A
*-lft-identityN/A
*-inversesN/A
associate-*l/N/A
associate-*r/N/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
associate-*l/N/A
unpow2N/A
associate-/l*N/A
*-inversesN/A
*-rgt-identityN/A
Applied rewrites99.6%
Final simplification97.5%
y\_m = (fabs.f64 y)
y\_s = (copysign.f64 #s(literal 1 binary64) y)
(FPCore (y_s x y_m z)
:precision binary64
(let* ((t_0 (* -0.5 (* (/ z y_m) z)))
(t_1 (/ (- (+ (* y_m y_m) (* x x)) (* z z)) (* 2.0 y_m))))
(*
y_s
(if (<= t_1 0.0)
t_0
(if (<= t_1 5e+152)
(* 0.5 y_m)
(if (<= t_1 INFINITY) (* (* (/ 0.5 y_m) x) x) t_0))))))y\_m = fabs(y);
y\_s = copysign(1.0, y);
double code(double y_s, double x, double y_m, double z) {
double t_0 = -0.5 * ((z / y_m) * z);
double t_1 = (((y_m * y_m) + (x * x)) - (z * z)) / (2.0 * y_m);
double tmp;
if (t_1 <= 0.0) {
tmp = t_0;
} else if (t_1 <= 5e+152) {
tmp = 0.5 * y_m;
} else if (t_1 <= ((double) INFINITY)) {
tmp = ((0.5 / y_m) * x) * x;
} else {
tmp = t_0;
}
return y_s * tmp;
}
y\_m = Math.abs(y);
y\_s = Math.copySign(1.0, y);
public static double code(double y_s, double x, double y_m, double z) {
double t_0 = -0.5 * ((z / y_m) * z);
double t_1 = (((y_m * y_m) + (x * x)) - (z * z)) / (2.0 * y_m);
double tmp;
if (t_1 <= 0.0) {
tmp = t_0;
} else if (t_1 <= 5e+152) {
tmp = 0.5 * y_m;
} else if (t_1 <= Double.POSITIVE_INFINITY) {
tmp = ((0.5 / y_m) * x) * x;
} else {
tmp = t_0;
}
return y_s * tmp;
}
y\_m = math.fabs(y) y\_s = math.copysign(1.0, y) def code(y_s, x, y_m, z): t_0 = -0.5 * ((z / y_m) * z) t_1 = (((y_m * y_m) + (x * x)) - (z * z)) / (2.0 * y_m) tmp = 0 if t_1 <= 0.0: tmp = t_0 elif t_1 <= 5e+152: tmp = 0.5 * y_m elif t_1 <= math.inf: tmp = ((0.5 / y_m) * x) * x else: tmp = t_0 return y_s * tmp
y\_m = abs(y) y\_s = copysign(1.0, y) function code(y_s, x, y_m, z) t_0 = Float64(-0.5 * Float64(Float64(z / y_m) * z)) t_1 = Float64(Float64(Float64(Float64(y_m * y_m) + Float64(x * x)) - Float64(z * z)) / Float64(2.0 * y_m)) tmp = 0.0 if (t_1 <= 0.0) tmp = t_0; elseif (t_1 <= 5e+152) tmp = Float64(0.5 * y_m); elseif (t_1 <= Inf) tmp = Float64(Float64(Float64(0.5 / y_m) * x) * x); else tmp = t_0; end return Float64(y_s * tmp) end
y\_m = abs(y); y\_s = sign(y) * abs(1.0); function tmp_2 = code(y_s, x, y_m, z) t_0 = -0.5 * ((z / y_m) * z); t_1 = (((y_m * y_m) + (x * x)) - (z * z)) / (2.0 * y_m); tmp = 0.0; if (t_1 <= 0.0) tmp = t_0; elseif (t_1 <= 5e+152) tmp = 0.5 * y_m; elseif (t_1 <= Inf) tmp = ((0.5 / y_m) * x) * x; else tmp = t_0; end tmp_2 = y_s * tmp; end
y\_m = N[Abs[y], $MachinePrecision]
y\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[y$95$s_, x_, y$95$m_, z_] := Block[{t$95$0 = N[(-0.5 * N[(N[(z / y$95$m), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(y$95$m * y$95$m), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] - N[(z * z), $MachinePrecision]), $MachinePrecision] / N[(2.0 * y$95$m), $MachinePrecision]), $MachinePrecision]}, N[(y$95$s * If[LessEqual[t$95$1, 0.0], t$95$0, If[LessEqual[t$95$1, 5e+152], N[(0.5 * y$95$m), $MachinePrecision], If[LessEqual[t$95$1, Infinity], N[(N[(N[(0.5 / y$95$m), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision], t$95$0]]]), $MachinePrecision]]]
\begin{array}{l}
y\_m = \left|y\right|
\\
y\_s = \mathsf{copysign}\left(1, y\right)
\\
\begin{array}{l}
t_0 := -0.5 \cdot \left(\frac{z}{y\_m} \cdot z\right)\\
t_1 := \frac{\left(y\_m \cdot y\_m + x \cdot x\right) - z \cdot z}{2 \cdot y\_m}\\
y\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq 0:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq 5 \cdot 10^{+152}:\\
\;\;\;\;0.5 \cdot y\_m\\
\mathbf{elif}\;t\_1 \leq \infty:\\
\;\;\;\;\left(\frac{0.5}{y\_m} \cdot x\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
\end{array}
if (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 0.0 or +inf.0 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) Initial program 65.9%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f648.8
Applied rewrites8.8%
Taylor expanded in z around inf
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6482.5
Applied rewrites82.5%
if 0.0 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < 5e152Initial program 99.6%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6475.6
Applied rewrites75.6%
if 5e152 < (/.f64 (-.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 z z)) (*.f64 y #s(literal 2 binary64))) < +inf.0Initial program 59.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6440.8
Applied rewrites40.8%
Taylor expanded in x around inf
associate-*r/N/A
associate-*l/N/A
metadata-evalN/A
associate-*r/N/A
unpow2N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6461.6
Applied rewrites61.6%
Final simplification72.7%
(FPCore (x y z) :precision binary64 (- (* y 0.5) (* (* (/ 0.5 y) (+ z x)) (- z x))))
double code(double x, double y, double z) {
return (y * 0.5) - (((0.5 / y) * (z + x)) * (z - 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 * 0.5d0) - (((0.5d0 / y) * (z + x)) * (z - x))
end function
public static double code(double x, double y, double z) {
return (y * 0.5) - (((0.5 / y) * (z + x)) * (z - x));
}
def code(x, y, z): return (y * 0.5) - (((0.5 / y) * (z + x)) * (z - x))
function code(x, y, z) return Float64(Float64(y * 0.5) - Float64(Float64(Float64(0.5 / y) * Float64(z + x)) * Float64(z - x))) end
function tmp = code(x, y, z) tmp = (y * 0.5) - (((0.5 / y) * (z + x)) * (z - x)); end
code[x_, y_, z_] := N[(N[(y * 0.5), $MachinePrecision] - N[(N[(N[(0.5 / y), $MachinePrecision] * N[(z + x), $MachinePrecision]), $MachinePrecision] * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot 0.5 - \left(\frac{0.5}{y} \cdot \left(z + x\right)\right) \cdot \left(z - x\right)
\end{array}
herbie shell --seed 2024230
(FPCore (x y z)
:name "Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, A"
:precision binary64
:alt
(! :herbie-platform default (- (* y 1/2) (* (* (/ 1/2 y) (+ z x)) (- z x))))
(/ (- (+ (* x x) (* y y)) (* z z)) (* y 2.0)))