
(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 4 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}
x_m = (fabs.f64 x) (FPCore (x_m y z) :precision binary64 (* 0.5 (+ y (* (+ x_m z) (/ (- x_m z) y)))))
x_m = fabs(x);
double code(double x_m, double y, double z) {
return 0.5 * (y + ((x_m + z) * ((x_m - z) / y)));
}
x_m = abs(x)
real(8) function code(x_m, y, z)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 0.5d0 * (y + ((x_m + z) * ((x_m - z) / y)))
end function
x_m = Math.abs(x);
public static double code(double x_m, double y, double z) {
return 0.5 * (y + ((x_m + z) * ((x_m - z) / y)));
}
x_m = math.fabs(x) def code(x_m, y, z): return 0.5 * (y + ((x_m + z) * ((x_m - z) / y)))
x_m = abs(x) function code(x_m, y, z) return Float64(0.5 * Float64(y + Float64(Float64(x_m + z) * Float64(Float64(x_m - z) / y)))) end
x_m = abs(x); function tmp = code(x_m, y, z) tmp = 0.5 * (y + ((x_m + z) * ((x_m - z) / y))); end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_, z_] := N[(0.5 * N[(y + N[(N[(x$95$m + z), $MachinePrecision] * N[(N[(x$95$m - z), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
0.5 \cdot \left(y + \left(x\_m + z\right) \cdot \frac{x\_m - z}{y}\right)
\end{array}
Initial program 68.8%
remove-double-neg68.8%
distribute-lft-neg-out68.8%
distribute-frac-neg268.8%
distribute-frac-neg68.8%
neg-mul-168.8%
distribute-lft-neg-out68.8%
*-commutative68.8%
distribute-lft-neg-in68.8%
times-frac68.8%
metadata-eval68.8%
metadata-eval68.8%
associate--l+68.8%
fma-define71.6%
Simplified71.6%
Taylor expanded in x around 0 82.3%
associate--l+82.3%
div-sub85.8%
Simplified85.8%
unpow285.8%
unpow285.8%
difference-of-squares90.2%
Applied egg-rr90.2%
associate-/l*99.9%
Applied egg-rr99.9%
Final simplification99.9%
x_m = (fabs.f64 x) (FPCore (x_m y z) :precision binary64 (if (or (<= y 1.8e+72) (and (not (<= y 7.5e+87)) (<= y 2.9e+109))) (* (+ x_m z) (* 0.5 (/ (- x_m z) y))) (* 0.5 y)))
x_m = fabs(x);
double code(double x_m, double y, double z) {
double tmp;
if ((y <= 1.8e+72) || (!(y <= 7.5e+87) && (y <= 2.9e+109))) {
tmp = (x_m + z) * (0.5 * ((x_m - z) / y));
} else {
tmp = 0.5 * y;
}
return tmp;
}
x_m = abs(x)
real(8) function code(x_m, y, z)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((y <= 1.8d+72) .or. (.not. (y <= 7.5d+87)) .and. (y <= 2.9d+109)) then
tmp = (x_m + z) * (0.5d0 * ((x_m - z) / y))
else
tmp = 0.5d0 * y
end if
code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y, double z) {
double tmp;
if ((y <= 1.8e+72) || (!(y <= 7.5e+87) && (y <= 2.9e+109))) {
tmp = (x_m + z) * (0.5 * ((x_m - z) / y));
} else {
tmp = 0.5 * y;
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y, z): tmp = 0 if (y <= 1.8e+72) or (not (y <= 7.5e+87) and (y <= 2.9e+109)): tmp = (x_m + z) * (0.5 * ((x_m - z) / y)) else: tmp = 0.5 * y return tmp
x_m = abs(x) function code(x_m, y, z) tmp = 0.0 if ((y <= 1.8e+72) || (!(y <= 7.5e+87) && (y <= 2.9e+109))) tmp = Float64(Float64(x_m + z) * Float64(0.5 * Float64(Float64(x_m - z) / y))); else tmp = Float64(0.5 * y); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y, z) tmp = 0.0; if ((y <= 1.8e+72) || (~((y <= 7.5e+87)) && (y <= 2.9e+109))) tmp = (x_m + z) * (0.5 * ((x_m - z) / y)); else tmp = 0.5 * y; end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_, z_] := If[Or[LessEqual[y, 1.8e+72], And[N[Not[LessEqual[y, 7.5e+87]], $MachinePrecision], LessEqual[y, 2.9e+109]]], N[(N[(x$95$m + z), $MachinePrecision] * N[(0.5 * N[(N[(x$95$m - z), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * y), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.8 \cdot 10^{+72} \lor \neg \left(y \leq 7.5 \cdot 10^{+87}\right) \land y \leq 2.9 \cdot 10^{+109}:\\
\;\;\;\;\left(x\_m + z\right) \cdot \left(0.5 \cdot \frac{x\_m - z}{y}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot y\\
\end{array}
\end{array}
if y < 1.80000000000000017e72 or 7.50000000000000014e87 < y < 2.9e109Initial program 76.7%
Taylor expanded in y around inf 79.7%
*-commutative79.7%
Simplified79.7%
unpow289.2%
unpow289.2%
difference-of-squares93.6%
Applied egg-rr83.1%
Taylor expanded in y around 0 70.4%
associate-*r/70.4%
+-commutative70.4%
Simplified70.4%
associate-/l*70.4%
*-commutative70.4%
associate-*l/73.4%
associate-*r*73.4%
+-commutative73.4%
Applied egg-rr73.4%
if 1.80000000000000017e72 < y < 7.50000000000000014e87 or 2.9e109 < y Initial program 37.3%
Taylor expanded in y around inf 73.4%
*-commutative73.4%
Simplified73.4%
Final simplification73.4%
x_m = (fabs.f64 x) (FPCore (x_m y z) :precision binary64 (if (<= y 3.2e+23) (* 0.5 (* x_m (/ x_m y))) (* 0.5 y)))
x_m = fabs(x);
double code(double x_m, double y, double z) {
double tmp;
if (y <= 3.2e+23) {
tmp = 0.5 * (x_m * (x_m / y));
} else {
tmp = 0.5 * y;
}
return tmp;
}
x_m = abs(x)
real(8) function code(x_m, y, z)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (y <= 3.2d+23) then
tmp = 0.5d0 * (x_m * (x_m / y))
else
tmp = 0.5d0 * y
end if
code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y, double z) {
double tmp;
if (y <= 3.2e+23) {
tmp = 0.5 * (x_m * (x_m / y));
} else {
tmp = 0.5 * y;
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y, z): tmp = 0 if y <= 3.2e+23: tmp = 0.5 * (x_m * (x_m / y)) else: tmp = 0.5 * y return tmp
x_m = abs(x) function code(x_m, y, z) tmp = 0.0 if (y <= 3.2e+23) tmp = Float64(0.5 * Float64(x_m * Float64(x_m / y))); else tmp = Float64(0.5 * y); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y, z) tmp = 0.0; if (y <= 3.2e+23) tmp = 0.5 * (x_m * (x_m / y)); else tmp = 0.5 * y; end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_, z_] := If[LessEqual[y, 3.2e+23], N[(0.5 * N[(x$95$m * N[(x$95$m / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * y), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.2 \cdot 10^{+23}:\\
\;\;\;\;0.5 \cdot \left(x\_m \cdot \frac{x\_m}{y}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot y\\
\end{array}
\end{array}
if y < 3.2e23Initial program 75.5%
remove-double-neg75.5%
distribute-lft-neg-out75.5%
distribute-frac-neg275.5%
distribute-frac-neg75.5%
neg-mul-175.5%
distribute-lft-neg-out75.5%
*-commutative75.5%
distribute-lft-neg-in75.5%
times-frac75.5%
metadata-eval75.5%
metadata-eval75.5%
associate--l+75.5%
fma-define78.6%
Simplified78.6%
Taylor expanded in z around inf 56.1%
add-sqr-sqrt30.9%
pow230.9%
Applied egg-rr11.5%
Taylor expanded in x around inf 14.7%
unpow214.7%
associate-*l*14.7%
sqrt-div14.7%
metadata-eval14.7%
sqrt-div14.7%
metadata-eval14.7%
un-div-inv14.7%
times-frac14.7%
*-un-lft-identity14.7%
add-sqr-sqrt34.4%
Applied egg-rr34.4%
if 3.2e23 < y Initial program 47.6%
Taylor expanded in y around inf 65.0%
*-commutative65.0%
Simplified65.0%
Final simplification41.7%
x_m = (fabs.f64 x) (FPCore (x_m y z) :precision binary64 (* 0.5 y))
x_m = fabs(x);
double code(double x_m, double y, double z) {
return 0.5 * y;
}
x_m = abs(x)
real(8) function code(x_m, y, z)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 0.5d0 * y
end function
x_m = Math.abs(x);
public static double code(double x_m, double y, double z) {
return 0.5 * y;
}
x_m = math.fabs(x) def code(x_m, y, z): return 0.5 * y
x_m = abs(x) function code(x_m, y, z) return Float64(0.5 * y) end
x_m = abs(x); function tmp = code(x_m, y, z) tmp = 0.5 * y; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_, z_] := N[(0.5 * y), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
0.5 \cdot y
\end{array}
Initial program 68.8%
Taylor expanded in y around inf 37.1%
*-commutative37.1%
Simplified37.1%
Final simplification37.1%
(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 2024073
(FPCore (x y z)
:name "Diagrams.TwoD.Apollonian:initialConfig from diagrams-contrib-1.3.0.5, A"
:precision binary64
:alt
(- (* y 0.5) (* (* (/ 0.5 y) (+ z x)) (- z x)))
(/ (- (+ (* x x) (* y y)) (* z z)) (* y 2.0)))