
(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}
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (log (exp (+ 1.0 (/ x (hypot x (* 2.0 p)))))))))
double code(double p, double x) {
return sqrt((0.5 * log(exp((1.0 + (x / hypot(x, (2.0 * p))))))));
}
public static double code(double p, double x) {
return Math.sqrt((0.5 * Math.log(Math.exp((1.0 + (x / Math.hypot(x, (2.0 * p))))))));
}
def code(p, x): return math.sqrt((0.5 * math.log(math.exp((1.0 + (x / math.hypot(x, (2.0 * p))))))))
function code(p, x) return sqrt(Float64(0.5 * log(exp(Float64(1.0 + Float64(x / hypot(x, Float64(2.0 * p)))))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * log(exp((1.0 + (x / hypot(x, (2.0 * p)))))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[Log[N[Exp[N[(1.0 + N[(x / N[Sqrt[x ^ 2 + N[(2.0 * p), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \log \left(e^{1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)}
\end{array}
Initial program 97.9%
add-log-exp97.9%
+-commutative97.9%
add-sqr-sqrt97.9%
hypot-define97.9%
associate-*l*97.9%
sqrt-prod97.9%
metadata-eval97.9%
sqrt-unprod45.3%
add-sqr-sqrt97.9%
Applied egg-rr97.9%
Final simplification97.9%
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* 2.0 p) x))))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / hypot((2.0 * p), x)))));
}
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.hypot((2.0 * p), x)))));
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.hypot((2.0 * p), x)))))
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(2.0 * p), x))))) end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / hypot((2.0 * p), x))))); end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(2.0 * p), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}
\end{array}
Initial program 97.9%
add-sqr-sqrt97.9%
hypot-define97.9%
associate-*l*97.9%
sqrt-prod97.9%
metadata-eval97.9%
sqrt-unprod45.2%
add-sqr-sqrt97.9%
Applied egg-rr97.9%
Final simplification97.9%
(FPCore (p x)
:precision binary64
(if (<= p 2.65e-138)
1.0
(if (<= p 7.5e-93)
(sqrt 0.0)
(if (<= p 1.35e-5) 1.0 (sqrt (* 0.5 (+ 1.0 (/ x (* 2.0 p)))))))))
double code(double p, double x) {
double tmp;
if (p <= 2.65e-138) {
tmp = 1.0;
} else if (p <= 7.5e-93) {
tmp = sqrt(0.0);
} else if (p <= 1.35e-5) {
tmp = 1.0;
} else {
tmp = sqrt((0.5 * (1.0 + (x / (2.0 * p)))));
}
return tmp;
}
real(8) function code(p, x)
real(8), intent (in) :: p
real(8), intent (in) :: x
real(8) :: tmp
if (p <= 2.65d-138) then
tmp = 1.0d0
else if (p <= 7.5d-93) then
tmp = sqrt(0.0d0)
else if (p <= 1.35d-5) then
tmp = 1.0d0
else
tmp = sqrt((0.5d0 * (1.0d0 + (x / (2.0d0 * p)))))
end if
code = tmp
end function
public static double code(double p, double x) {
double tmp;
if (p <= 2.65e-138) {
tmp = 1.0;
} else if (p <= 7.5e-93) {
tmp = Math.sqrt(0.0);
} else if (p <= 1.35e-5) {
tmp = 1.0;
} else {
tmp = Math.sqrt((0.5 * (1.0 + (x / (2.0 * p)))));
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 2.65e-138: tmp = 1.0 elif p <= 7.5e-93: tmp = math.sqrt(0.0) elif p <= 1.35e-5: tmp = 1.0 else: tmp = math.sqrt((0.5 * (1.0 + (x / (2.0 * p))))) return tmp
function code(p, x) tmp = 0.0 if (p <= 2.65e-138) tmp = 1.0; elseif (p <= 7.5e-93) tmp = sqrt(0.0); elseif (p <= 1.35e-5) tmp = 1.0; else tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / Float64(2.0 * p))))); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 2.65e-138) tmp = 1.0; elseif (p <= 7.5e-93) tmp = sqrt(0.0); elseif (p <= 1.35e-5) tmp = 1.0; else tmp = sqrt((0.5 * (1.0 + (x / (2.0 * p))))); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 2.65e-138], 1.0, If[LessEqual[p, 7.5e-93], N[Sqrt[0.0], $MachinePrecision], If[LessEqual[p, 1.35e-5], 1.0, N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[(2.0 * p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 2.65 \cdot 10^{-138}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 7.5 \cdot 10^{-93}:\\
\;\;\;\;\sqrt{0}\\
\mathbf{elif}\;p \leq 1.35 \cdot 10^{-5}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{2 \cdot p}\right)}\\
\end{array}
\end{array}
if p < 2.65000000000000013e-138 or 7.50000000000000034e-93 < p < 1.3499999999999999e-5Initial program 98.2%
Taylor expanded in x around inf 42.3%
if 2.65000000000000013e-138 < p < 7.50000000000000034e-93Initial program 100.0%
Taylor expanded in x around -inf 63.7%
if 1.3499999999999999e-5 < p Initial program 96.2%
Taylor expanded in p around inf 85.5%
Final simplification53.4%
(FPCore (p x) :precision binary64 (if (<= p 1.15e-137) 1.0 (if (<= p 1.15e-92) (sqrt 0.0) (if (<= p 2.1e-6) 1.0 (sqrt 0.5)))))
double code(double p, double x) {
double tmp;
if (p <= 1.15e-137) {
tmp = 1.0;
} else if (p <= 1.15e-92) {
tmp = sqrt(0.0);
} else if (p <= 2.1e-6) {
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.15d-137) then
tmp = 1.0d0
else if (p <= 1.15d-92) then
tmp = sqrt(0.0d0)
else if (p <= 2.1d-6) 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.15e-137) {
tmp = 1.0;
} else if (p <= 1.15e-92) {
tmp = Math.sqrt(0.0);
} else if (p <= 2.1e-6) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 1.15e-137: tmp = 1.0 elif p <= 1.15e-92: tmp = math.sqrt(0.0) elif p <= 2.1e-6: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= 1.15e-137) tmp = 1.0; elseif (p <= 1.15e-92) tmp = sqrt(0.0); elseif (p <= 2.1e-6) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 1.15e-137) tmp = 1.0; elseif (p <= 1.15e-92) tmp = sqrt(0.0); elseif (p <= 2.1e-6) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 1.15e-137], 1.0, If[LessEqual[p, 1.15e-92], N[Sqrt[0.0], $MachinePrecision], If[LessEqual[p, 2.1e-6], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 1.15 \cdot 10^{-137}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 1.15 \cdot 10^{-92}:\\
\;\;\;\;\sqrt{0}\\
\mathbf{elif}\;p \leq 2.1 \cdot 10^{-6}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 1.15000000000000004e-137 or 1.15000000000000008e-92 < p < 2.0999999999999998e-6Initial program 98.2%
Taylor expanded in x around inf 42.3%
if 1.15000000000000004e-137 < p < 1.15000000000000008e-92Initial program 100.0%
Taylor expanded in x around -inf 63.7%
if 2.0999999999999998e-6 < p Initial program 96.2%
Taylor expanded in x around 0 84.6%
Final simplification53.2%
(FPCore (p x) :precision binary64 (if (<= p 7.5e-161) (/ p (- x)) (sqrt 0.5)))
double code(double p, double x) {
double tmp;
if (p <= 7.5e-161) {
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 <= 7.5d-161) 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 <= 7.5e-161) {
tmp = p / -x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 7.5e-161: tmp = p / -x else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= 7.5e-161) tmp = Float64(p / Float64(-x)); else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 7.5e-161) tmp = p / -x; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 7.5e-161], N[(p / (-x)), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 7.5 \cdot 10^{-161}:\\
\;\;\;\;\frac{p}{-x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 7.49999999999999991e-161Initial program 98.0%
Taylor expanded in x around -inf 9.8%
mul-1-neg9.8%
associate-/l*9.8%
distribute-lft-neg-in9.8%
associate-/l*9.8%
Simplified9.8%
distribute-lft-neg-out9.8%
neg-sub09.8%
associate-*r/9.8%
sqrt-unprod9.8%
metadata-eval9.8%
metadata-eval9.8%
associate-*r/9.8%
*-commutative9.8%
*-un-lft-identity9.8%
Applied egg-rr9.8%
neg-sub09.8%
distribute-frac-neg9.8%
Simplified9.8%
if 7.49999999999999991e-161 < p Initial program 97.7%
Taylor expanded in x around 0 60.2%
Final simplification28.7%
(FPCore (p x) :precision binary64 (if (<= p 2.1e-6) 1.0 (sqrt 0.5)))
double code(double p, double x) {
double tmp;
if (p <= 2.1e-6) {
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 <= 2.1d-6) 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 <= 2.1e-6) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= 2.1e-6: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= 2.1e-6) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= 2.1e-6) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, 2.1e-6], 1.0, N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq 2.1 \cdot 10^{-6}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 2.0999999999999998e-6Initial program 98.4%
Taylor expanded in x around inf 40.8%
if 2.0999999999999998e-6 < p Initial program 96.2%
Taylor expanded in x around 0 84.6%
Final simplification50.7%
(FPCore (p x) :precision binary64 (if (<= x -7e-147) (/ p (- x)) (/ p x)))
double code(double p, double x) {
double tmp;
if (x <= -7e-147) {
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 <= (-7d-147)) 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 <= -7e-147) {
tmp = p / -x;
} else {
tmp = p / x;
}
return tmp;
}
def code(p, x): tmp = 0 if x <= -7e-147: tmp = p / -x else: tmp = p / x return tmp
function code(p, x) tmp = 0.0 if (x <= -7e-147) tmp = Float64(p / Float64(-x)); else tmp = Float64(p / x); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (x <= -7e-147) tmp = p / -x; else tmp = p / x; end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[x, -7e-147], N[(p / (-x)), $MachinePrecision], N[(p / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7 \cdot 10^{-147}:\\
\;\;\;\;\frac{p}{-x}\\
\mathbf{else}:\\
\;\;\;\;\frac{p}{x}\\
\end{array}
\end{array}
if x < -7.00000000000000007e-147Initial program 95.6%
Taylor expanded in x around -inf 13.6%
mul-1-neg13.6%
associate-/l*13.6%
distribute-lft-neg-in13.6%
associate-/l*13.6%
Simplified13.6%
distribute-lft-neg-out13.6%
neg-sub013.6%
associate-*r/13.6%
sqrt-unprod13.6%
metadata-eval13.6%
metadata-eval13.6%
associate-*r/13.7%
*-commutative13.7%
*-un-lft-identity13.7%
Applied egg-rr13.7%
neg-sub013.7%
distribute-frac-neg13.7%
Simplified13.7%
if -7.00000000000000007e-147 < x Initial program 100.0%
Taylor expanded in x around -inf 3.6%
mul-1-neg3.6%
associate-/l*3.6%
distribute-lft-neg-in3.6%
associate-/l*3.6%
Simplified3.6%
*-commutative3.6%
associate-*r/3.6%
sqrt-unprod3.6%
metadata-eval3.6%
metadata-eval3.6%
associate-*l/3.6%
*-un-lft-identity3.6%
add-sqr-sqrt2.9%
sqrt-unprod5.3%
sqr-neg5.3%
sqrt-unprod2.1%
add-sqr-sqrt3.0%
Applied egg-rr3.0%
Final simplification8.1%
(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 97.9%
Taylor expanded in x around -inf 8.4%
mul-1-neg8.4%
associate-/l*8.4%
distribute-lft-neg-in8.4%
associate-/l*8.4%
Simplified8.4%
*-commutative8.4%
associate-*r/8.4%
sqrt-unprod8.4%
metadata-eval8.4%
metadata-eval8.4%
associate-*l/8.4%
*-un-lft-identity8.4%
add-sqr-sqrt4.3%
sqrt-unprod13.0%
sqr-neg13.0%
sqrt-unprod2.9%
add-sqr-sqrt7.6%
Applied egg-rr7.6%
Final simplification7.6%
(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 2024052
(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))))))))