
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -0.6) (- (* 3.0 (* (pow (/ p_m x) 3.0) (/ (sqrt 0.5) (sqrt 2.0)))) (/ p_m x)) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p_m 2.0) x)))))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) {
tmp = (3.0 * (pow((p_m / x), 3.0) * (sqrt(0.5) / sqrt(2.0)))) - (p_m / x);
} else {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p_m * 2.0), x)))));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if ((x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) {
tmp = (3.0 * (Math.pow((p_m / x), 3.0) * (Math.sqrt(0.5) / Math.sqrt(2.0)))) - (p_m / x);
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p_m * 2.0), x)))));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if (x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6: tmp = (3.0 * (math.pow((p_m / x), 3.0) * (math.sqrt(0.5) / math.sqrt(2.0)))) - (p_m / x) else: tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p_m * 2.0), x))))) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -0.6) tmp = Float64(Float64(3.0 * Float64((Float64(p_m / x) ^ 3.0) * Float64(sqrt(0.5) / sqrt(2.0)))) - Float64(p_m / x)); else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p_m * 2.0), x))))); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) tmp = (3.0 * (((p_m / x) ^ 3.0) * (sqrt(0.5) / sqrt(2.0)))) - (p_m / x); else tmp = sqrt((0.5 * (1.0 + (x / hypot((p_m * 2.0), x))))); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.6], N[(N[(3.0 * N[(N[Power[N[(p$95$m / x), $MachinePrecision], 3.0], $MachinePrecision] * N[(N[Sqrt[0.5], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(p$95$m / x), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p$95$m * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p_m \cdot \left(4 \cdot p_m\right) + x \cdot x}} \leq -0.6:\\
\;\;\;\;3 \cdot \left({\left(\frac{p_m}{x}\right)}^{3} \cdot \frac{\sqrt{0.5}}{\sqrt{2}}\right) - \frac{p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p_m \cdot 2, x\right)}\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.599999999999999978Initial program 14.4%
add-sqr-sqrt14.4%
hypot-def14.4%
associate-*l*14.4%
sqrt-prod14.4%
metadata-eval14.4%
sqrt-unprod9.4%
add-sqr-sqrt14.4%
Applied egg-rr14.4%
Taylor expanded in x around -inf 39.5%
+-commutative39.5%
mul-1-neg39.5%
unsub-neg39.5%
associate-*r/39.5%
times-frac48.5%
distribute-rgt-out48.5%
metadata-eval48.5%
associate-/l*48.5%
Simplified48.5%
Taylor expanded in p around 0 48.5%
+-commutative48.5%
mul-1-neg48.5%
unsub-neg48.5%
times-frac48.5%
cube-div52.3%
associate-/l*52.3%
associate-/r*52.6%
Simplified52.6%
associate-/l/52.3%
metadata-eval52.3%
metadata-eval52.3%
sqrt-undiv52.3%
sqrt-unprod53.2%
sqrt-undiv53.2%
metadata-eval53.2%
metadata-eval53.2%
metadata-eval53.2%
metadata-eval53.2%
div-inv53.2%
metadata-eval53.2%
*-commutative53.2%
*-un-lft-identity53.2%
expm1-log1p-u22.1%
expm1-udef3.6%
Applied egg-rr3.6%
expm1-def22.1%
expm1-log1p53.2%
Simplified53.2%
if -0.599999999999999978 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.8%
add-sqr-sqrt99.8%
hypot-def99.8%
associate-*l*99.8%
sqrt-prod99.8%
metadata-eval99.8%
sqrt-unprod57.5%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification89.8%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -0.6) (/ (- p_m) x) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p_m 2.0) x)))))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) {
tmp = -p_m / x;
} else {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p_m * 2.0), x)))));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if ((x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) {
tmp = -p_m / x;
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p_m * 2.0), x)))));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if (x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6: tmp = -p_m / x else: tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p_m * 2.0), x))))) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -0.6) tmp = Float64(Float64(-p_m) / x); else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p_m * 2.0), x))))); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) tmp = -p_m / x; else tmp = sqrt((0.5 * (1.0 + (x / hypot((p_m * 2.0), x))))); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.6], N[((-p$95$m) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p$95$m * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p_m \cdot \left(4 \cdot p_m\right) + x \cdot x}} \leq -0.6:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p_m \cdot 2, x\right)}\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.599999999999999978Initial program 14.4%
add-sqr-sqrt14.4%
hypot-def14.4%
associate-*l*14.4%
sqrt-prod14.4%
metadata-eval14.4%
sqrt-unprod9.4%
add-sqr-sqrt14.4%
Applied egg-rr14.4%
Taylor expanded in x around -inf 39.5%
+-commutative39.5%
mul-1-neg39.5%
unsub-neg39.5%
associate-*r/39.5%
times-frac48.5%
distribute-rgt-out48.5%
metadata-eval48.5%
associate-/l*48.5%
Simplified48.5%
Taylor expanded in p around 0 52.3%
mul-1-neg52.3%
associate-/l*52.3%
distribute-neg-frac52.3%
associate-/r*52.5%
Simplified52.5%
associate-/l/52.3%
metadata-eval52.3%
metadata-eval52.3%
sqrt-undiv52.3%
sqrt-unprod53.2%
sqrt-undiv53.2%
metadata-eval53.2%
metadata-eval53.2%
metadata-eval53.2%
metadata-eval53.2%
div-inv53.2%
metadata-eval53.2%
*-commutative53.2%
*-un-lft-identity53.2%
expm1-log1p-u22.1%
expm1-udef3.6%
Applied egg-rr3.6%
expm1-def22.1%
expm1-log1p53.2%
Simplified53.2%
if -0.599999999999999978 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.8%
add-sqr-sqrt99.8%
hypot-def99.8%
associate-*l*99.8%
sqrt-prod99.8%
metadata-eval99.8%
sqrt-unprod57.5%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification89.7%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= p_m 3.6e-257) 1.0 (if (<= p_m 1.52e-238) (/ (- p_m) x) (if (<= p_m 4e-40) 1.0 (sqrt 0.5)))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (p_m <= 3.6e-257) {
tmp = 1.0;
} else if (p_m <= 1.52e-238) {
tmp = -p_m / x;
} else if (p_m <= 4e-40) {
tmp = 1.0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (p_m <= 3.6d-257) then
tmp = 1.0d0
else if (p_m <= 1.52d-238) then
tmp = -p_m / x
else if (p_m <= 4d-40) then
tmp = 1.0d0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (p_m <= 3.6e-257) {
tmp = 1.0;
} else if (p_m <= 1.52e-238) {
tmp = -p_m / x;
} else if (p_m <= 4e-40) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if p_m <= 3.6e-257: tmp = 1.0 elif p_m <= 1.52e-238: tmp = -p_m / x elif p_m <= 4e-40: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (p_m <= 3.6e-257) tmp = 1.0; elseif (p_m <= 1.52e-238) tmp = Float64(Float64(-p_m) / x); elseif (p_m <= 4e-40) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (p_m <= 3.6e-257) tmp = 1.0; elseif (p_m <= 1.52e-238) tmp = -p_m / x; elseif (p_m <= 4e-40) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[p$95$m, 3.6e-257], 1.0, If[LessEqual[p$95$m, 1.52e-238], N[((-p$95$m) / x), $MachinePrecision], If[LessEqual[p$95$m, 4e-40], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;p_m \leq 3.6 \cdot 10^{-257}:\\
\;\;\;\;1\\
\mathbf{elif}\;p_m \leq 1.52 \cdot 10^{-238}:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{elif}\;p_m \leq 4 \cdot 10^{-40}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 3.60000000000000007e-257 or 1.5200000000000001e-238 < p < 3.9999999999999997e-40Initial program 77.1%
Taylor expanded in x around inf 44.4%
if 3.60000000000000007e-257 < p < 1.5200000000000001e-238Initial program 36.6%
add-sqr-sqrt36.6%
hypot-def36.6%
associate-*l*36.6%
sqrt-prod36.6%
metadata-eval36.6%
sqrt-unprod36.6%
add-sqr-sqrt36.6%
Applied egg-rr36.6%
Taylor expanded in x around -inf 33.3%
+-commutative33.3%
mul-1-neg33.3%
unsub-neg33.3%
associate-*r/33.3%
times-frac65.8%
distribute-rgt-out65.8%
metadata-eval65.8%
associate-/l*66.1%
Simplified66.1%
Taylor expanded in p around 0 98.3%
mul-1-neg98.3%
associate-/l*99.0%
distribute-neg-frac99.0%
associate-/r*99.0%
Simplified99.0%
associate-/l/99.0%
metadata-eval99.0%
metadata-eval99.0%
sqrt-undiv99.0%
sqrt-unprod100.0%
sqrt-undiv100.0%
metadata-eval100.0%
metadata-eval100.0%
metadata-eval100.0%
metadata-eval100.0%
div-inv100.0%
metadata-eval100.0%
*-commutative100.0%
*-un-lft-identity100.0%
expm1-log1p-u66.7%
expm1-udef0.7%
Applied egg-rr0.7%
expm1-def66.7%
expm1-log1p100.0%
Simplified100.0%
if 3.9999999999999997e-40 < p Initial program 92.0%
Taylor expanded in x around 0 86.8%
Final simplification57.8%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= p_m 1.5e-82) (/ (- p_m) x) (sqrt 0.5)))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (p_m <= 1.5e-82) {
tmp = -p_m / x;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (p_m <= 1.5d-82) then
tmp = -p_m / x
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (p_m <= 1.5e-82) {
tmp = -p_m / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if p_m <= 1.5e-82: tmp = -p_m / x else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (p_m <= 1.5e-82) tmp = Float64(Float64(-p_m) / x); else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (p_m <= 1.5e-82) tmp = -p_m / x; else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[p$95$m, 1.5e-82], N[((-p$95$m) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;p_m \leq 1.5 \cdot 10^{-82}:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 1.4999999999999999e-82Initial program 74.9%
add-sqr-sqrt74.9%
hypot-def74.9%
associate-*l*74.9%
sqrt-prod74.9%
metadata-eval74.9%
sqrt-unprod22.6%
add-sqr-sqrt74.9%
Applied egg-rr74.9%
Taylor expanded in x around -inf 10.8%
+-commutative10.8%
mul-1-neg10.8%
unsub-neg10.8%
associate-*r/10.8%
times-frac14.2%
distribute-rgt-out14.2%
metadata-eval14.2%
associate-/l*14.2%
Simplified14.2%
Taylor expanded in p around 0 16.4%
mul-1-neg16.4%
associate-/l*16.4%
distribute-neg-frac16.4%
associate-/r*16.4%
Simplified16.4%
associate-/l/16.0%
metadata-eval16.0%
metadata-eval16.0%
sqrt-undiv16.0%
sqrt-unprod16.2%
sqrt-undiv16.2%
metadata-eval16.2%
metadata-eval16.2%
metadata-eval16.2%
metadata-eval16.2%
div-inv16.2%
metadata-eval16.2%
*-commutative16.2%
*-un-lft-identity16.2%
expm1-log1p-u8.5%
expm1-udef2.3%
Applied egg-rr2.9%
expm1-def8.5%
expm1-log1p16.2%
Simplified16.6%
if 1.4999999999999999e-82 < p Initial program 93.0%
Taylor expanded in x around 0 82.1%
Final simplification39.1%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -5e-310) (/ (- p_m) x) (/ 1.0 (/ x p_m))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p_m / x;
} else {
tmp = 1.0 / (x / p_m);
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = -p_m / x
else
tmp = 1.0d0 / (x / p_m)
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p_m / x;
} else {
tmp = 1.0 / (x / p_m);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -5e-310: tmp = -p_m / x else: tmp = 1.0 / (x / p_m) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -5e-310) tmp = Float64(Float64(-p_m) / x); else tmp = Float64(1.0 / Float64(x / p_m)); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -5e-310) tmp = -p_m / x; else tmp = 1.0 / (x / p_m); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -5e-310], N[((-p$95$m) / x), $MachinePrecision], N[(1.0 / N[(x / p$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{p_m}}\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 61.6%
add-sqr-sqrt61.6%
hypot-def61.6%
associate-*l*61.6%
sqrt-prod61.6%
metadata-eval61.6%
sqrt-unprod34.6%
add-sqr-sqrt61.6%
Applied egg-rr61.6%
Taylor expanded in x around -inf 18.4%
+-commutative18.4%
mul-1-neg18.4%
unsub-neg18.4%
associate-*r/18.4%
times-frac22.8%
distribute-rgt-out22.8%
metadata-eval22.8%
associate-/l*22.8%
Simplified22.8%
Taylor expanded in p around 0 25.4%
mul-1-neg25.4%
associate-/l*25.4%
distribute-neg-frac25.4%
associate-/r*25.5%
Simplified25.5%
associate-/l/24.6%
metadata-eval24.6%
metadata-eval24.6%
sqrt-undiv24.6%
sqrt-unprod25.0%
sqrt-undiv25.0%
metadata-eval25.0%
metadata-eval25.0%
metadata-eval25.0%
metadata-eval25.0%
div-inv25.0%
metadata-eval25.0%
*-commutative25.0%
*-un-lft-identity25.0%
expm1-log1p-u10.5%
expm1-udef2.1%
Applied egg-rr2.3%
expm1-def10.5%
expm1-log1p25.0%
Simplified25.8%
if -4.999999999999985e-310 < x Initial program 99.7%
+-commutative99.7%
add-cube-cbrt99.7%
fma-def99.7%
Applied egg-rr100.0%
Taylor expanded in x around -inf 1.8%
mul-1-neg1.8%
distribute-rgt-neg-in1.8%
distribute-rgt-out1.8%
metadata-eval1.8%
rem-square-sqrt1.8%
unpow21.8%
*-commutative1.8%
unpow21.8%
rem-square-sqrt1.8%
Simplified1.8%
add-sqr-sqrt0.8%
sqrt-unprod4.6%
sqr-neg4.6%
add-sqr-sqrt4.6%
sqrt-unprod4.6%
unpow24.6%
sqrt-prod3.0%
add-sqr-sqrt3.6%
associate-*r*3.6%
clear-num3.6%
associate-/r/3.6%
associate-/r/3.6%
clear-num3.6%
clear-num3.6%
clear-num3.6%
associate-/l/3.6%
Applied egg-rr3.6%
Final simplification14.5%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -5e-310) (/ (- p_m) x) (/ p_m x)))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p_m / x;
} else {
tmp = p_m / x;
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = -p_m / x
else
tmp = p_m / x
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p_m / x;
} else {
tmp = p_m / x;
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -5e-310: tmp = -p_m / x else: tmp = p_m / x return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -5e-310) tmp = Float64(Float64(-p_m) / x); else tmp = Float64(p_m / x); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -5e-310) tmp = -p_m / x; else tmp = p_m / x; end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -5e-310], N[((-p$95$m) / x), $MachinePrecision], N[(p$95$m / x), $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{p_m}{x}\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 61.6%
add-sqr-sqrt61.6%
hypot-def61.6%
associate-*l*61.6%
sqrt-prod61.6%
metadata-eval61.6%
sqrt-unprod34.6%
add-sqr-sqrt61.6%
Applied egg-rr61.6%
Taylor expanded in x around -inf 18.4%
+-commutative18.4%
mul-1-neg18.4%
unsub-neg18.4%
associate-*r/18.4%
times-frac22.8%
distribute-rgt-out22.8%
metadata-eval22.8%
associate-/l*22.8%
Simplified22.8%
Taylor expanded in p around 0 25.4%
mul-1-neg25.4%
associate-/l*25.4%
distribute-neg-frac25.4%
associate-/r*25.5%
Simplified25.5%
associate-/l/24.6%
metadata-eval24.6%
metadata-eval24.6%
sqrt-undiv24.6%
sqrt-unprod25.0%
sqrt-undiv25.0%
metadata-eval25.0%
metadata-eval25.0%
metadata-eval25.0%
metadata-eval25.0%
div-inv25.0%
metadata-eval25.0%
*-commutative25.0%
*-un-lft-identity25.0%
expm1-log1p-u10.5%
expm1-udef2.1%
Applied egg-rr2.3%
expm1-def10.5%
expm1-log1p25.0%
Simplified25.8%
if -4.999999999999985e-310 < x Initial program 99.7%
+-commutative99.7%
add-cube-cbrt99.7%
fma-def99.7%
Applied egg-rr100.0%
Taylor expanded in x around -inf 1.8%
mul-1-neg1.8%
distribute-rgt-neg-in1.8%
distribute-rgt-out1.8%
metadata-eval1.8%
rem-square-sqrt1.8%
unpow21.8%
*-commutative1.8%
unpow21.8%
rem-square-sqrt1.8%
Simplified1.8%
add-sqr-sqrt0.8%
sqrt-unprod4.6%
sqr-neg4.6%
add-sqr-sqrt4.6%
sqrt-unprod4.6%
unpow24.6%
sqrt-prod3.0%
add-sqr-sqrt3.6%
associate-*r*3.6%
clear-num3.6%
associate-/r/3.6%
associate-/r/3.6%
clear-num3.6%
clear-num3.6%
clear-num3.6%
associate-/l/3.6%
Applied egg-rr3.0%
expm1-def3.4%
expm1-log1p3.6%
Simplified3.6%
Final simplification14.5%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (/ p_m x))
p_m = fabs(p);
double code(double p_m, double x) {
return p_m / x;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
code = p_m / x
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
return p_m / x;
}
p_m = math.fabs(p) def code(p_m, x): return p_m / x
p_m = abs(p) function code(p_m, x) return Float64(p_m / x) end
p_m = abs(p); function tmp = code(p_m, x) tmp = p_m / x; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := N[(p$95$m / x), $MachinePrecision]
\begin{array}{l}
p_m = \left|p\right|
\\
\frac{p_m}{x}
\end{array}
Initial program 81.1%
+-commutative81.1%
add-cube-cbrt81.0%
fma-def81.0%
Applied egg-rr81.2%
Taylor expanded in x around -inf 14.4%
mul-1-neg14.4%
distribute-rgt-neg-in14.4%
distribute-rgt-out14.4%
metadata-eval14.4%
rem-square-sqrt14.3%
unpow214.3%
*-commutative14.3%
unpow214.3%
rem-square-sqrt14.4%
Simplified14.4%
add-sqr-sqrt2.8%
sqrt-unprod5.3%
sqr-neg5.3%
add-sqr-sqrt5.3%
sqrt-unprod5.3%
unpow25.3%
sqrt-prod3.5%
add-sqr-sqrt15.1%
associate-*r*15.1%
clear-num15.1%
associate-/r/15.1%
associate-/r/15.1%
clear-num15.1%
clear-num15.1%
clear-num15.1%
associate-/l/15.0%
Applied egg-rr5.5%
expm1-def15.0%
expm1-log1p15.2%
Simplified15.2%
Final simplification15.2%
(FPCore (p x) :precision binary64 (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x))))))
double code(double p, double x) {
return sqrt((0.5 + (copysign(0.5, x) / hypot(1.0, ((2.0 * p) / x)))));
}
public static double code(double p, double x) {
return Math.sqrt((0.5 + (Math.copySign(0.5, x) / Math.hypot(1.0, ((2.0 * p) / x)))));
}
def code(p, x): return math.sqrt((0.5 + (math.copysign(0.5, x) / math.hypot(1.0, ((2.0 * p) / x)))))
function code(p, x) return sqrt(Float64(0.5 + Float64(copysign(0.5, x) / hypot(1.0, Float64(Float64(2.0 * p) / x))))) end
function tmp = code(p, x) tmp = sqrt((0.5 + ((sign(x) * abs(0.5)) / hypot(1.0, ((2.0 * p) / x))))); end
code[p_, x_] := N[Sqrt[N[(0.5 + N[(N[With[{TMP1 = Abs[0.5], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] / N[Sqrt[1.0 ^ 2 + N[(N[(2.0 * p), $MachinePrecision] / x), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}
\end{array}
herbie shell --seed 2024020
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:herbie-target
(sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x)))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))