
(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}
(FPCore (p x) :precision binary64 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0) (sqrt (* 0.5 (* 2.0 (* (/ p x) (/ p x))))) (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)))) <= -1.0) {
tmp = sqrt((0.5 * (2.0 * ((p / x) * (p / x)))));
} 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)))) <= -1.0) {
tmp = Math.sqrt((0.5 * (2.0 * ((p / x) * (p / x)))));
} 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)))) <= -1.0: tmp = math.sqrt((0.5 * (2.0 * ((p / x) * (p / x))))) 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)))) <= -1.0) tmp = sqrt(Float64(0.5 * Float64(2.0 * Float64(Float64(p / x) * Float64(p / x))))); 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)))) <= -1.0) tmp = sqrt((0.5 * (2.0 * ((p / x) * (p / x))))); 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], -1.0], N[Sqrt[N[(0.5 * N[(2.0 * N[(N[(p / x), $MachinePrecision] * N[(p / x), $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 -1:\\
\;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\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)))) < -1Initial program 15.3%
Taylor expanded in x around -inf 45.8%
unpow245.8%
unpow245.8%
times-frac60.0%
Simplified60.0%
if -1 < (/.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-unprod49.2%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Final simplification89.8%
(FPCore (p x)
:precision binary64
(if (<= p -1.06e-122)
(sqrt 0.5)
(if (<= p -5e-311)
(/ p x)
(if (<= p 1e-204) (/ (- p) x) (if (<= p 1.02e-44) 1.0 (sqrt 0.5))))))
double code(double p, double x) {
double tmp;
if (p <= -1.06e-122) {
tmp = sqrt(0.5);
} else if (p <= -5e-311) {
tmp = p / x;
} else if (p <= 1e-204) {
tmp = -p / x;
} else if (p <= 1.02e-44) {
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.06d-122)) then
tmp = sqrt(0.5d0)
else if (p <= (-5d-311)) then
tmp = p / x
else if (p <= 1d-204) then
tmp = -p / x
else if (p <= 1.02d-44) 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.06e-122) {
tmp = Math.sqrt(0.5);
} else if (p <= -5e-311) {
tmp = p / x;
} else if (p <= 1e-204) {
tmp = -p / x;
} else if (p <= 1.02e-44) {
tmp = 1.0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= -1.06e-122: tmp = math.sqrt(0.5) elif p <= -5e-311: tmp = p / x elif p <= 1e-204: tmp = -p / x elif p <= 1.02e-44: tmp = 1.0 else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= -1.06e-122) tmp = sqrt(0.5); elseif (p <= -5e-311) tmp = Float64(p / x); elseif (p <= 1e-204) tmp = Float64(Float64(-p) / x); elseif (p <= 1.02e-44) tmp = 1.0; else tmp = sqrt(0.5); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= -1.06e-122) tmp = sqrt(0.5); elseif (p <= -5e-311) tmp = p / x; elseif (p <= 1e-204) tmp = -p / x; elseif (p <= 1.02e-44) tmp = 1.0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, -1.06e-122], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -5e-311], N[(p / x), $MachinePrecision], If[LessEqual[p, 1e-204], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 1.02e-44], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq -1.06 \cdot 10^{-122}:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{elif}\;p \leq -5 \cdot 10^{-311}:\\
\;\;\;\;\frac{p}{x}\\
\mathbf{elif}\;p \leq 10^{-204}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{elif}\;p \leq 1.02 \cdot 10^{-44}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < -1.05999999999999994e-122 or 1.0199999999999999e-44 < p Initial program 89.5%
Taylor expanded in x around 0 78.9%
if -1.05999999999999994e-122 < p < -5.00000000000023e-311Initial program 54.3%
Taylor expanded in x around -inf 19.4%
unpow219.4%
unpow219.4%
times-frac35.4%
Simplified35.4%
Taylor expanded in p around 0 62.1%
if -5.00000000000023e-311 < p < 1e-204Initial program 49.1%
Taylor expanded in x around -inf 14.2%
unpow214.2%
unpow214.2%
times-frac18.1%
Simplified18.1%
Taylor expanded in p around -inf 65.0%
associate-*r/65.0%
neg-mul-165.0%
Simplified65.0%
if 1e-204 < p < 1.0199999999999999e-44Initial program 72.1%
Taylor expanded in x around inf 62.4%
Final simplification73.2%
(FPCore (p x) :precision binary64 (if (<= p -1.06e-122) (sqrt 0.5) (if (<= p -5e-311) (/ p x) (if (<= p 5.5e-116) (/ (- p) x) (sqrt 0.5)))))
double code(double p, double x) {
double tmp;
if (p <= -1.06e-122) {
tmp = sqrt(0.5);
} else if (p <= -5e-311) {
tmp = p / x;
} else if (p <= 5.5e-116) {
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 <= (-1.06d-122)) then
tmp = sqrt(0.5d0)
else if (p <= (-5d-311)) then
tmp = p / x
else if (p <= 5.5d-116) 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 <= -1.06e-122) {
tmp = Math.sqrt(0.5);
} else if (p <= -5e-311) {
tmp = p / x;
} else if (p <= 5.5e-116) {
tmp = -p / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
def code(p, x): tmp = 0 if p <= -1.06e-122: tmp = math.sqrt(0.5) elif p <= -5e-311: tmp = p / x elif p <= 5.5e-116: tmp = -p / x else: tmp = math.sqrt(0.5) return tmp
function code(p, x) tmp = 0.0 if (p <= -1.06e-122) tmp = sqrt(0.5); elseif (p <= -5e-311) tmp = Float64(p / x); elseif (p <= 5.5e-116) 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 <= -1.06e-122) tmp = sqrt(0.5); elseif (p <= -5e-311) tmp = p / x; elseif (p <= 5.5e-116) tmp = -p / x; else tmp = sqrt(0.5); end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, -1.06e-122], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -5e-311], N[(p / x), $MachinePrecision], If[LessEqual[p, 5.5e-116], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq -1.06 \cdot 10^{-122}:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{elif}\;p \leq -5 \cdot 10^{-311}:\\
\;\;\;\;\frac{p}{x}\\
\mathbf{elif}\;p \leq 5.5 \cdot 10^{-116}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < -1.05999999999999994e-122 or 5.4999999999999998e-116 < p Initial program 88.4%
Taylor expanded in x around 0 74.3%
if -1.05999999999999994e-122 < p < -5.00000000000023e-311Initial program 54.3%
Taylor expanded in x around -inf 19.4%
unpow219.4%
unpow219.4%
times-frac35.4%
Simplified35.4%
Taylor expanded in p around 0 62.1%
if -5.00000000000023e-311 < p < 5.4999999999999998e-116Initial program 53.9%
Taylor expanded in x around -inf 16.4%
unpow216.4%
unpow216.4%
times-frac26.0%
Simplified26.0%
Taylor expanded in p around -inf 56.5%
associate-*r/56.5%
neg-mul-156.5%
Simplified56.5%
Final simplification70.0%
(FPCore (p x) :precision binary64 (if (<= p -5e-311) (/ p x) (/ (- p) x)))
double code(double p, double x) {
double tmp;
if (p <= -5e-311) {
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 (p <= (-5d-311)) 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 (p <= -5e-311) {
tmp = p / x;
} else {
tmp = -p / x;
}
return tmp;
}
def code(p, x): tmp = 0 if p <= -5e-311: tmp = p / x else: tmp = -p / x return tmp
function code(p, x) tmp = 0.0 if (p <= -5e-311) tmp = Float64(p / x); else tmp = Float64(Float64(-p) / x); end return tmp end
function tmp_2 = code(p, x) tmp = 0.0; if (p <= -5e-311) tmp = p / x; else tmp = -p / x; end tmp_2 = tmp; end
code[p_, x_] := If[LessEqual[p, -5e-311], N[(p / x), $MachinePrecision], N[((-p) / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;p \leq -5 \cdot 10^{-311}:\\
\;\;\;\;\frac{p}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{-p}{x}\\
\end{array}
\end{array}
if p < -5.00000000000023e-311Initial program 79.2%
Taylor expanded in x around -inf 14.7%
unpow214.7%
unpow214.7%
times-frac19.3%
Simplified19.3%
Taylor expanded in p around 0 27.9%
if -5.00000000000023e-311 < p Initial program 77.7%
Taylor expanded in x around -inf 15.7%
unpow215.7%
unpow215.7%
times-frac18.7%
Simplified18.7%
Taylor expanded in p around -inf 27.3%
associate-*r/27.3%
neg-mul-127.3%
Simplified27.3%
Final simplification27.6%
(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 78.5%
Taylor expanded in x around -inf 15.2%
unpow215.2%
unpow215.2%
times-frac19.0%
Simplified19.0%
Taylor expanded in p around 0 17.2%
Final simplification17.2%
(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 2023196
(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))))))))