
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}
(FPCore (x) :precision binary64 (/ (/ -1.0 (- -1.0 x)) (+ (sqrt x) (* x (pow (+ x 1.0) -0.5)))))
double code(double x) {
return (-1.0 / (-1.0 - x)) / (sqrt(x) + (x * pow((x + 1.0), -0.5)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-1.0d0) / ((-1.0d0) - x)) / (sqrt(x) + (x * ((x + 1.0d0) ** (-0.5d0))))
end function
public static double code(double x) {
return (-1.0 / (-1.0 - x)) / (Math.sqrt(x) + (x * Math.pow((x + 1.0), -0.5)));
}
def code(x): return (-1.0 / (-1.0 - x)) / (math.sqrt(x) + (x * math.pow((x + 1.0), -0.5)))
function code(x) return Float64(Float64(-1.0 / Float64(-1.0 - x)) / Float64(sqrt(x) + Float64(x * (Float64(x + 1.0) ^ -0.5)))) end
function tmp = code(x) tmp = (-1.0 / (-1.0 - x)) / (sqrt(x) + (x * ((x + 1.0) ^ -0.5))); end
code[x_] := N[(N[(-1.0 / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[(x * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{-1}{-1 - x}}{\sqrt{x} + x \cdot {\left(x + 1\right)}^{-0.5}}
\end{array}
Initial program 38.5%
flip--38.5%
div-inv38.5%
frac-times22.5%
metadata-eval22.5%
add-sqr-sqrt21.7%
frac-times26.9%
metadata-eval26.9%
add-sqr-sqrt38.7%
+-commutative38.7%
inv-pow38.7%
sqrt-pow238.7%
metadata-eval38.7%
inv-pow38.7%
sqrt-pow238.7%
+-commutative38.7%
metadata-eval38.7%
Applied egg-rr38.7%
associate-*r/38.7%
*-rgt-identity38.7%
Simplified38.7%
frac-sub40.7%
div-inv40.7%
*-un-lft-identity40.7%
*-rgt-identity40.7%
metadata-eval40.7%
frac-times40.7%
un-div-inv40.7%
Applied egg-rr40.7%
associate-*r/40.7%
associate--l+82.6%
+-inverses82.6%
metadata-eval82.6%
*-lft-identity82.6%
associate-/l/80.8%
metadata-eval80.8%
+-inverses80.8%
associate--l+40.7%
*-rgt-identity40.7%
times-frac40.7%
associate--l+82.4%
+-inverses82.4%
metadata-eval82.4%
remove-double-neg82.4%
distribute-lft-neg-in82.4%
*-commutative82.4%
associate-*l/82.6%
*-lft-identity82.6%
distribute-neg-frac82.6%
Simplified82.6%
*-un-lft-identity82.6%
associate-/l/80.8%
associate-/l/80.8%
+-commutative80.8%
Applied egg-rr80.8%
*-lft-identity80.8%
associate-/l/80.8%
*-commutative80.8%
associate-/r*82.6%
associate-/r*99.6%
distribute-rgt-in99.6%
pow-plus99.6%
metadata-eval99.6%
unpow1/299.6%
*-commutative99.6%
+-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (<= x 120000000.0) (- (pow x -0.5) (pow (+ x 1.0) -0.5)) (* 0.5 (pow x -1.5))))
double code(double x) {
double tmp;
if (x <= 120000000.0) {
tmp = pow(x, -0.5) - pow((x + 1.0), -0.5);
} else {
tmp = 0.5 * pow(x, -1.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 120000000.0d0) then
tmp = (x ** (-0.5d0)) - ((x + 1.0d0) ** (-0.5d0))
else
tmp = 0.5d0 * (x ** (-1.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 120000000.0) {
tmp = Math.pow(x, -0.5) - Math.pow((x + 1.0), -0.5);
} else {
tmp = 0.5 * Math.pow(x, -1.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 120000000.0: tmp = math.pow(x, -0.5) - math.pow((x + 1.0), -0.5) else: tmp = 0.5 * math.pow(x, -1.5) return tmp
function code(x) tmp = 0.0 if (x <= 120000000.0) tmp = Float64((x ^ -0.5) - (Float64(x + 1.0) ^ -0.5)); else tmp = Float64(0.5 * (x ^ -1.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 120000000.0) tmp = (x ^ -0.5) - ((x + 1.0) ^ -0.5); else tmp = 0.5 * (x ^ -1.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 120000000.0], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 120000000:\\
\;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\end{array}
\end{array}
if x < 1.2e8Initial program 79.6%
sub-neg79.6%
inv-pow79.6%
sqrt-pow279.6%
metadata-eval79.6%
distribute-neg-frac79.6%
metadata-eval79.6%
+-commutative79.6%
Applied egg-rr79.6%
*-rgt-identity79.6%
cancel-sign-sub79.6%
distribute-lft-neg-in79.6%
*-rgt-identity79.6%
distribute-neg-frac79.6%
metadata-eval79.6%
unpow1/279.6%
exp-to-pow77.4%
log1p-undefine77.4%
*-commutative77.4%
exp-neg77.8%
*-commutative77.8%
distribute-rgt-neg-in77.8%
log1p-undefine77.8%
metadata-eval77.8%
exp-to-pow79.9%
Simplified79.9%
if 1.2e8 < x Initial program 36.5%
Taylor expanded in x around inf 79.4%
div-sub79.4%
*-commutative79.4%
associate-/l*79.4%
*-commutative79.4%
associate-/l*79.4%
distribute-rgt-out--79.4%
Simplified79.4%
Taylor expanded in x around inf 99.2%
Taylor expanded in x around inf 66.0%
exp-to-pow63.5%
*-commutative63.5%
exp-neg64.1%
distribute-lft-neg-in64.1%
metadata-eval64.1%
*-commutative64.1%
exp-to-pow66.7%
Simplified66.7%
sqrt-pow199.4%
metadata-eval99.4%
*-un-lft-identity99.4%
*-commutative99.4%
Applied egg-rr99.4%
*-rgt-identity99.4%
Simplified99.4%
Final simplification98.5%
(FPCore (x) :precision binary64 (* 0.5 (pow x -1.5)))
double code(double x) {
return 0.5 * pow(x, -1.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * (x ** (-1.5d0))
end function
public static double code(double x) {
return 0.5 * Math.pow(x, -1.5);
}
def code(x): return 0.5 * math.pow(x, -1.5)
function code(x) return Float64(0.5 * (x ^ -1.5)) end
function tmp = code(x) tmp = 0.5 * (x ^ -1.5); end
code[x_] := N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-1.5}
\end{array}
Initial program 38.5%
Taylor expanded in x around inf 77.7%
div-sub77.7%
*-commutative77.7%
associate-/l*77.7%
*-commutative77.7%
associate-/l*77.6%
distribute-rgt-out--77.6%
Simplified77.6%
Taylor expanded in x around inf 96.6%
Taylor expanded in x around inf 64.8%
exp-to-pow62.4%
*-commutative62.4%
exp-neg63.0%
distribute-lft-neg-in63.0%
metadata-eval63.0%
*-commutative63.0%
exp-to-pow65.4%
Simplified65.4%
sqrt-pow196.6%
metadata-eval96.6%
*-un-lft-identity96.6%
*-commutative96.6%
Applied egg-rr96.6%
*-rgt-identity96.6%
Simplified96.6%
Final simplification96.6%
(FPCore (x) :precision binary64 (pow x -0.5))
double code(double x) {
return pow(x, -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-0.5d0)
end function
public static double code(double x) {
return Math.pow(x, -0.5);
}
def code(x): return math.pow(x, -0.5)
function code(x) return x ^ -0.5 end
function tmp = code(x) tmp = x ^ -0.5; end
code[x_] := N[Power[x, -0.5], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5}
\end{array}
Initial program 38.5%
Taylor expanded in x around 0 5.8%
inv-pow5.8%
sqrt-pow15.8%
metadata-eval5.8%
*-un-lft-identity5.8%
Applied egg-rr5.8%
*-lft-identity5.8%
Simplified5.8%
Final simplification5.8%
(FPCore (x) :precision binary64 (/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0))))))
double code(double x) {
return 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (((x + 1.0d0) * sqrt(x)) + (x * sqrt((x + 1.0d0))))
end function
public static double code(double x) {
return 1.0 / (((x + 1.0) * Math.sqrt(x)) + (x * Math.sqrt((x + 1.0))));
}
def code(x): return 1.0 / (((x + 1.0) * math.sqrt(x)) + (x * math.sqrt((x + 1.0))))
function code(x) return Float64(1.0 / Float64(Float64(Float64(x + 1.0) * sqrt(x)) + Float64(x * sqrt(Float64(x + 1.0))))) end
function tmp = code(x) tmp = 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0)))); end
code[x_] := N[(1.0 / N[(N[(N[(x + 1.0), $MachinePrecision] * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(x * N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}}
\end{array}
herbie shell --seed 2024079
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))