
(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 6 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) (/ (- 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 = -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 = -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 = -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(-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 = -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[((-p) / x), $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:\\
\;\;\;\;\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 14.8%
Taylor expanded in x around -inf 53.4%
unpow253.4%
unpow253.4%
times-frac62.9%
Simplified62.9%
Taylor expanded in p around -inf 62.4%
associate-*r/62.4%
neg-mul-162.4%
Simplified62.4%
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-unprod47.3%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification89.4%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(let* ((t_0 (/ (- p) x)))
(if (<= p 2.5e-188)
t_0
(if (<= p 6.8e-152)
1.0
(if (<= p 2.15e-113)
t_0
(if (<= p 7.8e-89) 1.0 (if (<= p 1.2e-41) t_0 (sqrt 0.5))))))))p = abs(p);
double code(double p, double x) {
double t_0 = -p / x;
double tmp;
if (p <= 2.5e-188) {
tmp = t_0;
} else if (p <= 6.8e-152) {
tmp = 1.0;
} else if (p <= 2.15e-113) {
tmp = t_0;
} else if (p <= 7.8e-89) {
tmp = 1.0;
} else if (p <= 1.2e-41) {
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 <= 2.5d-188) then
tmp = t_0
else if (p <= 6.8d-152) then
tmp = 1.0d0
else if (p <= 2.15d-113) then
tmp = t_0
else if (p <= 7.8d-89) then
tmp = 1.0d0
else if (p <= 1.2d-41) 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 <= 2.5e-188) {
tmp = t_0;
} else if (p <= 6.8e-152) {
tmp = 1.0;
} else if (p <= 2.15e-113) {
tmp = t_0;
} else if (p <= 7.8e-89) {
tmp = 1.0;
} else if (p <= 1.2e-41) {
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 <= 2.5e-188: tmp = t_0 elif p <= 6.8e-152: tmp = 1.0 elif p <= 2.15e-113: tmp = t_0 elif p <= 7.8e-89: tmp = 1.0 elif p <= 1.2e-41: 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 <= 2.5e-188) tmp = t_0; elseif (p <= 6.8e-152) tmp = 1.0; elseif (p <= 2.15e-113) tmp = t_0; elseif (p <= 7.8e-89) tmp = 1.0; elseif (p <= 1.2e-41) 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 <= 2.5e-188) tmp = t_0; elseif (p <= 6.8e-152) tmp = 1.0; elseif (p <= 2.15e-113) tmp = t_0; elseif (p <= 7.8e-89) tmp = 1.0; elseif (p <= 1.2e-41) 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, 2.5e-188], t$95$0, If[LessEqual[p, 6.8e-152], 1.0, If[LessEqual[p, 2.15e-113], t$95$0, If[LessEqual[p, 7.8e-89], 1.0, If[LessEqual[p, 1.2e-41], 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 2.5 \cdot 10^{-188}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p \leq 6.8 \cdot 10^{-152}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 2.15 \cdot 10^{-113}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p \leq 7.8 \cdot 10^{-89}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 1.2 \cdot 10^{-41}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 2.5e-188 or 6.79999999999999968e-152 < p < 2.15e-113 or 7.79999999999999957e-89 < p < 1.20000000000000011e-41Initial program 69.6%
Taylor expanded in x around -inf 22.2%
unpow222.2%
unpow222.2%
times-frac25.4%
Simplified25.4%
Taylor expanded in p around -inf 22.7%
associate-*r/22.7%
neg-mul-122.7%
Simplified22.7%
if 2.5e-188 < p < 6.79999999999999968e-152 or 2.15e-113 < p < 7.79999999999999957e-89Initial program 63.6%
Taylor expanded in x around inf 63.4%
if 1.20000000000000011e-41 < p Initial program 92.4%
Taylor expanded in x around 0 85.2%
Final simplification43.3%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= p 7e-42) (/ (- p) x) (sqrt 0.5)))
p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 7e-42) {
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 <= 7d-42) 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 <= 7e-42) {
tmp = -p / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 7e-42: tmp = -p / x else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 7e-42) 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 <= 7e-42) 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, 7e-42], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 7 \cdot 10^{-42}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 7.0000000000000004e-42Initial program 69.1%
Taylor expanded in x around -inf 21.0%
unpow221.0%
unpow221.0%
times-frac25.0%
Simplified25.0%
Taylor expanded in p around -inf 23.9%
associate-*r/23.9%
neg-mul-123.9%
Simplified23.9%
if 7.0000000000000004e-42 < p Initial program 92.4%
Taylor expanded in x around 0 85.2%
Final simplification42.1%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= x -2e-310) (/ (- p) x) (/ 1.0 (/ x p))))
p = abs(p);
double code(double p, double x) {
double tmp;
if (x <= -2e-310) {
tmp = -p / x;
} else {
tmp = 1.0 / (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 <= (-2d-310)) then
tmp = -p / x
else
tmp = 1.0d0 / (x / p)
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double tmp;
if (x <= -2e-310) {
tmp = -p / x;
} else {
tmp = 1.0 / (x / p);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if x <= -2e-310: tmp = -p / x else: tmp = 1.0 / (x / p) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (x <= -2e-310) tmp = Float64(Float64(-p) / x); else tmp = Float64(1.0 / Float64(x / p)); end return tmp end
p = abs(p) function tmp_2 = code(p, x) tmp = 0.0; if (x <= -2e-310) tmp = -p / x; else tmp = 1.0 / (x / p); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function code[p_, x_] := If[LessEqual[x, -2e-310], N[((-p) / x), $MachinePrecision], N[(1.0 / N[(x / p), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{p}}\\
\end{array}
\end{array}
if x < -1.999999999999994e-310Initial program 52.8%
Taylor expanded in x around -inf 31.7%
unpow231.7%
unpow231.7%
times-frac37.2%
Simplified37.2%
Taylor expanded in p around -inf 36.0%
associate-*r/36.0%
neg-mul-136.0%
Simplified36.0%
if -1.999999999999994e-310 < x Initial program 100.0%
Taylor expanded in x around -inf 4.6%
unpow24.6%
unpow24.6%
times-frac4.9%
Simplified4.9%
Applied egg-rr3.3%
Final simplification19.9%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= x -2e-310) (/ (- p) x) (/ p x)))
p = abs(p);
double code(double p, double x) {
double tmp;
if (x <= -2e-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 <= (-2d-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 <= -2e-310) {
tmp = -p / x;
} else {
tmp = p / x;
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if x <= -2e-310: tmp = -p / x else: tmp = p / x return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (x <= -2e-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 <= -2e-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, -2e-310], N[((-p) / x), $MachinePrecision], N[(p / x), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{p}{x}\\
\end{array}
\end{array}
if x < -1.999999999999994e-310Initial program 52.8%
Taylor expanded in x around -inf 31.7%
unpow231.7%
unpow231.7%
times-frac37.2%
Simplified37.2%
Taylor expanded in p around -inf 36.0%
associate-*r/36.0%
neg-mul-136.0%
Simplified36.0%
if -1.999999999999994e-310 < x Initial program 100.0%
Taylor expanded in x around -inf 4.6%
unpow24.6%
unpow24.6%
times-frac4.9%
Simplified4.9%
Taylor expanded in p around 0 3.3%
Final simplification19.9%
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.0%
Taylor expanded in x around -inf 18.4%
unpow218.4%
unpow218.4%
times-frac21.3%
Simplified21.3%
Taylor expanded in p around 0 16.4%
Final simplification16.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 2023224
(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))))))))