
(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 5 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 (let* ((t_0 (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))))) (if (<= t_0 -1.0) (/ (- p) x) (sqrt (* 0.5 (+ t_0 1.0))))))
p = abs(p);
double code(double p, double x) {
double t_0 = x / sqrt(((p * (4.0 * p)) + (x * x)));
double tmp;
if (t_0 <= -1.0) {
tmp = -p / x;
} else {
tmp = sqrt((0.5 * (t_0 + 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) :: t_0
real(8) :: tmp
t_0 = x / sqrt(((p * (4.0d0 * p)) + (x * x)))
if (t_0 <= (-1.0d0)) then
tmp = -p / x
else
tmp = sqrt((0.5d0 * (t_0 + 1.0d0)))
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double t_0 = x / Math.sqrt(((p * (4.0 * p)) + (x * x)));
double tmp;
if (t_0 <= -1.0) {
tmp = -p / x;
} else {
tmp = Math.sqrt((0.5 * (t_0 + 1.0)));
}
return tmp;
}
p = abs(p) def code(p, x): t_0 = x / math.sqrt(((p * (4.0 * p)) + (x * x))) tmp = 0 if t_0 <= -1.0: tmp = -p / x else: tmp = math.sqrt((0.5 * (t_0 + 1.0))) return tmp
p = abs(p) function code(p, x) t_0 = Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) tmp = 0.0 if (t_0 <= -1.0) tmp = Float64(Float64(-p) / x); else tmp = sqrt(Float64(0.5 * Float64(t_0 + 1.0))); end return tmp end
p = abs(p) function tmp_2 = code(p, x) t_0 = x / sqrt(((p * (4.0 * p)) + (x * x))); tmp = 0.0; if (t_0 <= -1.0) tmp = -p / x; else tmp = sqrt((0.5 * (t_0 + 1.0))); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1.0], N[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\\
\mathbf{if}\;t_0 \leq -1:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(t_0 + 1\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 14.8%
Taylor expanded in x around -inf 57.4%
Taylor expanded in p around -inf 57.4%
associate-*r/57.4%
mul-1-neg57.4%
Simplified57.4%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 100.0%
Final simplification88.7%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (or (<= x -6.6e+42) (not (<= x -3.2e-18))) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x))))) (/ (- p) x)))
p = abs(p);
double code(double p, double x) {
double tmp;
if ((x <= -6.6e+42) || !(x <= -3.2e-18)) {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
} else {
tmp = -p / x;
}
return tmp;
}
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if ((x <= -6.6e+42) || !(x <= -3.2e-18)) {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
} else {
tmp = -p / x;
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if (x <= -6.6e+42) or not (x <= -3.2e-18): tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x))))) else: tmp = -p / x return tmp
p = abs(p) function code(p, x) tmp = 0.0 if ((x <= -6.6e+42) || !(x <= -3.2e-18)) tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x))))); else tmp = Float64(Float64(-p) / x); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if ((x <= -6.6e+42) || ~((x <= -3.2e-18))) tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x))))); else tmp = -p / x; end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[Or[LessEqual[x, -6.6e+42], N[Not[LessEqual[x, -3.2e-18]], $MachinePrecision]], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[((-p) / x), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.6 \cdot 10^{+42} \lor \neg \left(x \leq -3.2 \cdot 10^{-18}\right):\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{-p}{x}\\
\end{array}
\end{array}
if x < -6.5999999999999998e42 or -3.1999999999999999e-18 < x Initial program 83.8%
add-sqr-sqrt83.8%
hypot-def83.8%
associate-*l*83.8%
sqrt-prod83.8%
metadata-eval83.8%
sqrt-unprod43.9%
add-sqr-sqrt83.8%
Applied egg-rr83.8%
if -6.5999999999999998e42 < x < -3.1999999999999999e-18Initial program 36.9%
Taylor expanded in x around -inf 42.4%
Taylor expanded in p around -inf 30.1%
associate-*r/30.1%
mul-1-neg30.1%
Simplified30.1%
Final simplification76.4%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(let* ((t_0 (/ (- p) x)))
(if (<= p 3.1e-237)
1.0
(if (<= p 9.6e-222)
t_0
(if (<= p 1.15e-189)
1.0
(if (or (<= p 9.2e-55) (and (not (<= p 4.2e-24)) (<= p 350000.0)))
t_0
(sqrt 0.5)))))))p = abs(p);
double code(double p, double x) {
double t_0 = -p / x;
double tmp;
if (p <= 3.1e-237) {
tmp = 1.0;
} else if (p <= 9.6e-222) {
tmp = t_0;
} else if (p <= 1.15e-189) {
tmp = 1.0;
} else if ((p <= 9.2e-55) || (!(p <= 4.2e-24) && (p <= 350000.0))) {
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 = -p / x
if (p <= 3.1d-237) then
tmp = 1.0d0
else if (p <= 9.6d-222) then
tmp = t_0
else if (p <= 1.15d-189) then
tmp = 1.0d0
else if ((p <= 9.2d-55) .or. (.not. (p <= 4.2d-24)) .and. (p <= 350000.0d0)) 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 = -p / x;
double tmp;
if (p <= 3.1e-237) {
tmp = 1.0;
} else if (p <= 9.6e-222) {
tmp = t_0;
} else if (p <= 1.15e-189) {
tmp = 1.0;
} else if ((p <= 9.2e-55) || (!(p <= 4.2e-24) && (p <= 350000.0))) {
tmp = t_0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): t_0 = -p / x tmp = 0 if p <= 3.1e-237: tmp = 1.0 elif p <= 9.6e-222: tmp = t_0 elif p <= 1.15e-189: tmp = 1.0 elif (p <= 9.2e-55) or (not (p <= 4.2e-24) and (p <= 350000.0)): tmp = t_0 else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) t_0 = Float64(Float64(-p) / x) tmp = 0.0 if (p <= 3.1e-237) tmp = 1.0; elseif (p <= 9.6e-222) tmp = t_0; elseif (p <= 1.15e-189) tmp = 1.0; elseif ((p <= 9.2e-55) || (!(p <= 4.2e-24) && (p <= 350000.0))) tmp = t_0; else tmp = sqrt(0.5); end return tmp end
p = abs(p) function tmp_2 = code(p, x) t_0 = -p / x; tmp = 0.0; if (p <= 3.1e-237) tmp = 1.0; elseif (p <= 9.6e-222) tmp = t_0; elseif (p <= 1.15e-189) tmp = 1.0; elseif ((p <= 9.2e-55) || (~((p <= 4.2e-24)) && (p <= 350000.0))) 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[((-p) / x), $MachinePrecision]}, If[LessEqual[p, 3.1e-237], 1.0, If[LessEqual[p, 9.6e-222], t$95$0, If[LessEqual[p, 1.15e-189], 1.0, If[Or[LessEqual[p, 9.2e-55], And[N[Not[LessEqual[p, 4.2e-24]], $MachinePrecision], LessEqual[p, 350000.0]]], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{-p}{x}\\
\mathbf{if}\;p \leq 3.1 \cdot 10^{-237}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 9.6 \cdot 10^{-222}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p \leq 1.15 \cdot 10^{-189}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 9.2 \cdot 10^{-55} \lor \neg \left(p \leq 4.2 \cdot 10^{-24}\right) \land p \leq 350000:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 3.0999999999999998e-237 or 9.59999999999999972e-222 < p < 1.1499999999999999e-189Initial program 74.9%
add-sqr-sqrt74.9%
hypot-def74.9%
associate-*l*74.9%
sqrt-prod74.9%
metadata-eval74.9%
sqrt-unprod13.0%
add-sqr-sqrt74.9%
Applied egg-rr74.9%
add-sqr-sqrt74.3%
pow274.3%
pow1/274.3%
sqrt-pow174.3%
distribute-lft-in74.3%
metadata-eval74.3%
associate-*r/74.3%
metadata-eval74.3%
Applied egg-rr74.3%
Taylor expanded in x around inf 39.8%
if 3.0999999999999998e-237 < p < 9.59999999999999972e-222 or 1.1499999999999999e-189 < p < 9.20000000000000046e-55 or 4.1999999999999999e-24 < p < 3.5e5Initial program 44.5%
Taylor expanded in x around -inf 39.4%
Taylor expanded in p around -inf 62.3%
associate-*r/62.3%
mul-1-neg62.3%
Simplified62.3%
if 9.20000000000000046e-55 < p < 4.1999999999999999e-24 or 3.5e5 < p Initial program 96.1%
Taylor expanded in x around 0 87.8%
Final simplification56.4%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= x -3.2e-150) (/ (- p) x) 1.0))
p = abs(p);
double code(double p, double x) {
double tmp;
if (x <= -3.2e-150) {
tmp = -p / x;
} 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 (x <= (-3.2d-150)) then
tmp = -p / x
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 (x <= -3.2e-150) {
tmp = -p / x;
} else {
tmp = 1.0;
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if x <= -3.2e-150: tmp = -p / x else: tmp = 1.0 return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (x <= -3.2e-150) tmp = Float64(Float64(-p) / x); else tmp = 1.0; end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (x <= -3.2e-150) tmp = -p / x; else tmp = 1.0; end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[x, -3.2e-150], N[((-p) / x), $MachinePrecision], 1.0]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-150}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -3.1999999999999998e-150Initial program 55.4%
Taylor expanded in x around -inf 32.0%
Taylor expanded in p around -inf 31.5%
associate-*r/31.5%
mul-1-neg31.5%
Simplified31.5%
if -3.1999999999999998e-150 < x Initial program 100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
associate-*l*100.0%
sqrt-prod100.0%
metadata-eval100.0%
sqrt-unprod51.6%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
add-sqr-sqrt99.2%
pow299.2%
pow1/299.2%
sqrt-pow199.2%
distribute-lft-in99.2%
metadata-eval99.2%
associate-*r/99.2%
metadata-eval99.2%
Applied egg-rr99.2%
Taylor expanded in x around inf 59.4%
Final simplification45.3%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 1.0)
p = abs(p);
double code(double p, double x) {
return 1.0;
}
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 = 1.0d0
end function
p = Math.abs(p);
public static double code(double p, double x) {
return 1.0;
}
p = abs(p) def code(p, x): return 1.0
p = abs(p) function code(p, x) return 1.0 end
p = abs(p) function tmp = code(p, x) tmp = 1.0; end
NOTE: p should be positive before calling this function code[p_, x_] := 1.0
\begin{array}{l}
p = |p|\\
\\
1
\end{array}
Initial program 77.4%
add-sqr-sqrt77.4%
hypot-def77.3%
associate-*l*77.3%
sqrt-prod77.3%
metadata-eval77.3%
sqrt-unprod40.8%
add-sqr-sqrt77.3%
Applied egg-rr77.3%
add-sqr-sqrt76.6%
pow276.6%
pow1/276.6%
sqrt-pow176.6%
distribute-lft-in76.6%
metadata-eval76.6%
associate-*r/76.6%
metadata-eval76.6%
Applied egg-rr76.6%
Taylor expanded in x around inf 35.4%
Final simplification35.4%
(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 2023299
(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))))))))