
(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 (let* ((t_0 (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))))) (if (<= t_0 -0.5) (/ (- p) x) (sqrt (* 0.5 (+ t_0 1.0))))))
p = abs(p);
double code(double p, double x) {
double t_0 = x / sqrt(((p * (4.0 * p)) + (x * x)));
double tmp;
if (t_0 <= -0.5) {
tmp = -p / x;
} else {
tmp = sqrt((0.5 * (t_0 + 1.0)));
}
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 = x / sqrt(((p * (4.0d0 * p)) + (x * x)))
if (t_0 <= (-0.5d0)) then
tmp = -p / x
else
tmp = sqrt((0.5d0 * (t_0 + 1.0d0)))
end if
code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
double t_0 = x / Math.sqrt(((p * (4.0 * p)) + (x * x)));
double tmp;
if (t_0 <= -0.5) {
tmp = -p / x;
} else {
tmp = Math.sqrt((0.5 * (t_0 + 1.0)));
}
return tmp;
}
p = abs(p) def code(p, x): t_0 = x / math.sqrt(((p * (4.0 * p)) + (x * x))) tmp = 0 if t_0 <= -0.5: tmp = -p / x else: tmp = math.sqrt((0.5 * (t_0 + 1.0))) return tmp
p = abs(p) function code(p, x) t_0 = Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) tmp = 0.0 if (t_0 <= -0.5) tmp = Float64(Float64(-p) / x); else tmp = sqrt(Float64(0.5 * Float64(t_0 + 1.0))); end return tmp end
p = abs(p) function tmp_2 = code(p, x) t_0 = x / sqrt(((p * (4.0 * p)) + (x * x))); tmp = 0.0; if (t_0 <= -0.5) tmp = -p / x; else tmp = sqrt((0.5 * (t_0 + 1.0))); end tmp_2 = tmp; end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.5], N[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\\
\mathbf{if}\;t_0 \leq -0.5:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(t_0 + 1\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.5Initial program 16.5%
Taylor expanded in x around -inf 47.1%
unpow247.1%
unpow247.1%
Simplified47.1%
Taylor expanded in p around -inf 48.7%
mul-1-neg48.7%
Simplified48.7%
if -0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 100.0%
Final simplification86.0%
NOTE: p should be positive before calling this function
(FPCore (p x)
:precision binary64
(let* ((t_0 (/ (- p) x)))
(if (<= p 1.9e-276)
1.0
(if (<= p 1.1e-225)
t_0
(if (<= p 1.7e-152) 1.0 (if (<= p 8.2e-92) t_0 (sqrt 0.5)))))))p = abs(p);
double code(double p, double x) {
double t_0 = -p / x;
double tmp;
if (p <= 1.9e-276) {
tmp = 1.0;
} else if (p <= 1.1e-225) {
tmp = t_0;
} else if (p <= 1.7e-152) {
tmp = 1.0;
} else if (p <= 8.2e-92) {
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 <= 1.9d-276) then
tmp = 1.0d0
else if (p <= 1.1d-225) then
tmp = t_0
else if (p <= 1.7d-152) then
tmp = 1.0d0
else if (p <= 8.2d-92) 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 <= 1.9e-276) {
tmp = 1.0;
} else if (p <= 1.1e-225) {
tmp = t_0;
} else if (p <= 1.7e-152) {
tmp = 1.0;
} else if (p <= 8.2e-92) {
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 <= 1.9e-276: tmp = 1.0 elif p <= 1.1e-225: tmp = t_0 elif p <= 1.7e-152: tmp = 1.0 elif p <= 8.2e-92: 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 <= 1.9e-276) tmp = 1.0; elseif (p <= 1.1e-225) tmp = t_0; elseif (p <= 1.7e-152) tmp = 1.0; elseif (p <= 8.2e-92) 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 <= 1.9e-276) tmp = 1.0; elseif (p <= 1.1e-225) tmp = t_0; elseif (p <= 1.7e-152) tmp = 1.0; elseif (p <= 8.2e-92) 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, 1.9e-276], 1.0, If[LessEqual[p, 1.1e-225], t$95$0, If[LessEqual[p, 1.7e-152], 1.0, If[LessEqual[p, 8.2e-92], 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 1.9 \cdot 10^{-276}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 1.1 \cdot 10^{-225}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p \leq 1.7 \cdot 10^{-152}:\\
\;\;\;\;1\\
\mathbf{elif}\;p \leq 8.2 \cdot 10^{-92}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 1.9e-276 or 1.1e-225 < p < 1.69999999999999992e-152Initial program 73.1%
Taylor expanded in x around inf 40.1%
if 1.9e-276 < p < 1.1e-225 or 1.69999999999999992e-152 < p < 8.2000000000000005e-92Initial program 50.6%
Taylor expanded in x around -inf 35.7%
unpow235.7%
unpow235.7%
Simplified35.7%
Taylor expanded in p around -inf 67.2%
mul-1-neg67.2%
Simplified67.2%
if 8.2000000000000005e-92 < p Initial program 90.7%
Taylor expanded in x around 0 80.2%
Final simplification55.1%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= p 1.02e-91) (/ (- p) x) (sqrt 0.5)))
p = abs(p);
double code(double p, double x) {
double tmp;
if (p <= 1.02e-91) {
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 <= 1.02d-91) 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 <= 1.02e-91) {
tmp = -p / x;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if p <= 1.02e-91: tmp = -p / x else: tmp = math.sqrt(0.5) return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (p <= 1.02e-91) 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 <= 1.02e-91) 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, 1.02e-91], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 1.02 \cdot 10^{-91}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 1.01999999999999994e-91Initial program 70.6%
Taylor expanded in x around -inf 17.8%
unpow217.8%
unpow217.8%
Simplified17.8%
Taylor expanded in p around -inf 17.4%
mul-1-neg17.4%
Simplified17.4%
if 1.01999999999999994e-91 < p Initial program 90.7%
Taylor expanded in x around 0 80.2%
Final simplification37.8%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 (if (<= x -4e-311) (/ (- p) x) (/ p x)))
p = abs(p);
double code(double p, double x) {
double tmp;
if (x <= -4e-311) {
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 <= (-4d-311)) 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 <= -4e-311) {
tmp = -p / x;
} else {
tmp = p / x;
}
return tmp;
}
p = abs(p) def code(p, x): tmp = 0 if x <= -4e-311: tmp = -p / x else: tmp = p / x return tmp
p = abs(p) function code(p, x) tmp = 0.0 if (x <= -4e-311) 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 <= -4e-311) 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, -4e-311], N[((-p) / x), $MachinePrecision], N[(p / x), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4 \cdot 10^{-311}:\\
\;\;\;\;\frac{-p}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{p}{x}\\
\end{array}
\end{array}
if x < -3.99999999999979e-311Initial program 54.7%
Taylor expanded in x around -inf 27.6%
unpow227.6%
unpow227.6%
Simplified27.6%
Taylor expanded in p around -inf 28.0%
mul-1-neg28.0%
Simplified28.0%
if -3.99999999999979e-311 < x Initial program 100.0%
Taylor expanded in x around -inf 4.6%
unpow24.6%
unpow24.6%
Simplified4.6%
Taylor expanded in p around 0 3.5%
Final simplification15.8%
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 77.1%
Taylor expanded in x around -inf 16.2%
unpow216.2%
unpow216.2%
Simplified16.2%
Taylor expanded in p around 0 20.1%
Final simplification20.1%
NOTE: p should be positive before calling this function (FPCore (p x) :precision binary64 -1.0)
p = abs(p);
double code(double p, double x) {
return -1.0;
}
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 = -1.0d0
end function
p = Math.abs(p);
public static double code(double p, double x) {
return -1.0;
}
p = abs(p) def code(p, x): return -1.0
p = abs(p) function code(p, x) return -1.0 end
p = abs(p) function tmp = code(p, x) tmp = -1.0; end
NOTE: p should be positive before calling this function code[p_, x_] := -1.0
\begin{array}{l}
p = |p|\\
\\
-1
\end{array}
Initial program 77.1%
distribute-rgt-in77.1%
metadata-eval77.1%
flip-+51.8%
sqrt-div51.0%
Applied egg-rr51.1%
unpow251.1%
associate-*l/51.1%
unpow251.1%
Simplified51.1%
Taylor expanded in x around -inf 15.3%
mul-1-neg15.3%
distribute-neg-frac15.3%
Simplified15.3%
Taylor expanded in p around 0 1.8%
Final simplification1.8%
(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 2023297
(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))))))))