
(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 8 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}
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0) (/ (- p) x) (sqrt (+ 0.5 (* (/ x (hypot x (* p 2.0))) 0.5)))))
p = abs(p);
double code(double p, double x) {
double tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
tmp = -p / x;
} else {
tmp = sqrt((0.5 + ((x / hypot(x, (p * 2.0))) * 0.5)));
}
return tmp;
}
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
tmp = -p / x;
} else {
tmp = Math.sqrt((0.5 + ((x / Math.hypot(x, (p * 2.0))) * 0.5)));
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0: tmp = -p / x else: tmp = math.sqrt((0.5 + ((x / math.hypot(x, (p * 2.0))) * 0.5))) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -1.0) tmp = Float64(Float64(-p) / x); else tmp = sqrt(Float64(0.5 + Float64(Float64(x / hypot(x, Float64(p * 2.0))) * 0.5))); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) tmp = -p / x; else tmp = sqrt((0.5 + ((x / hypot(x, (p * 2.0))) * 0.5))); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 + N[(N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 + \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)} \cdot 0.5}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 14.8%
+-commutative14.8%
distribute-rgt-in14.8%
+-commutative14.8%
add-sqr-sqrt14.8%
hypot-def14.8%
associate-*l*14.8%
sqrt-prod14.8%
metadata-eval14.8%
sqrt-unprod4.5%
add-sqr-sqrt14.8%
metadata-eval14.8%
Applied egg-rr14.8%
Taylor expanded in x around -inf 50.3%
neg-mul-150.3%
distribute-neg-frac50.3%
Simplified50.3%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 100.0%
+-commutative100.0%
distribute-rgt-in100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
associate-*l*100.0%
sqrt-prod100.0%
metadata-eval100.0%
sqrt-unprod52.4%
add-sqr-sqrt100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Final simplification86.2%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(let* ((t_0 (/ (- p) x)))
(if (<= x -3.8e+49)
t_0
(if (<= x -1.2)
(sqrt 0.5)
(if (<= x -1.5e-9)
t_0
(if (<= x -2.3e-82)
(sqrt (+ 0.5 (* 0.25 (/ x p))))
(if (<= x -3.2e-137)
t_0
(sqrt (* 0.5 (+ 1.0 (/ x (+ x (* 2.0 (/ (* p p) x))))))))))))))p = abs(p);
double code(double p, double x) {
double t_0 = -p / x;
double tmp;
if (x <= -3.8e+49) {
tmp = t_0;
} else if (x <= -1.2) {
tmp = sqrt(0.5);
} else if (x <= -1.5e-9) {
tmp = t_0;
} else if (x <= -2.3e-82) {
tmp = sqrt((0.5 + (0.25 * (x / p))));
} else if (x <= -3.2e-137) {
tmp = t_0;
} else {
tmp = sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x)))))));
}
return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = -p / x
if (x <= (-3.8d+49)) then
tmp = t_0
else if (x <= (-1.2d0)) then
tmp = sqrt(0.5d0)
else if (x <= (-1.5d-9)) then
tmp = t_0
else if (x <= (-2.3d-82)) then
tmp = sqrt((0.5d0 + (0.25d0 * (x / p))))
else if (x <= (-3.2d-137)) then
tmp = t_0
else
tmp = sqrt((0.5d0 * (1.0d0 + (x / (x + (2.0d0 * ((p * p) / x)))))))
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double t_0 = -p / x;
double tmp;
if (x <= -3.8e+49) {
tmp = t_0;
} else if (x <= -1.2) {
tmp = Math.sqrt(0.5);
} else if (x <= -1.5e-9) {
tmp = t_0;
} else if (x <= -2.3e-82) {
tmp = Math.sqrt((0.5 + (0.25 * (x / p))));
} else if (x <= -3.2e-137) {
tmp = t_0;
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x)))))));
}
return tmp;
}
p = abs(p) def code(p, x): t_0 = -p / x tmp = 0 if x <= -3.8e+49: tmp = t_0 elif x <= -1.2: tmp = math.sqrt(0.5) elif x <= -1.5e-9: tmp = t_0 elif x <= -2.3e-82: tmp = math.sqrt((0.5 + (0.25 * (x / p)))) elif x <= -3.2e-137: tmp = t_0 else: tmp = math.sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x))))))) return tmp
p = abs(p) function code(p, x) t_0 = Float64(Float64(-p) / x) tmp = 0.0 if (x <= -3.8e+49) tmp = t_0; elseif (x <= -1.2) tmp = sqrt(0.5); elseif (x <= -1.5e-9) tmp = t_0; elseif (x <= -2.3e-82) tmp = sqrt(Float64(0.5 + Float64(0.25 * Float64(x / p)))); elseif (x <= -3.2e-137) tmp = t_0; else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / Float64(x + Float64(2.0 * Float64(Float64(p * p) / x))))))); end return tmp end
p = abs(p) function tmp_2 = code(p, x) t_0 = -p / x; tmp = 0.0; if (x <= -3.8e+49) tmp = t_0; elseif (x <= -1.2) tmp = sqrt(0.5); elseif (x <= -1.5e-9) tmp = t_0; elseif (x <= -2.3e-82) tmp = sqrt((0.5 + (0.25 * (x / p)))); elseif (x <= -3.2e-137) tmp = t_0; else tmp = sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x))))))); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[((-p) / x), $MachinePrecision]}, If[LessEqual[x, -3.8e+49], t$95$0, If[LessEqual[x, -1.2], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[x, -1.5e-9], t$95$0, If[LessEqual[x, -2.3e-82], N[Sqrt[N[(0.5 + N[(0.25 * N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[x, -3.2e-137], t$95$0, N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[(x + N[(2.0 * N[(N[(p * p), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{-p}{x}\\
\mathbf{if}\;x \leq -3.8 \cdot 10^{+49}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq -1.2:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{elif}\;x \leq -1.5 \cdot 10^{-9}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq -2.3 \cdot 10^{-82}:\\
\;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\
\mathbf{elif}\;x \leq -3.2 \cdot 10^{-137}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{x + 2 \cdot \frac{p \cdot p}{x}}\right)}\\
\end{array}
\end{array}
if x < -3.7999999999999999e49 or -1.19999999999999996 < x < -1.49999999999999999e-9 or -2.29999999999999997e-82 < x < -3.20000000000000021e-137Initial program 43.1%
+-commutative43.1%
distribute-rgt-in43.1%
+-commutative43.1%
add-sqr-sqrt43.1%
hypot-def43.1%
associate-*l*43.1%
sqrt-prod43.1%
metadata-eval43.1%
sqrt-unprod16.0%
add-sqr-sqrt43.1%
metadata-eval43.1%
Applied egg-rr43.1%
Taylor expanded in x around -inf 36.1%
neg-mul-136.1%
distribute-neg-frac36.1%
Simplified36.1%
if -3.7999999999999999e49 < x < -1.19999999999999996Initial program 74.0%
Taylor expanded in x around 0 74.5%
if -1.49999999999999999e-9 < x < -2.29999999999999997e-82Initial program 65.7%
Taylor expanded in x around 0 63.6%
if -3.20000000000000021e-137 < x Initial program 98.6%
Taylor expanded in p around 0 98.3%
unpow298.3%
Simplified98.3%
Final simplification73.8%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(if (<= p 5e-180)
(+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))
(if (<= p 6.2e-31)
(/ (- p) x)
(if (or (<= p 3.8e-5) (not (<= p 1.05e+68)))
(sqrt (+ 0.5 (* 0.25 (/ x p))))
1.0))))p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 5e-180) {
tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
} else if (p <= 6.2e-31) {
tmp = -p / x;
} else if ((p <= 3.8e-5) || !(p <= 1.05e+68)) {
tmp = sqrt((0.5 + (0.25 * (x / p))));
} else {
tmp = 1.0;
}
return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (p <= 5d-180) then
tmp = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
else if (p <= 6.2d-31) then
tmp = -p / x
else if ((p <= 3.8d-5) .or. (.not. (p <= 1.05d+68))) then
tmp = sqrt((0.5d0 + (0.25d0 * (x / p))))
else
tmp = 1.0d0
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if (p <= 5e-180) {
tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
} else if (p <= 6.2e-31) {
tmp = -p / x;
} else if ((p <= 3.8e-5) || !(p <= 1.05e+68)) {
tmp = Math.sqrt((0.5 + (0.25 * (x / p))));
} else {
tmp = 1.0;
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 5e-180: tmp = 1.0 + (-0.5 * ((p / x) / (x / p))) elif p <= 6.2e-31: tmp = -p / x elif (p <= 3.8e-5) or not (p <= 1.05e+68): tmp = math.sqrt((0.5 + (0.25 * (x / p)))) else: tmp = 1.0 return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 5e-180) tmp = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p)))); elseif (p <= 6.2e-31) tmp = Float64(Float64(-p) / x); elseif ((p <= 3.8e-5) || !(p <= 1.05e+68)) tmp = sqrt(Float64(0.5 + Float64(0.25 * Float64(x / p)))); else tmp = 1.0; end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (p <= 5e-180) tmp = 1.0 + (-0.5 * ((p / x) / (x / p))); elseif (p <= 6.2e-31) tmp = -p / x; elseif ((p <= 3.8e-5) || ~((p <= 1.05e+68))) tmp = sqrt((0.5 + (0.25 * (x / p)))); else tmp = 1.0; end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[p, 5e-180], N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 6.2e-31], N[((-p) / x), $MachinePrecision], If[Or[LessEqual[p, 3.8e-5], N[Not[LessEqual[p, 1.05e+68]], $MachinePrecision]], N[Sqrt[N[(0.5 + N[(0.25 * N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 1.0]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 5 \cdot 10^{-180}:\\
\;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\
\mathbf{elif}\;p \leq 6.2 \cdot 10^{-31}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{elif}\;p \leq 3.8 \cdot 10^{-5} \lor \neg \left(p \leq 1.05 \cdot 10^{+68}\right):\\
\;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if p < 5.0000000000000001e-180Initial program 74.6%
+-commutative74.6%
distribute-rgt-in74.6%
+-commutative74.6%
add-sqr-sqrt74.6%
hypot-def74.6%
associate-*l*74.6%
sqrt-prod74.6%
metadata-eval74.6%
sqrt-unprod13.1%
add-sqr-sqrt74.6%
metadata-eval74.6%
Applied egg-rr74.6%
Taylor expanded in x around inf 34.5%
unpow234.5%
unpow234.5%
times-frac34.5%
unpow234.5%
Simplified34.5%
unpow234.5%
clear-num34.5%
un-div-inv34.5%
Applied egg-rr34.5%
if 5.0000000000000001e-180 < p < 6.19999999999999999e-31Initial program 42.6%
+-commutative42.6%
distribute-rgt-in42.6%
+-commutative42.6%
add-sqr-sqrt42.6%
hypot-def42.6%
associate-*l*42.6%
sqrt-prod42.6%
metadata-eval42.6%
sqrt-unprod42.6%
add-sqr-sqrt42.6%
metadata-eval42.6%
Applied egg-rr42.6%
Taylor expanded in x around -inf 61.4%
neg-mul-161.4%
distribute-neg-frac61.4%
Simplified61.4%
if 6.19999999999999999e-31 < p < 3.8000000000000002e-5 or 1.05e68 < p Initial program 96.8%
Taylor expanded in x around 0 95.1%
if 3.8000000000000002e-5 < p < 1.05e68Initial program 67.8%
+-commutative67.8%
distribute-rgt-in67.8%
+-commutative67.8%
add-sqr-sqrt67.8%
hypot-def67.8%
associate-*l*67.8%
sqrt-prod67.8%
metadata-eval67.8%
sqrt-unprod67.8%
add-sqr-sqrt67.8%
metadata-eval67.8%
Applied egg-rr67.8%
Taylor expanded in x around inf 53.5%
Final simplification52.7%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(let* ((t_0 (+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))))
(if (<= p 4.3e-182)
t_0
(if (<= p 5.6e-35)
(/ (- p) x)
(if (<= p 0.00048) (sqrt 0.5) (if (<= p 4.75e+33) t_0 (sqrt 0.5)))))))p = abs(p);
double code(double p, double x) {
double t_0 = 1.0 + (-0.5 * ((p / x) / (x / p)));
double tmp;
if (p <= 4.3e-182) {
tmp = t_0;
} else if (p <= 5.6e-35) {
tmp = -p / x;
} else if (p <= 0.00048) {
tmp = sqrt(0.5);
} else if (p <= 4.75e+33) {
tmp = t_0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
if (p <= 4.3d-182) then
tmp = t_0
else if (p <= 5.6d-35) then
tmp = -p / x
else if (p <= 0.00048d0) then
tmp = sqrt(0.5d0)
else if (p <= 4.75d+33) then
tmp = t_0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double t_0 = 1.0 + (-0.5 * ((p / x) / (x / p)));
double tmp;
if (p <= 4.3e-182) {
tmp = t_0;
} else if (p <= 5.6e-35) {
tmp = -p / x;
} else if (p <= 0.00048) {
tmp = Math.sqrt(0.5);
} else if (p <= 4.75e+33) {
tmp = t_0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): t_0 = 1.0 + (-0.5 * ((p / x) / (x / p))) tmp = 0 if p <= 4.3e-182: tmp = t_0 elif p <= 5.6e-35: tmp = -p / x elif p <= 0.00048: tmp = math.sqrt(0.5) elif p <= 4.75e+33: tmp = t_0 else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) t_0 = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p)))) tmp = 0.0 if (p <= 4.3e-182) tmp = t_0; elseif (p <= 5.6e-35) tmp = Float64(Float64(-p) / x); elseif (p <= 0.00048) tmp = sqrt(0.5); elseif (p <= 4.75e+33) tmp = t_0; else tmp = sqrt(0.5); end return tmp end
p = abs(p) function tmp_2 = code(p, x) t_0 = 1.0 + (-0.5 * ((p / x) / (x / p))); tmp = 0.0; if (p <= 4.3e-182) tmp = t_0; elseif (p <= 5.6e-35) tmp = -p / x; elseif (p <= 0.00048) tmp = sqrt(0.5); elseif (p <= 4.75e+33) tmp = t_0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p, 4.3e-182], t$95$0, If[LessEqual[p, 5.6e-35], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 0.00048], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, 4.75e+33], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := 1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\
\mathbf{if}\;p \leq 4.3 \cdot 10^{-182}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p \leq 5.6 \cdot 10^{-35}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{elif}\;p \leq 0.00048:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 4.3e-182 or 4.80000000000000012e-4 < p < 4.7500000000000002e33Initial program 74.6%
+-commutative74.6%
distribute-rgt-in74.6%
+-commutative74.6%
add-sqr-sqrt74.6%
hypot-def74.6%
associate-*l*74.6%
sqrt-prod74.6%
metadata-eval74.6%
sqrt-unprod16.2%
add-sqr-sqrt74.6%
metadata-eval74.6%
Applied egg-rr74.6%
Taylor expanded in x around inf 36.0%
unpow236.0%
unpow236.0%
times-frac36.0%
unpow236.0%
Simplified36.0%
unpow236.0%
clear-num36.0%
un-div-inv36.0%
Applied egg-rr36.0%
if 4.3e-182 < p < 5.5999999999999999e-35Initial program 42.6%
+-commutative42.6%
distribute-rgt-in42.6%
+-commutative42.6%
add-sqr-sqrt42.6%
hypot-def42.6%
associate-*l*42.6%
sqrt-prod42.6%
metadata-eval42.6%
sqrt-unprod42.6%
add-sqr-sqrt42.6%
metadata-eval42.6%
Applied egg-rr42.6%
Taylor expanded in x around -inf 61.4%
neg-mul-161.4%
distribute-neg-frac61.4%
Simplified61.4%
if 5.5999999999999999e-35 < p < 4.80000000000000012e-4 or 4.7500000000000002e33 < p Initial program 92.9%
Taylor expanded in x around 0 88.9%
Final simplification52.5%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(if (<= p 3.5e-180)
(+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))
(if (<= p 2e-34)
(/ (- p) x)
(if (<= p 0.135) (sqrt 0.5) (if (<= p 4.75e+33) 1.0 (sqrt 0.5))))))p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 3.5e-180) {
tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
} else if (p <= 2e-34) {
tmp = -p / x;
} else if (p <= 0.135) {
tmp = sqrt(0.5);
} else if (p <= 4.75e+33) {
tmp = 1.0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (p <= 3.5d-180) then
tmp = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
else if (p <= 2d-34) then
tmp = -p / x
else if (p <= 0.135d0) then
tmp = sqrt(0.5d0)
else if (p <= 4.75d+33) then
tmp = 1.0d0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if (p <= 3.5e-180) {
tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
} else if (p <= 2e-34) {
tmp = -p / x;
} else if (p <= 0.135) {
tmp = Math.sqrt(0.5);
} else if (p <= 4.75e+33) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 3.5e-180: tmp = 1.0 + (-0.5 * ((p / x) / (x / p))) elif p <= 2e-34: tmp = -p / x elif p <= 0.135: tmp = math.sqrt(0.5) elif p <= 4.75e+33: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 3.5e-180) tmp = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p)))); elseif (p <= 2e-34) tmp = Float64(Float64(-p) / x); elseif (p <= 0.135) tmp = sqrt(0.5); elseif (p <= 4.75e+33) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (p <= 3.5e-180) tmp = 1.0 + (-0.5 * ((p / x) / (x / p))); elseif (p <= 2e-34) tmp = -p / x; elseif (p <= 0.135) tmp = sqrt(0.5); elseif (p <= 4.75e+33) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[p, 3.5e-180], N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 2e-34], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 0.135], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, 4.75e+33], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 3.5 \cdot 10^{-180}:\\
\;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\
\mathbf{elif}\;p \leq 2 \cdot 10^{-34}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{elif}\;p \leq 0.135:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 3.5000000000000001e-180Initial program 74.6%
+-commutative74.6%
distribute-rgt-in74.6%
+-commutative74.6%
add-sqr-sqrt74.6%
hypot-def74.6%
associate-*l*74.6%
sqrt-prod74.6%
metadata-eval74.6%
sqrt-unprod13.1%
add-sqr-sqrt74.6%
metadata-eval74.6%
Applied egg-rr74.6%
Taylor expanded in x around inf 34.5%
unpow234.5%
unpow234.5%
times-frac34.5%
unpow234.5%
Simplified34.5%
unpow234.5%
clear-num34.5%
un-div-inv34.5%
Applied egg-rr34.5%
if 3.5000000000000001e-180 < p < 1.99999999999999986e-34Initial program 42.6%
+-commutative42.6%
distribute-rgt-in42.6%
+-commutative42.6%
add-sqr-sqrt42.6%
hypot-def42.6%
associate-*l*42.6%
sqrt-prod42.6%
metadata-eval42.6%
sqrt-unprod42.6%
add-sqr-sqrt42.6%
metadata-eval42.6%
Applied egg-rr42.6%
Taylor expanded in x around -inf 61.4%
neg-mul-161.4%
distribute-neg-frac61.4%
Simplified61.4%
if 1.99999999999999986e-34 < p < 0.13500000000000001 or 4.7500000000000002e33 < p Initial program 92.9%
Taylor expanded in x around 0 88.9%
if 0.13500000000000001 < p < 4.7500000000000002e33Initial program 75.9%
+-commutative75.9%
distribute-rgt-in75.9%
+-commutative75.9%
add-sqr-sqrt75.9%
hypot-def75.9%
associate-*l*75.9%
sqrt-prod75.9%
metadata-eval75.9%
sqrt-unprod75.9%
add-sqr-sqrt75.9%
metadata-eval75.9%
Applied egg-rr75.9%
Taylor expanded in x around inf 67.0%
Final simplification52.6%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= x 3.9e-228) (/ (- p) x) (+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))))
p = abs(p);
double code(double p, double x) {
double tmp;
if (x <= 3.9e-228) {
tmp = -p / x;
} else {
tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
}
return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.9d-228) then
tmp = -p / x
else
tmp = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if (x <= 3.9e-228) {
tmp = -p / x;
} else {
tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if x <= 3.9e-228: tmp = -p / x else: tmp = 1.0 + (-0.5 * ((p / x) / (x / p))) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (x <= 3.9e-228) tmp = Float64(Float64(-p) / x); else tmp = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p)))); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (x <= 3.9e-228) tmp = -p / x; else tmp = 1.0 + (-0.5 * ((p / x) / (x / p))); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[x, 3.9e-228], N[((-p) / x), $MachinePrecision], N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.9 \cdot 10^{-228}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\
\end{array}
\end{array}
if x < 3.90000000000000029e-228Initial program 51.6%
+-commutative51.6%
distribute-rgt-in51.6%
+-commutative51.6%
add-sqr-sqrt51.6%
hypot-def51.6%
associate-*l*51.6%
sqrt-prod51.6%
metadata-eval51.6%
sqrt-unprod26.6%
add-sqr-sqrt51.6%
metadata-eval51.6%
Applied egg-rr51.6%
Taylor expanded in x around -inf 30.1%
neg-mul-130.1%
distribute-neg-frac30.1%
Simplified30.1%
if 3.90000000000000029e-228 < x Initial program 100.0%
+-commutative100.0%
distribute-rgt-in100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
associate-*l*100.0%
sqrt-prod100.0%
metadata-eval100.0%
sqrt-unprod51.1%
add-sqr-sqrt100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 48.8%
unpow248.8%
unpow248.8%
times-frac48.8%
unpow248.8%
Simplified48.8%
unpow248.8%
clear-num48.8%
un-div-inv48.8%
Applied egg-rr48.8%
Final simplification39.7%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= x -5e-310) (/ (- p) x) (/ p x)))
p = abs(p);
double code(double p, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p / x;
} else {
tmp = p / x;
}
return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = -p / x
else
tmp = p / x
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p / x;
} else {
tmp = p / x;
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if x <= -5e-310: tmp = -p / x else: tmp = p / x return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (x <= -5e-310) tmp = Float64(Float64(-p) / x); else tmp = Float64(p / x); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (x <= -5e-310) tmp = -p / x; else tmp = p / x; end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[x, -5e-310], N[((-p) / x), $MachinePrecision], N[(p / x), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{p}{x}\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 51.6%
+-commutative51.6%
distribute-rgt-in51.6%
+-commutative51.6%
add-sqr-sqrt51.6%
hypot-def51.6%
associate-*l*51.6%
sqrt-prod51.6%
metadata-eval51.6%
sqrt-unprod26.6%
add-sqr-sqrt51.6%
metadata-eval51.6%
Applied egg-rr51.6%
Taylor expanded in x around -inf 30.1%
neg-mul-130.1%
distribute-neg-frac30.1%
Simplified30.1%
if -4.999999999999985e-310 < x Initial program 100.0%
Taylor expanded in x around -inf 4.3%
unpow24.3%
unpow24.3%
times-frac4.6%
Simplified4.6%
Taylor expanded in p around 0 3.3%
Final simplification16.4%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (/ p x))
p = abs(p);
double code(double p, double x) {
return p / x;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = p / x
end function
p = Math.abs(p);
public static double code(double p, double x) {
return p / x;
}
p = abs(p) def code(p, x): return p / x
p = abs(p) function code(p, x) return Float64(p / x) end
p = abs(p) function tmp = code(p, x) tmp = p / x; end
NOTE: p should be positive before calling this function code[p_, x_] := N[(p / x), $MachinePrecision]
\begin{array}{l}
p = |p|\\
\\
\frac{p}{x}
\end{array}
Initial program 76.4%
Taylor expanded in x around -inf 18.2%
unpow218.2%
unpow218.2%
times-frac23.7%
Simplified23.7%
Taylor expanded in p around 0 19.8%
Final simplification19.8%
(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 2023271
(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))))))))