
(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 7 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 (pow (* (hypot (sqrt x) x) (hypot (pow (+ x 1.0) -0.25) (pow x -0.25))) -2.0))
double code(double x) {
return pow((hypot(sqrt(x), x) * hypot(pow((x + 1.0), -0.25), pow(x, -0.25))), -2.0);
}
public static double code(double x) {
return Math.pow((Math.hypot(Math.sqrt(x), x) * Math.hypot(Math.pow((x + 1.0), -0.25), Math.pow(x, -0.25))), -2.0);
}
def code(x): return math.pow((math.hypot(math.sqrt(x), x) * math.hypot(math.pow((x + 1.0), -0.25), math.pow(x, -0.25))), -2.0)
function code(x) return Float64(hypot(sqrt(x), x) * hypot((Float64(x + 1.0) ^ -0.25), (x ^ -0.25))) ^ -2.0 end
function tmp = code(x) tmp = (hypot(sqrt(x), x) * hypot(((x + 1.0) ^ -0.25), (x ^ -0.25))) ^ -2.0; end
code[x_] := N[Power[N[(N[Sqrt[N[Sqrt[x], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision] * N[Sqrt[N[Power[N[(x + 1.0), $MachinePrecision], -0.25], $MachinePrecision] ^ 2 + N[Power[x, -0.25], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], -2.0], $MachinePrecision]
\begin{array}{l}
\\
{\left(\mathsf{hypot}\left(\sqrt{x}, x\right) \cdot \mathsf{hypot}\left({\left(x + 1\right)}^{-0.25}, {x}^{-0.25}\right)\right)}^{-2}
\end{array}
Initial program 38.0%
flip--37.9%
clear-num37.9%
inv-pow37.9%
sqrt-pow237.9%
metadata-eval37.9%
inv-pow37.9%
sqrt-pow237.9%
+-commutative37.9%
metadata-eval37.9%
frac-times21.0%
metadata-eval21.0%
add-sqr-sqrt18.8%
frac-times26.1%
metadata-eval26.1%
add-sqr-sqrt38.1%
+-commutative38.1%
Applied egg-rr38.1%
frac-sub40.3%
div-inv40.3%
*-un-lft-identity40.3%
*-rgt-identity40.3%
Applied egg-rr40.3%
associate-*r/40.3%
*-rgt-identity40.3%
associate--l+81.0%
Simplified81.0%
add-sqr-sqrt80.9%
Applied egg-rr99.2%
unpow-199.2%
unpow-199.2%
pow-sqr99.2%
metadata-eval99.2%
*-commutative99.2%
Simplified99.2%
Final simplification99.2%
(FPCore (x) :precision binary64 (* (- (/ 0.5 x) (/ 0.125 (pow x 2.0))) (pow (+ x 1.0) -0.5)))
double code(double x) {
return ((0.5 / x) - (0.125 / pow(x, 2.0))) * pow((x + 1.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 / x) - (0.125d0 / (x ** 2.0d0))) * ((x + 1.0d0) ** (-0.5d0))
end function
public static double code(double x) {
return ((0.5 / x) - (0.125 / Math.pow(x, 2.0))) * Math.pow((x + 1.0), -0.5);
}
def code(x): return ((0.5 / x) - (0.125 / math.pow(x, 2.0))) * math.pow((x + 1.0), -0.5)
function code(x) return Float64(Float64(Float64(0.5 / x) - Float64(0.125 / (x ^ 2.0))) * (Float64(x + 1.0) ^ -0.5)) end
function tmp = code(x) tmp = ((0.5 / x) - (0.125 / (x ^ 2.0))) * ((x + 1.0) ^ -0.5); end
code[x_] := N[(N[(N[(0.5 / x), $MachinePrecision] - N[(0.125 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{0.5}{x} - \frac{0.125}{{x}^{2}}\right) \cdot {\left(x + 1\right)}^{-0.5}
\end{array}
Initial program 38.0%
frac-sub38.0%
div-inv38.0%
*-un-lft-identity38.0%
+-commutative38.0%
*-rgt-identity38.0%
metadata-eval38.0%
frac-times38.0%
associate-*l/38.0%
*-un-lft-identity38.0%
pow1/238.0%
pow-flip38.0%
+-commutative38.0%
metadata-eval38.0%
Applied egg-rr38.0%
*-commutative38.0%
/-rgt-identity38.0%
times-frac38.0%
*-commutative38.0%
times-frac38.0%
div-sub38.0%
sub-neg38.0%
*-inverses38.0%
metadata-eval38.0%
Simplified38.0%
Taylor expanded in x around inf 98.8%
associate-*r/98.8%
metadata-eval98.8%
associate-*r/98.8%
metadata-eval98.8%
Simplified98.8%
Final simplification98.8%
(FPCore (x) :precision binary64 (/ (+ (/ 0.5 x) (* -0.125 (pow x -2.0))) (sqrt (+ x 1.0))))
double code(double x) {
return ((0.5 / x) + (-0.125 * pow(x, -2.0))) / sqrt((x + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 / x) + ((-0.125d0) * (x ** (-2.0d0)))) / sqrt((x + 1.0d0))
end function
public static double code(double x) {
return ((0.5 / x) + (-0.125 * Math.pow(x, -2.0))) / Math.sqrt((x + 1.0));
}
def code(x): return ((0.5 / x) + (-0.125 * math.pow(x, -2.0))) / math.sqrt((x + 1.0))
function code(x) return Float64(Float64(Float64(0.5 / x) + Float64(-0.125 * (x ^ -2.0))) / sqrt(Float64(x + 1.0))) end
function tmp = code(x) tmp = ((0.5 / x) + (-0.125 * (x ^ -2.0))) / sqrt((x + 1.0)); end
code[x_] := N[(N[(N[(0.5 / x), $MachinePrecision] + N[(-0.125 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{x} + -0.125 \cdot {x}^{-2}}{\sqrt{x + 1}}
\end{array}
Initial program 38.0%
frac-sub38.0%
div-inv38.0%
*-un-lft-identity38.0%
+-commutative38.0%
*-rgt-identity38.0%
metadata-eval38.0%
frac-times38.0%
associate-*l/38.0%
*-un-lft-identity38.0%
pow1/238.0%
pow-flip38.0%
+-commutative38.0%
metadata-eval38.0%
Applied egg-rr38.0%
*-commutative38.0%
/-rgt-identity38.0%
times-frac38.0%
*-commutative38.0%
times-frac38.0%
div-sub38.0%
sub-neg38.0%
*-inverses38.0%
metadata-eval38.0%
Simplified38.0%
Taylor expanded in x around inf 98.8%
associate-*r/98.8%
metadata-eval98.8%
associate-*r/98.8%
metadata-eval98.8%
Simplified98.8%
/-rgt-identity98.8%
metadata-eval98.8%
sqrt-pow298.6%
+-commutative98.6%
inv-pow98.6%
un-div-inv98.7%
div-inv98.7%
cancel-sign-sub-inv98.7%
metadata-eval98.7%
pow-flip98.7%
metadata-eval98.7%
Applied egg-rr98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (* (/ 0.5 x) (pow (+ x 1.0) -0.5)))
double code(double x) {
return (0.5 / x) * pow((x + 1.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / x) * ((x + 1.0d0) ** (-0.5d0))
end function
public static double code(double x) {
return (0.5 / x) * Math.pow((x + 1.0), -0.5);
}
def code(x): return (0.5 / x) * math.pow((x + 1.0), -0.5)
function code(x) return Float64(Float64(0.5 / x) * (Float64(x + 1.0) ^ -0.5)) end
function tmp = code(x) tmp = (0.5 / x) * ((x + 1.0) ^ -0.5); end
code[x_] := N[(N[(0.5 / x), $MachinePrecision] * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{x} \cdot {\left(x + 1\right)}^{-0.5}
\end{array}
Initial program 38.0%
frac-sub38.0%
div-inv38.0%
*-un-lft-identity38.0%
+-commutative38.0%
*-rgt-identity38.0%
metadata-eval38.0%
frac-times38.0%
associate-*l/38.0%
*-un-lft-identity38.0%
pow1/238.0%
pow-flip38.0%
+-commutative38.0%
metadata-eval38.0%
Applied egg-rr38.0%
*-commutative38.0%
/-rgt-identity38.0%
times-frac38.0%
*-commutative38.0%
times-frac38.0%
div-sub38.0%
sub-neg38.0%
*-inverses38.0%
metadata-eval38.0%
Simplified38.0%
Taylor expanded in x around inf 97.2%
Final simplification97.2%
(FPCore (x) :precision binary64 (/ (/ 0.5 x) (sqrt (+ x 1.0))))
double code(double x) {
return (0.5 / x) / sqrt((x + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / x) / sqrt((x + 1.0d0))
end function
public static double code(double x) {
return (0.5 / x) / Math.sqrt((x + 1.0));
}
def code(x): return (0.5 / x) / math.sqrt((x + 1.0))
function code(x) return Float64(Float64(0.5 / x) / sqrt(Float64(x + 1.0))) end
function tmp = code(x) tmp = (0.5 / x) / sqrt((x + 1.0)); end
code[x_] := N[(N[(0.5 / x), $MachinePrecision] / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{x}}{\sqrt{x + 1}}
\end{array}
Initial program 38.0%
frac-sub38.0%
div-inv38.0%
*-un-lft-identity38.0%
+-commutative38.0%
*-rgt-identity38.0%
metadata-eval38.0%
frac-times38.0%
associate-*l/38.0%
*-un-lft-identity38.0%
pow1/238.0%
pow-flip38.0%
+-commutative38.0%
metadata-eval38.0%
Applied egg-rr38.0%
*-commutative38.0%
/-rgt-identity38.0%
times-frac38.0%
*-commutative38.0%
times-frac38.0%
div-sub38.0%
sub-neg38.0%
*-inverses38.0%
metadata-eval38.0%
Simplified38.0%
Taylor expanded in x around inf 37.0%
associate-*r/37.0%
metadata-eval37.0%
Simplified37.0%
/-rgt-identity37.0%
clear-num37.0%
/-rgt-identity37.0%
metadata-eval37.0%
sqrt-pow237.0%
+-commutative37.0%
inv-pow37.0%
un-div-inv37.0%
associate-+l+37.0%
Applied egg-rr37.0%
remove-double-div37.0%
+-commutative37.0%
associate-+r+97.2%
metadata-eval97.2%
Simplified97.2%
Final simplification97.2%
(FPCore (x) :precision binary64 (if (<= x 6.4e+153) (/ 0.5 x) 0.0))
double code(double x) {
double tmp;
if (x <= 6.4e+153) {
tmp = 0.5 / x;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.4d+153) then
tmp = 0.5d0 / x
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.4e+153) {
tmp = 0.5 / x;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.4e+153: tmp = 0.5 / x else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 6.4e+153) tmp = Float64(0.5 / x); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.4e+153) tmp = 0.5 / x; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.4e+153], N[(0.5 / x), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.4 \cdot 10^{+153}:\\
\;\;\;\;\frac{0.5}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 6.4000000000000003e153Initial program 11.7%
frac-sub11.8%
div-inv11.8%
*-un-lft-identity11.8%
+-commutative11.8%
*-rgt-identity11.8%
metadata-eval11.8%
frac-times11.8%
associate-*l/11.8%
*-un-lft-identity11.8%
pow1/211.8%
pow-flip11.8%
+-commutative11.8%
metadata-eval11.8%
Applied egg-rr11.8%
*-commutative11.8%
/-rgt-identity11.8%
times-frac11.8%
*-commutative11.8%
times-frac11.8%
div-sub11.7%
sub-neg11.7%
*-inverses11.7%
metadata-eval11.7%
Simplified11.7%
Taylor expanded in x around inf 9.7%
associate-*r/9.7%
metadata-eval9.7%
Simplified9.7%
Taylor expanded in x around 0 5.1%
Taylor expanded in x around 0 8.7%
if 6.4000000000000003e153 < x Initial program 63.4%
Taylor expanded in x around inf 63.4%
Final simplification36.5%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 38.0%
Taylor expanded in x around inf 34.3%
Final simplification34.3%
(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 2024042
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:herbie-target
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))