
(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 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.5) (- (* -0.25 (/ -6.0 (pow (/ x p) 3.0))) (/ p x)) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x)))))))
p = abs(p);
double code(double p, double x) {
double tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) {
tmp = (-0.25 * (-6.0 / pow((x / p), 3.0))) - (p / x);
} else {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
}
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)))) <= -0.5) {
tmp = (-0.25 * (-6.0 / Math.pow((x / p), 3.0))) - (p / x);
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5: tmp = (-0.25 * (-6.0 / math.pow((x / p), 3.0))) - (p / x) else: tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x))))) 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)))) <= -0.5) tmp = Float64(Float64(-0.25 * Float64(-6.0 / (Float64(x / p) ^ 3.0))) - Float64(p / x)); else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x))))); 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)))) <= -0.5) tmp = (-0.25 * (-6.0 / ((x / p) ^ 3.0))) - (p / x); else tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x))))); 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], -0.5], N[(N[(-0.25 * N[(-6.0 / N[Power[N[(x / p), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(p / x), $MachinePrecision]), $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]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.5:\\
\;\;\;\;-0.25 \cdot \frac{-6}{{\left(\frac{x}{p}\right)}^{3}} - \frac{p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.5Initial program 28.6%
Simplified28.2%
clear-num28.2%
fma-udef28.6%
*-commutative28.6%
+-commutative28.6%
inv-pow28.6%
Applied egg-rr28.3%
pow1/228.3%
distribute-lft-in28.3%
metadata-eval28.3%
unpow-128.3%
un-div-inv28.3%
Applied egg-rr28.3%
Taylor expanded in x around -inf 43.5%
+-commutative43.5%
mul-1-neg43.5%
unsub-neg43.5%
distribute-rgt-out43.5%
metadata-eval43.5%
*-commutative43.5%
times-frac53.9%
Simplified53.9%
Taylor expanded in p around 0 57.9%
associate-*r/57.9%
associate-/l*57.9%
cube-div60.0%
Simplified60.0%
if -0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
associate-*l*100.0%
sqrt-prod100.0%
metadata-eval100.0%
sqrt-unprod52.7%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification92.3%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(if (<= p 7.6e-179)
(pow 1.0 0.5)
(if (<= p 5.4e-87)
(- (* -0.25 (/ -6.0 (pow (/ x p) 3.0))) (/ p x))
(sqrt 0.5))))p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 7.6e-179) {
tmp = pow(1.0, 0.5);
} else if (p <= 5.4e-87) {
tmp = (-0.25 * (-6.0 / pow((x / p), 3.0))) - (p / x);
} 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 <= 7.6d-179) then
tmp = 1.0d0 ** 0.5d0
else if (p <= 5.4d-87) then
tmp = ((-0.25d0) * ((-6.0d0) / ((x / p) ** 3.0d0))) - (p / x)
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 <= 7.6e-179) {
tmp = Math.pow(1.0, 0.5);
} else if (p <= 5.4e-87) {
tmp = (-0.25 * (-6.0 / Math.pow((x / p), 3.0))) - (p / x);
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 7.6e-179: tmp = math.pow(1.0, 0.5) elif p <= 5.4e-87: tmp = (-0.25 * (-6.0 / math.pow((x / p), 3.0))) - (p / x) else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 7.6e-179) tmp = 1.0 ^ 0.5; elseif (p <= 5.4e-87) tmp = Float64(Float64(-0.25 * Float64(-6.0 / (Float64(x / p) ^ 3.0))) - Float64(p / x)); else tmp = sqrt(0.5); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (p <= 7.6e-179) tmp = 1.0 ^ 0.5; elseif (p <= 5.4e-87) tmp = (-0.25 * (-6.0 / ((x / p) ^ 3.0))) - (p / x); 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, 7.6e-179], N[Power[1.0, 0.5], $MachinePrecision], If[LessEqual[p, 5.4e-87], N[(N[(-0.25 * N[(-6.0 / N[Power[N[(x / p), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(p / x), $MachinePrecision]), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 7.6 \cdot 10^{-179}:\\
\;\;\;\;{1}^{0.5}\\
\mathbf{elif}\;p \leq 5.4 \cdot 10^{-87}:\\
\;\;\;\;-0.25 \cdot \frac{-6}{{\left(\frac{x}{p}\right)}^{3}} - \frac{p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 7.59999999999999947e-179Initial program 85.3%
Simplified85.3%
clear-num85.3%
fma-udef85.3%
*-commutative85.3%
+-commutative85.3%
inv-pow85.3%
Applied egg-rr85.3%
pow1/285.3%
distribute-lft-in85.3%
metadata-eval85.3%
unpow-185.3%
un-div-inv85.3%
Applied egg-rr85.3%
Taylor expanded in x around inf 38.5%
if 7.59999999999999947e-179 < p < 5.39999999999999967e-87Initial program 52.4%
Simplified52.4%
clear-num52.4%
fma-udef52.4%
*-commutative52.4%
+-commutative52.4%
inv-pow52.4%
Applied egg-rr52.4%
pow1/252.4%
distribute-lft-in52.4%
metadata-eval52.4%
unpow-152.4%
un-div-inv52.4%
Applied egg-rr52.4%
Taylor expanded in x around -inf 32.6%
+-commutative32.6%
mul-1-neg32.6%
unsub-neg32.6%
distribute-rgt-out32.6%
metadata-eval32.6%
*-commutative32.6%
times-frac46.5%
Simplified46.5%
Taylor expanded in p around 0 46.5%
associate-*r/46.5%
associate-/l*46.5%
cube-div51.4%
Simplified51.4%
if 5.39999999999999967e-87 < p Initial program 96.0%
Taylor expanded in x around 0 86.9%
Final simplification57.0%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= p 2.15e-179) (pow 1.0 0.5) (if (<= p 5e-87) (/ (- p) x) (sqrt 0.5))))
p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 2.15e-179) {
tmp = pow(1.0, 0.5);
} else if (p <= 5e-87) {
tmp = -p / x;
} 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 <= 2.15d-179) then
tmp = 1.0d0 ** 0.5d0
else if (p <= 5d-87) then
tmp = -p / x
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 <= 2.15e-179) {
tmp = Math.pow(1.0, 0.5);
} else if (p <= 5e-87) {
tmp = -p / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 2.15e-179: tmp = math.pow(1.0, 0.5) elif p <= 5e-87: tmp = -p / x else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 2.15e-179) tmp = 1.0 ^ 0.5; elseif (p <= 5e-87) tmp = Float64(Float64(-p) / x); else tmp = sqrt(0.5); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (p <= 2.15e-179) tmp = 1.0 ^ 0.5; elseif (p <= 5e-87) tmp = -p / x; 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, 2.15e-179], N[Power[1.0, 0.5], $MachinePrecision], If[LessEqual[p, 5e-87], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 2.15 \cdot 10^{-179}:\\
\;\;\;\;{1}^{0.5}\\
\mathbf{elif}\;p \leq 5 \cdot 10^{-87}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 2.15000000000000013e-179Initial program 85.3%
Simplified85.3%
clear-num85.3%
fma-udef85.3%
*-commutative85.3%
+-commutative85.3%
inv-pow85.3%
Applied egg-rr85.3%
pow1/285.3%
distribute-lft-in85.3%
metadata-eval85.3%
unpow-185.3%
un-div-inv85.3%
Applied egg-rr85.3%
Taylor expanded in x around inf 38.5%
if 2.15000000000000013e-179 < p < 5.00000000000000042e-87Initial program 52.4%
Simplified52.4%
clear-num52.4%
fma-udef52.4%
*-commutative52.4%
+-commutative52.4%
inv-pow52.4%
Applied egg-rr52.4%
pow1/252.4%
distribute-lft-in52.4%
metadata-eval52.4%
unpow-152.4%
un-div-inv52.4%
Applied egg-rr52.4%
Taylor expanded in x around -inf 51.1%
associate-*r/51.1%
mul-1-neg51.1%
Simplified51.1%
if 5.00000000000000042e-87 < p Initial program 96.0%
Taylor expanded in x around 0 86.9%
Final simplification57.0%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= p 4.2e-87) (/ (- p) x) (sqrt 0.5)))
p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 4.2e-87) {
tmp = -p / x;
} 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 <= 4.2d-87) then
tmp = -p / x
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 <= 4.2e-87) {
tmp = -p / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 4.2e-87: tmp = -p / x else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 4.2e-87) tmp = Float64(Float64(-p) / x); else tmp = sqrt(0.5); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (p <= 4.2e-87) tmp = -p / x; 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, 4.2e-87], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 4.2 \cdot 10^{-87}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 4.20000000000000014e-87Initial program 80.9%
Simplified80.9%
clear-num80.9%
fma-udef80.9%
*-commutative80.9%
+-commutative80.9%
inv-pow80.9%
Applied egg-rr80.9%
pow1/280.9%
distribute-lft-in80.9%
metadata-eval80.9%
unpow-180.9%
un-div-inv80.9%
Applied egg-rr80.9%
Taylor expanded in x around -inf 17.5%
associate-*r/17.5%
mul-1-neg17.5%
Simplified17.5%
if 4.20000000000000014e-87 < p Initial program 96.0%
Taylor expanded in x around 0 86.9%
Final simplification42.5%
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(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 86.3%
Simplified86.3%
clear-num86.3%
fma-udef86.3%
*-commutative86.3%
+-commutative86.3%
inv-pow86.3%
Applied egg-rr86.3%
pow1/286.3%
distribute-lft-in86.3%
metadata-eval86.3%
unpow-186.3%
un-div-inv86.3%
Applied egg-rr86.3%
Taylor expanded in x around -inf 13.9%
associate-*r/13.9%
mul-1-neg13.9%
Simplified13.9%
Final simplification13.9%
(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 2023320
(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))))))))