
(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.5) (/ p_m (- x)) (sqrt (* (+ 1.0 (/ x (hypot x (* p_m 2.0)))) 0.5))))
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.5) {
tmp = p_m / -x;
} else {
tmp = sqrt(((1.0 + (x / hypot(x, (p_m * 2.0)))) * 0.5));
}
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.5) {
tmp = p_m / -x;
} else {
tmp = Math.sqrt(((1.0 + (x / Math.hypot(x, (p_m * 2.0)))) * 0.5));
}
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.5: tmp = p_m / -x else: tmp = math.sqrt(((1.0 + (x / math.hypot(x, (p_m * 2.0)))) * 0.5)) 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.5) tmp = Float64(p_m / Float64(-x)); else tmp = sqrt(Float64(Float64(1.0 + Float64(x / hypot(x, Float64(p_m * 2.0)))) * 0.5)); 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.5) tmp = p_m / -x; else tmp = sqrt(((1.0 + (x / hypot(x, (p_m * 2.0)))) * 0.5)); 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.5], N[(p$95$m / (-x)), $MachinePrecision], N[Sqrt[N[(N[(1.0 + N[(x / N[Sqrt[x ^ 2 + N[(p$95$m * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.5), $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.5:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(1 + \frac{x}{\mathsf{hypot}\left(x, p\_m \cdot 2\right)}\right) \cdot 0.5}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < -0.5Initial program 15.1%
+-commutative15.1%
sqr-neg15.1%
associate-*l*15.1%
sqr-neg15.1%
fma-define15.1%
sqr-neg15.1%
fma-define15.1%
associate-*l*15.1%
+-commutative15.1%
Simplified15.4%
Taylor expanded in x around -inf 60.7%
mul-1-neg60.7%
distribute-neg-frac260.7%
Simplified60.7%
if -0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
+-commutative100.0%
sqr-neg100.0%
associate-*l*100.0%
sqr-neg100.0%
fma-define100.0%
sqr-neg100.0%
fma-define100.0%
associate-*l*100.0%
+-commutative100.0%
Simplified100.0%
*-commutative100.0%
fma-undefine100.0%
associate-*r*100.0%
+-commutative100.0%
distribute-rgt1-in100.0%
+-commutative100.0%
Applied egg-rr100.0%
Final simplification90.9%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ p_m (- x))) (t_1 (+ 1.0 (/ (* -0.5 (* p_m p_m)) (* x x)))))
(if (<= p_m 1.8e-258)
t_1
(if (<= p_m 3.2e-242)
t_0
(if (<= p_m 5.2e-222)
t_1
(if (<= p_m 1.55e-103)
t_0
(if (<= p_m 3.1e-32)
1.0
(cbrt (pow (+ 0.5 (* 0.25 (/ x p_m))) 1.5)))))))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 1.8e-258) {
tmp = t_1;
} else if (p_m <= 3.2e-242) {
tmp = t_0;
} else if (p_m <= 5.2e-222) {
tmp = t_1;
} else if (p_m <= 1.55e-103) {
tmp = t_0;
} else if (p_m <= 3.1e-32) {
tmp = 1.0;
} else {
tmp = cbrt(pow((0.5 + (0.25 * (x / p_m))), 1.5));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 1.8e-258) {
tmp = t_1;
} else if (p_m <= 3.2e-242) {
tmp = t_0;
} else if (p_m <= 5.2e-222) {
tmp = t_1;
} else if (p_m <= 1.55e-103) {
tmp = t_0;
} else if (p_m <= 3.1e-32) {
tmp = 1.0;
} else {
tmp = Math.cbrt(Math.pow((0.5 + (0.25 * (x / p_m))), 1.5));
}
return tmp;
}
p_m = abs(p) function code(p_m, x) t_0 = Float64(p_m / Float64(-x)) t_1 = Float64(1.0 + Float64(Float64(-0.5 * Float64(p_m * p_m)) / Float64(x * x))) tmp = 0.0 if (p_m <= 1.8e-258) tmp = t_1; elseif (p_m <= 3.2e-242) tmp = t_0; elseif (p_m <= 5.2e-222) tmp = t_1; elseif (p_m <= 1.55e-103) tmp = t_0; elseif (p_m <= 3.1e-32) tmp = 1.0; else tmp = cbrt((Float64(0.5 + Float64(0.25 * Float64(x / p_m))) ^ 1.5)); end return tmp end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(p$95$m / (-x)), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(N[(-0.5 * N[(p$95$m * p$95$m), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p$95$m, 1.8e-258], t$95$1, If[LessEqual[p$95$m, 3.2e-242], t$95$0, If[LessEqual[p$95$m, 5.2e-222], t$95$1, If[LessEqual[p$95$m, 1.55e-103], t$95$0, If[LessEqual[p$95$m, 3.1e-32], 1.0, N[Power[N[Power[N[(0.5 + N[(0.25 * N[(x / p$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.5], $MachinePrecision], 1/3], $MachinePrecision]]]]]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{p\_m}{-x}\\
t_1 := 1 + \frac{-0.5 \cdot \left(p\_m \cdot p\_m\right)}{x \cdot x}\\
\mathbf{if}\;p\_m \leq 1.8 \cdot 10^{-258}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 3.2 \cdot 10^{-242}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 5.2 \cdot 10^{-222}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 1.55 \cdot 10^{-103}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 3.1 \cdot 10^{-32}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{{\left(0.5 + 0.25 \cdot \frac{x}{p\_m}\right)}^{1.5}}\\
\end{array}
\end{array}
if p < 1.79999999999999989e-258 or 3.19999999999999999e-242 < p < 5.1999999999999997e-222Initial program 81.5%
+-commutative81.5%
sqr-neg81.5%
associate-*l*81.5%
sqr-neg81.5%
fma-define81.5%
sqr-neg81.5%
fma-define81.5%
associate-*l*81.5%
+-commutative81.5%
Simplified81.5%
Taylor expanded in x around inf 37.0%
associate-*r/37.0%
Simplified37.0%
unpow237.0%
Applied egg-rr37.0%
unpow237.0%
Applied egg-rr37.0%
if 1.79999999999999989e-258 < p < 3.19999999999999999e-242 or 5.1999999999999997e-222 < p < 1.5500000000000001e-103Initial program 45.4%
+-commutative45.4%
sqr-neg45.4%
associate-*l*45.4%
sqr-neg45.4%
fma-define45.4%
sqr-neg45.4%
fma-define45.4%
associate-*l*45.4%
+-commutative45.4%
Simplified45.4%
Taylor expanded in x around -inf 66.6%
mul-1-neg66.6%
distribute-neg-frac266.6%
Simplified66.6%
if 1.5500000000000001e-103 < p < 3.10000000000000011e-32Initial program 66.2%
Taylor expanded in x around inf 50.9%
if 3.10000000000000011e-32 < p Initial program 93.1%
+-commutative93.1%
sqr-neg93.1%
associate-*l*93.1%
sqr-neg93.1%
fma-define93.1%
sqr-neg93.1%
fma-define93.1%
associate-*l*93.1%
+-commutative93.1%
Simplified93.3%
*-commutative93.3%
fma-undefine93.1%
associate-*r*93.1%
+-commutative93.1%
distribute-rgt1-in93.1%
+-commutative93.1%
Applied egg-rr93.3%
add-cbrt-cube93.3%
add-sqr-sqrt93.3%
pow193.3%
pow1/293.3%
pow-prod-up93.3%
*-commutative93.3%
distribute-lft-in93.3%
metadata-eval93.3%
metadata-eval93.3%
Applied egg-rr93.3%
Taylor expanded in x around 0 85.8%
Final simplification56.2%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ p_m (- x))) (t_1 (+ 1.0 (/ (* -0.5 (* p_m p_m)) (* x x)))))
(if (<= p_m 9e-258)
t_1
(if (<= p_m 1.15e-241)
t_0
(if (<= p_m 1.55e-217)
t_1
(if (<= p_m 1.7e-103)
t_0
(if (<= p_m 6e-34) 1.0 (sqrt (+ 0.5 (* 0.25 (/ x p_m)))))))))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 9e-258) {
tmp = t_1;
} else if (p_m <= 1.15e-241) {
tmp = t_0;
} else if (p_m <= 1.55e-217) {
tmp = t_1;
} else if (p_m <= 1.7e-103) {
tmp = t_0;
} else if (p_m <= 6e-34) {
tmp = 1.0;
} else {
tmp = sqrt((0.5 + (0.25 * (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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = p_m / -x
t_1 = 1.0d0 + (((-0.5d0) * (p_m * p_m)) / (x * x))
if (p_m <= 9d-258) then
tmp = t_1
else if (p_m <= 1.15d-241) then
tmp = t_0
else if (p_m <= 1.55d-217) then
tmp = t_1
else if (p_m <= 1.7d-103) then
tmp = t_0
else if (p_m <= 6d-34) then
tmp = 1.0d0
else
tmp = sqrt((0.5d0 + (0.25d0 * (x / p_m))))
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 9e-258) {
tmp = t_1;
} else if (p_m <= 1.15e-241) {
tmp = t_0;
} else if (p_m <= 1.55e-217) {
tmp = t_1;
} else if (p_m <= 1.7e-103) {
tmp = t_0;
} else if (p_m <= 6e-34) {
tmp = 1.0;
} else {
tmp = Math.sqrt((0.5 + (0.25 * (x / p_m))));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): t_0 = p_m / -x t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)) tmp = 0 if p_m <= 9e-258: tmp = t_1 elif p_m <= 1.15e-241: tmp = t_0 elif p_m <= 1.55e-217: tmp = t_1 elif p_m <= 1.7e-103: tmp = t_0 elif p_m <= 6e-34: tmp = 1.0 else: tmp = math.sqrt((0.5 + (0.25 * (x / p_m)))) return tmp
p_m = abs(p) function code(p_m, x) t_0 = Float64(p_m / Float64(-x)) t_1 = Float64(1.0 + Float64(Float64(-0.5 * Float64(p_m * p_m)) / Float64(x * x))) tmp = 0.0 if (p_m <= 9e-258) tmp = t_1; elseif (p_m <= 1.15e-241) tmp = t_0; elseif (p_m <= 1.55e-217) tmp = t_1; elseif (p_m <= 1.7e-103) tmp = t_0; elseif (p_m <= 6e-34) tmp = 1.0; else tmp = sqrt(Float64(0.5 + Float64(0.25 * Float64(x / p_m)))); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) t_0 = p_m / -x; t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)); tmp = 0.0; if (p_m <= 9e-258) tmp = t_1; elseif (p_m <= 1.15e-241) tmp = t_0; elseif (p_m <= 1.55e-217) tmp = t_1; elseif (p_m <= 1.7e-103) tmp = t_0; elseif (p_m <= 6e-34) tmp = 1.0; else tmp = sqrt((0.5 + (0.25 * (x / p_m)))); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(p$95$m / (-x)), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(N[(-0.5 * N[(p$95$m * p$95$m), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p$95$m, 9e-258], t$95$1, If[LessEqual[p$95$m, 1.15e-241], t$95$0, If[LessEqual[p$95$m, 1.55e-217], t$95$1, If[LessEqual[p$95$m, 1.7e-103], t$95$0, If[LessEqual[p$95$m, 6e-34], 1.0, N[Sqrt[N[(0.5 + N[(0.25 * N[(x / p$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{p\_m}{-x}\\
t_1 := 1 + \frac{-0.5 \cdot \left(p\_m \cdot p\_m\right)}{x \cdot x}\\
\mathbf{if}\;p\_m \leq 9 \cdot 10^{-258}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 1.15 \cdot 10^{-241}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 1.55 \cdot 10^{-217}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 1.7 \cdot 10^{-103}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 6 \cdot 10^{-34}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p\_m}}\\
\end{array}
\end{array}
if p < 9.00000000000000017e-258 or 1.15e-241 < p < 1.5499999999999999e-217Initial program 81.5%
+-commutative81.5%
sqr-neg81.5%
associate-*l*81.5%
sqr-neg81.5%
fma-define81.5%
sqr-neg81.5%
fma-define81.5%
associate-*l*81.5%
+-commutative81.5%
Simplified81.5%
Taylor expanded in x around inf 37.0%
associate-*r/37.0%
Simplified37.0%
unpow237.0%
Applied egg-rr37.0%
unpow237.0%
Applied egg-rr37.0%
if 9.00000000000000017e-258 < p < 1.15e-241 or 1.5499999999999999e-217 < p < 1.70000000000000001e-103Initial program 45.4%
+-commutative45.4%
sqr-neg45.4%
associate-*l*45.4%
sqr-neg45.4%
fma-define45.4%
sqr-neg45.4%
fma-define45.4%
associate-*l*45.4%
+-commutative45.4%
Simplified45.4%
Taylor expanded in x around -inf 66.6%
mul-1-neg66.6%
distribute-neg-frac266.6%
Simplified66.6%
if 1.70000000000000001e-103 < p < 6e-34Initial program 66.2%
Taylor expanded in x around inf 50.9%
if 6e-34 < p Initial program 93.1%
+-commutative93.1%
sqr-neg93.1%
associate-*l*93.1%
sqr-neg93.1%
fma-define93.1%
sqr-neg93.1%
fma-define93.1%
associate-*l*93.1%
+-commutative93.1%
Simplified93.3%
Taylor expanded in x around 0 85.8%
Final simplification56.2%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ p_m (- x))) (t_1 (+ 1.0 (/ (* -0.5 (* p_m p_m)) (* x x)))))
(if (<= p_m 2.2e-258)
t_1
(if (<= p_m 3.5e-242)
t_0
(if (<= p_m 1.55e-217)
t_1
(if (<= p_m 9.5e-104) t_0 (if (<= p_m 8e-42) 1.0 (sqrt 0.5))))))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 2.2e-258) {
tmp = t_1;
} else if (p_m <= 3.5e-242) {
tmp = t_0;
} else if (p_m <= 1.55e-217) {
tmp = t_1;
} else if (p_m <= 9.5e-104) {
tmp = t_0;
} else if (p_m <= 8e-42) {
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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = p_m / -x
t_1 = 1.0d0 + (((-0.5d0) * (p_m * p_m)) / (x * x))
if (p_m <= 2.2d-258) then
tmp = t_1
else if (p_m <= 3.5d-242) then
tmp = t_0
else if (p_m <= 1.55d-217) then
tmp = t_1
else if (p_m <= 9.5d-104) then
tmp = t_0
else if (p_m <= 8d-42) 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 t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 2.2e-258) {
tmp = t_1;
} else if (p_m <= 3.5e-242) {
tmp = t_0;
} else if (p_m <= 1.55e-217) {
tmp = t_1;
} else if (p_m <= 9.5e-104) {
tmp = t_0;
} else if (p_m <= 8e-42) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): t_0 = p_m / -x t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)) tmp = 0 if p_m <= 2.2e-258: tmp = t_1 elif p_m <= 3.5e-242: tmp = t_0 elif p_m <= 1.55e-217: tmp = t_1 elif p_m <= 9.5e-104: tmp = t_0 elif p_m <= 8e-42: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) t_0 = Float64(p_m / Float64(-x)) t_1 = Float64(1.0 + Float64(Float64(-0.5 * Float64(p_m * p_m)) / Float64(x * x))) tmp = 0.0 if (p_m <= 2.2e-258) tmp = t_1; elseif (p_m <= 3.5e-242) tmp = t_0; elseif (p_m <= 1.55e-217) tmp = t_1; elseif (p_m <= 9.5e-104) tmp = t_0; elseif (p_m <= 8e-42) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) t_0 = p_m / -x; t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)); tmp = 0.0; if (p_m <= 2.2e-258) tmp = t_1; elseif (p_m <= 3.5e-242) tmp = t_0; elseif (p_m <= 1.55e-217) tmp = t_1; elseif (p_m <= 9.5e-104) tmp = t_0; elseif (p_m <= 8e-42) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(p$95$m / (-x)), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(N[(-0.5 * N[(p$95$m * p$95$m), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p$95$m, 2.2e-258], t$95$1, If[LessEqual[p$95$m, 3.5e-242], t$95$0, If[LessEqual[p$95$m, 1.55e-217], t$95$1, If[LessEqual[p$95$m, 9.5e-104], t$95$0, If[LessEqual[p$95$m, 8e-42], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{p\_m}{-x}\\
t_1 := 1 + \frac{-0.5 \cdot \left(p\_m \cdot p\_m\right)}{x \cdot x}\\
\mathbf{if}\;p\_m \leq 2.2 \cdot 10^{-258}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 3.5 \cdot 10^{-242}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 1.55 \cdot 10^{-217}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 9.5 \cdot 10^{-104}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 8 \cdot 10^{-42}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 2.20000000000000015e-258 or 3.4999999999999999e-242 < p < 1.5499999999999999e-217Initial program 81.5%
+-commutative81.5%
sqr-neg81.5%
associate-*l*81.5%
sqr-neg81.5%
fma-define81.5%
sqr-neg81.5%
fma-define81.5%
associate-*l*81.5%
+-commutative81.5%
Simplified81.5%
Taylor expanded in x around inf 37.0%
associate-*r/37.0%
Simplified37.0%
unpow237.0%
Applied egg-rr37.0%
unpow237.0%
Applied egg-rr37.0%
if 2.20000000000000015e-258 < p < 3.4999999999999999e-242 or 1.5499999999999999e-217 < p < 9.5000000000000002e-104Initial program 45.4%
+-commutative45.4%
sqr-neg45.4%
associate-*l*45.4%
sqr-neg45.4%
fma-define45.4%
sqr-neg45.4%
fma-define45.4%
associate-*l*45.4%
+-commutative45.4%
Simplified45.4%
Taylor expanded in x around -inf 66.6%
mul-1-neg66.6%
distribute-neg-frac266.6%
Simplified66.6%
if 9.5000000000000002e-104 < p < 8.0000000000000003e-42Initial program 74.5%
Taylor expanded in x around inf 57.1%
if 8.0000000000000003e-42 < p Initial program 90.9%
Taylor expanded in x around 0 82.9%
Final simplification55.9%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ p_m (- x))) (t_1 (+ 1.0 (/ (* -0.5 (* p_m p_m)) (* x x)))))
(if (<= p_m 9e-259)
t_1
(if (<= p_m 9.6e-242)
t_0
(if (<= p_m 1.55e-218)
t_1
(if (<= p_m 7.8e-104) t_0 (if (<= p_m 3.2e-42) t_1 (sqrt 0.5))))))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 9e-259) {
tmp = t_1;
} else if (p_m <= 9.6e-242) {
tmp = t_0;
} else if (p_m <= 1.55e-218) {
tmp = t_1;
} else if (p_m <= 7.8e-104) {
tmp = t_0;
} else if (p_m <= 3.2e-42) {
tmp = t_1;
} 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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = p_m / -x
t_1 = 1.0d0 + (((-0.5d0) * (p_m * p_m)) / (x * x))
if (p_m <= 9d-259) then
tmp = t_1
else if (p_m <= 9.6d-242) then
tmp = t_0
else if (p_m <= 1.55d-218) then
tmp = t_1
else if (p_m <= 7.8d-104) then
tmp = t_0
else if (p_m <= 3.2d-42) then
tmp = t_1
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 t_0 = p_m / -x;
double t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
double tmp;
if (p_m <= 9e-259) {
tmp = t_1;
} else if (p_m <= 9.6e-242) {
tmp = t_0;
} else if (p_m <= 1.55e-218) {
tmp = t_1;
} else if (p_m <= 7.8e-104) {
tmp = t_0;
} else if (p_m <= 3.2e-42) {
tmp = t_1;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): t_0 = p_m / -x t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)) tmp = 0 if p_m <= 9e-259: tmp = t_1 elif p_m <= 9.6e-242: tmp = t_0 elif p_m <= 1.55e-218: tmp = t_1 elif p_m <= 7.8e-104: tmp = t_0 elif p_m <= 3.2e-42: tmp = t_1 else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) t_0 = Float64(p_m / Float64(-x)) t_1 = Float64(1.0 + Float64(Float64(-0.5 * Float64(p_m * p_m)) / Float64(x * x))) tmp = 0.0 if (p_m <= 9e-259) tmp = t_1; elseif (p_m <= 9.6e-242) tmp = t_0; elseif (p_m <= 1.55e-218) tmp = t_1; elseif (p_m <= 7.8e-104) tmp = t_0; elseif (p_m <= 3.2e-42) tmp = t_1; else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) t_0 = p_m / -x; t_1 = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)); tmp = 0.0; if (p_m <= 9e-259) tmp = t_1; elseif (p_m <= 9.6e-242) tmp = t_0; elseif (p_m <= 1.55e-218) tmp = t_1; elseif (p_m <= 7.8e-104) tmp = t_0; elseif (p_m <= 3.2e-42) tmp = t_1; else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(p$95$m / (-x)), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(N[(-0.5 * N[(p$95$m * p$95$m), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p$95$m, 9e-259], t$95$1, If[LessEqual[p$95$m, 9.6e-242], t$95$0, If[LessEqual[p$95$m, 1.55e-218], t$95$1, If[LessEqual[p$95$m, 7.8e-104], t$95$0, If[LessEqual[p$95$m, 3.2e-42], t$95$1, N[Sqrt[0.5], $MachinePrecision]]]]]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{p\_m}{-x}\\
t_1 := 1 + \frac{-0.5 \cdot \left(p\_m \cdot p\_m\right)}{x \cdot x}\\
\mathbf{if}\;p\_m \leq 9 \cdot 10^{-259}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 9.6 \cdot 10^{-242}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 1.55 \cdot 10^{-218}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;p\_m \leq 7.8 \cdot 10^{-104}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;p\_m \leq 3.2 \cdot 10^{-42}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 8.99999999999999947e-259 or 9.6000000000000004e-242 < p < 1.54999999999999999e-218 or 7.8000000000000004e-104 < p < 3.20000000000000025e-42Initial program 80.8%
+-commutative80.8%
sqr-neg80.8%
associate-*l*80.8%
sqr-neg80.8%
fma-define80.8%
sqr-neg80.8%
fma-define80.8%
associate-*l*80.8%
+-commutative80.8%
Simplified80.8%
Taylor expanded in x around inf 38.8%
associate-*r/38.8%
Simplified38.8%
unpow238.8%
Applied egg-rr38.8%
unpow238.8%
Applied egg-rr38.8%
if 8.99999999999999947e-259 < p < 9.6000000000000004e-242 or 1.54999999999999999e-218 < p < 7.8000000000000004e-104Initial program 45.4%
+-commutative45.4%
sqr-neg45.4%
associate-*l*45.4%
sqr-neg45.4%
fma-define45.4%
sqr-neg45.4%
fma-define45.4%
associate-*l*45.4%
+-commutative45.4%
Simplified45.4%
Taylor expanded in x around -inf 66.6%
mul-1-neg66.6%
distribute-neg-frac266.6%
Simplified66.6%
if 3.20000000000000025e-42 < p Initial program 90.9%
Taylor expanded in x around 0 82.9%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x 1.22e-150) (/ p_m (- x)) (+ 1.0 (/ (* -0.5 (* p_m p_m)) (* x x)))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= 1.22e-150) {
tmp = p_m / -x;
} else {
tmp = 1.0 + ((-0.5 * (p_m * p_m)) / (x * 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 <= 1.22d-150) then
tmp = p_m / -x
else
tmp = 1.0d0 + (((-0.5d0) * (p_m * p_m)) / (x * 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 <= 1.22e-150) {
tmp = p_m / -x;
} else {
tmp = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= 1.22e-150: tmp = p_m / -x else: tmp = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= 1.22e-150) tmp = Float64(p_m / Float64(-x)); else tmp = Float64(1.0 + Float64(Float64(-0.5 * Float64(p_m * p_m)) / Float64(x * x))); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= 1.22e-150) tmp = p_m / -x; else tmp = 1.0 + ((-0.5 * (p_m * p_m)) / (x * x)); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, 1.22e-150], N[(p$95$m / (-x)), $MachinePrecision], N[(1.0 + N[(N[(-0.5 * N[(p$95$m * p$95$m), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.22 \cdot 10^{-150}:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{-0.5 \cdot \left(p\_m \cdot p\_m\right)}{x \cdot x}\\
\end{array}
\end{array}
if x < 1.22e-150Initial program 59.0%
+-commutative59.0%
sqr-neg59.0%
associate-*l*59.0%
sqr-neg59.0%
fma-define59.0%
sqr-neg59.0%
fma-define59.0%
associate-*l*59.0%
+-commutative59.0%
Simplified59.1%
Taylor expanded in x around -inf 31.3%
mul-1-neg31.3%
distribute-neg-frac231.3%
Simplified31.3%
if 1.22e-150 < x Initial program 100.0%
+-commutative100.0%
sqr-neg100.0%
associate-*l*100.0%
sqr-neg100.0%
fma-define100.0%
sqr-neg100.0%
fma-define100.0%
associate-*l*100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around inf 51.5%
associate-*r/51.5%
Simplified51.5%
unpow251.5%
Applied egg-rr51.5%
unpow251.5%
Applied egg-rr51.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 / Float64(-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 80.4%
+-commutative80.4%
sqr-neg80.4%
associate-*l*80.4%
sqr-neg80.4%
fma-define80.4%
sqr-neg80.4%
fma-define80.4%
associate-*l*80.4%
+-commutative80.4%
Simplified80.5%
Taylor expanded in x around -inf 16.7%
mul-1-neg16.7%
distribute-neg-frac216.7%
Simplified16.7%
(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 2024085
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:alt
(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))))))))