
(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}
(FPCore (p x) :precision binary64 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.5) (sqrt (* 0.5 (* 2.0 (/ (pow p 2.0) (pow x 2.0))))) (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x)))))))
double code(double p, double x) {
double tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) {
tmp = sqrt((0.5 * (2.0 * (pow(p, 2.0) / pow(x, 2.0)))));
} else {
tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
}
return tmp;
}
public static double code(double p, double x) {
double tmp;
if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) {
tmp = Math.sqrt((0.5 * (2.0 * (Math.pow(p, 2.0) / Math.pow(x, 2.0)))));
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
}
return tmp;
}
def code(p, x): tmp = 0 if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5: tmp = math.sqrt((0.5 * (2.0 * (math.pow(p, 2.0) / math.pow(x, 2.0))))) else: tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x))))) return tmp
function code(p, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.5) tmp = sqrt(Float64(0.5 * Float64(2.0 * Float64((p ^ 2.0) / (x ^ 2.0))))); else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x))))); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) tmp = sqrt((0.5 * (2.0 * ((p ^ 2.0) / (x ^ 2.0))))); else tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x))))); end tmp_2 = tmp; end
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[Sqrt[N[(0.5 * N[(2.0 * N[(N[Power[p, 2.0], $MachinePrecision] / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.5:\\
\;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}\\
\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 79.7%
Taylor expanded in x around -inf 86.8%
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-unprod48.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification96.9%
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
}
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x)))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x))))); end
code[p_, x_] := 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}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}
\end{array}
Initial program 95.3%
add-sqr-sqrt95.3%
hypot-def95.3%
associate-*l*95.3%
sqrt-prod95.3%
metadata-eval95.3%
sqrt-unprod46.9%
add-sqr-sqrt95.3%
Applied egg-rr95.3%
Final simplification95.3%
(FPCore (p x)
:precision binary64
(if (<= p 7.8e-305)
(sqrt 0.0)
(if (<= p 2e-262)
1.0
(if (<= p 2.3e-183)
(sqrt 0.0)
(if (<= p 1.56e-132)
1.0
(if (<= p 9.2e-122)
(sqrt 0.0)
(if (<= p 7.2e-59) 1.0 (sqrt 0.5))))))))
double code(double p, double x) {
double tmp;
if (p <= 7.8e-305) {
tmp = sqrt(0.0);
} else if (p <= 2e-262) {
tmp = 1.0;
} else if (p <= 2.3e-183) {
tmp = sqrt(0.0);
} else if (p <= 1.56e-132) {
tmp = 1.0;
} else if (p <= 9.2e-122) {
tmp = sqrt(0.0);
} else if (p <= 7.2e-59) {
tmp = 1.0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (p <= 7.8d-305) then
tmp = sqrt(0.0d0)
else if (p <= 2d-262) then
tmp = 1.0d0
else if (p <= 2.3d-183) then
tmp = sqrt(0.0d0)
else if (p <= 1.56d-132) then
tmp = 1.0d0
else if (p <= 9.2d-122) then
tmp = sqrt(0.0d0)
else if (p <= 7.2d-59) then
tmp = 1.0d0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
public static double code(double p, double x) {
double tmp;
if (p <= 7.8e-305) {
tmp = Math.sqrt(0.0);
} else if (p <= 2e-262) {
tmp = 1.0;
} else if (p <= 2.3e-183) {
tmp = Math.sqrt(0.0);
} else if (p <= 1.56e-132) {
tmp = 1.0;
} else if (p <= 9.2e-122) {
tmp = Math.sqrt(0.0);
} else if (p <= 7.2e-59) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 7.8e-305: tmp = math.sqrt(0.0) elif p <= 2e-262: tmp = 1.0 elif p <= 2.3e-183: tmp = math.sqrt(0.0) elif p <= 1.56e-132: tmp = 1.0 elif p <= 9.2e-122: tmp = math.sqrt(0.0) elif p <= 7.2e-59: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= 7.8e-305) tmp = sqrt(0.0); elseif (p <= 2e-262) tmp = 1.0; elseif (p <= 2.3e-183) tmp = sqrt(0.0); elseif (p <= 1.56e-132) tmp = 1.0; elseif (p <= 9.2e-122) tmp = sqrt(0.0); elseif (p <= 7.2e-59) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 7.8e-305) tmp = sqrt(0.0); elseif (p <= 2e-262) tmp = 1.0; elseif (p <= 2.3e-183) tmp = sqrt(0.0); elseif (p <= 1.56e-132) tmp = 1.0; elseif (p <= 9.2e-122) tmp = sqrt(0.0); elseif (p <= 7.2e-59) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 7.8e-305], N[Sqrt[0.0], $MachinePrecision], If[LessEqual[p, 2e-262], 1.0, If[LessEqual[p, 2.3e-183], N[Sqrt[0.0], $MachinePrecision], If[LessEqual[p, 1.56e-132], 1.0, If[LessEqual[p, 9.2e-122], N[Sqrt[0.0], $MachinePrecision], If[LessEqual[p, 7.2e-59], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 7.8 \cdot 10^{-305}:\\
\;\;\;\;\sqrt{0}\\
\mathbf{elif}\;p \leq 2 \cdot 10^{-262}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 2.3 \cdot 10^{-183}:\\
\;\;\;\;\sqrt{0}\\
\mathbf{elif}\;p \leq 1.56 \cdot 10^{-132}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 9.2 \cdot 10^{-122}:\\
\;\;\;\;\sqrt{0}\\
\mathbf{elif}\;p \leq 7.2 \cdot 10^{-59}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 7.8000000000000005e-305 or 2.00000000000000002e-262 < p < 2.30000000000000016e-183 or 1.56000000000000012e-132 < p < 9.20000000000000028e-122Initial program 94.1%
add-sqr-sqrt94.1%
hypot-def94.1%
associate-*l*94.1%
sqrt-prod94.1%
metadata-eval94.1%
sqrt-unprod14.7%
add-sqr-sqrt94.1%
Applied egg-rr94.1%
Taylor expanded in x around -inf 26.7%
if 7.8000000000000005e-305 < p < 2.00000000000000002e-262 or 2.30000000000000016e-183 < p < 1.56000000000000012e-132 or 9.20000000000000028e-122 < p < 7.20000000000000001e-59Initial program 96.8%
Taylor expanded in x around inf 66.0%
if 7.20000000000000001e-59 < p Initial program 97.2%
Taylor expanded in x around 0 88.9%
Final simplification48.3%
(FPCore (p x) :precision binary64 (if (<= p 2.8e-178) (/ (- p) x) (sqrt 0.5)))
double code(double p, double x) {
double tmp;
if (p <= 2.8e-178) {
tmp = -p / x;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (p <= 2.8d-178) then
tmp = -p / x
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
public static double code(double p, double x) {
double tmp;
if (p <= 2.8e-178) {
tmp = -p / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 2.8e-178: tmp = -p / x else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= 2.8e-178) tmp = Float64(Float64(-p) / x); else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 2.8e-178) tmp = -p / x; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 2.8e-178], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 2.8 \cdot 10^{-178}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 2.80000000000000019e-178Initial program 94.3%
Taylor expanded in x around -inf 31.0%
Taylor expanded in p around -inf 8.0%
associate-*r/8.0%
mul-1-neg8.0%
Simplified8.0%
if 2.80000000000000019e-178 < p Initial program 96.9%
Taylor expanded in x around 0 72.3%
Final simplification31.4%
(FPCore (p x) :precision binary64 (if (<= p 1.46e-58) 1.0 (sqrt 0.5)))
double code(double p, double x) {
double tmp;
if (p <= 1.46e-58) {
tmp = 1.0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (p <= 1.46d-58) then
tmp = 1.0d0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
public static double code(double p, double x) {
double tmp;
if (p <= 1.46e-58) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 1.46e-58: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= 1.46e-58) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 1.46e-58) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 1.46e-58], 1.0, N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 1.46 \cdot 10^{-58}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 1.4600000000000001e-58Initial program 94.5%
Taylor expanded in x around inf 39.7%
if 1.4600000000000001e-58 < p Initial program 97.2%
Taylor expanded in x around 0 88.9%
Final simplification53.2%
(FPCore (p x) :precision binary64 (if (<= x -5e-310) (/ (- p) x) (/ p x)))
double code(double p, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p / x;
} else {
tmp = p / x;
}
return tmp;
}
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
public static double code(double p, double x) {
double tmp;
if (x <= -5e-310) {
tmp = -p / x;
} else {
tmp = p / x;
}
return tmp;
}
def code(p, x): tmp = 0 if x <= -5e-310: tmp = -p / x else: tmp = p / x return tmp
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
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
code[p_, x_] := If[LessEqual[x, -5e-310], N[((-p) / x), $MachinePrecision], N[(p / x), $MachinePrecision]]
\begin{array}{l}
\\
\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 90.7%
Taylor expanded in x around -inf 42.2%
Taylor expanded in p around -inf 10.8%
associate-*r/10.8%
mul-1-neg10.8%
Simplified10.8%
if -4.999999999999985e-310 < x Initial program 100.0%
Taylor expanded in x around -inf 5.0%
Taylor expanded in p around 0 3.5%
Final simplification7.2%
(FPCore (p x) :precision binary64 (/ p x))
double code(double p, double x) {
return p / x;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
code = p / x
end function
public static double code(double p, double x) {
return p / x;
}
def code(p, x): return p / x
function code(p, x) return Float64(p / x) end
function tmp = code(p, x) tmp = p / x; end
code[p_, x_] := N[(p / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{p}{x}
\end{array}
Initial program 95.3%
Taylor expanded in x around -inf 24.0%
Taylor expanded in p around 0 10.0%
Final simplification10.0%
(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 2024031
(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))))))))