
(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 5 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 x) (+ 1.0 (sqrt (+ 1.0 (/ 1.0 x))))) (pow (+ 1.0 x) -0.5)))
double code(double x) {
return ((1.0 / x) / (1.0 + sqrt((1.0 + (1.0 / x))))) * pow((1.0 + x), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / x) / (1.0d0 + sqrt((1.0d0 + (1.0d0 / x))))) * ((1.0d0 + x) ** (-0.5d0))
end function
public static double code(double x) {
return ((1.0 / x) / (1.0 + Math.sqrt((1.0 + (1.0 / x))))) * Math.pow((1.0 + x), -0.5);
}
def code(x): return ((1.0 / x) / (1.0 + math.sqrt((1.0 + (1.0 / x))))) * math.pow((1.0 + x), -0.5)
function code(x) return Float64(Float64(Float64(1.0 / x) / Float64(1.0 + sqrt(Float64(1.0 + Float64(1.0 / x))))) * (Float64(1.0 + x) ^ -0.5)) end
function tmp = code(x) tmp = ((1.0 / x) / (1.0 + sqrt((1.0 + (1.0 / x))))) * ((1.0 + x) ^ -0.5); end
code[x_] := N[(N[(N[(1.0 / x), $MachinePrecision] / N[(1.0 + N[Sqrt[N[(1.0 + N[(1.0 / x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{1 + \sqrt{1 + \frac{1}{x}}} \cdot {\left(1 + x\right)}^{-0.5}
\end{array}
Initial program 35.5%
frac-sub35.5%
div-inv35.5%
*-rgt-identity35.5%
*-un-lft-identity35.5%
+-commutative35.5%
metadata-eval35.5%
frac-times35.5%
associate-*l/35.5%
*-un-lft-identity35.5%
inv-pow35.5%
sqrt-pow235.5%
+-commutative35.5%
metadata-eval35.5%
Applied egg-rr35.5%
associate-*r/35.5%
*-rgt-identity35.5%
times-frac35.5%
div-sub35.5%
sub-neg35.5%
*-inverses35.5%
metadata-eval35.5%
/-rgt-identity35.5%
Simplified35.5%
flip-+35.5%
sqrt-undiv35.5%
sqrt-undiv35.5%
add-sqr-sqrt35.6%
metadata-eval35.6%
sqrt-undiv35.6%
Applied egg-rr35.6%
*-rgt-identity35.6%
associate-*r/31.3%
*-commutative31.3%
+-commutative31.3%
distribute-rgt-in31.3%
rgt-mult-inverse35.6%
*-lft-identity35.6%
rem-exp-log35.6%
log1p-undefine35.6%
expm1-define99.6%
sub-neg99.6%
*-lft-identity99.6%
metadata-eval99.6%
*-commutative99.6%
distribute-lft1-in99.6%
distribute-rgt1-in99.6%
*-rgt-identity99.6%
*-rgt-identity99.6%
associate-*r/99.6%
*-commutative99.6%
+-commutative99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
(FPCore (x) :precision binary64 (/ (* (pow (+ 1.0 x) -0.5) (+ 0.5 (/ -0.125 x))) x))
double code(double x) {
return (pow((1.0 + x), -0.5) * (0.5 + (-0.125 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((1.0d0 + x) ** (-0.5d0)) * (0.5d0 + ((-0.125d0) / x))) / x
end function
public static double code(double x) {
return (Math.pow((1.0 + x), -0.5) * (0.5 + (-0.125 / x))) / x;
}
def code(x): return (math.pow((1.0 + x), -0.5) * (0.5 + (-0.125 / x))) / x
function code(x) return Float64(Float64((Float64(1.0 + x) ^ -0.5) * Float64(0.5 + Float64(-0.125 / x))) / x) end
function tmp = code(x) tmp = (((1.0 + x) ^ -0.5) * (0.5 + (-0.125 / x))) / x; end
code[x_] := N[(N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] * N[(0.5 + N[(-0.125 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{{\left(1 + x\right)}^{-0.5} \cdot \left(0.5 + \frac{-0.125}{x}\right)}{x}
\end{array}
Initial program 35.5%
frac-sub35.5%
div-inv35.5%
*-rgt-identity35.5%
*-un-lft-identity35.5%
+-commutative35.5%
metadata-eval35.5%
frac-times35.5%
associate-*l/35.5%
*-un-lft-identity35.5%
inv-pow35.5%
sqrt-pow235.5%
+-commutative35.5%
metadata-eval35.5%
Applied egg-rr35.5%
associate-*r/35.5%
*-rgt-identity35.5%
times-frac35.5%
div-sub35.5%
sub-neg35.5%
*-inverses35.5%
metadata-eval35.5%
/-rgt-identity35.5%
Simplified35.5%
Taylor expanded in x around inf 99.4%
associate-*r/99.4%
metadata-eval99.4%
Simplified99.4%
*-commutative99.4%
associate-*r/99.5%
sub-neg99.5%
distribute-neg-frac99.5%
metadata-eval99.5%
Applied egg-rr99.5%
(FPCore (x) :precision binary64 (* (pow (+ 1.0 x) -0.5) (/ (- 0.5 (/ 0.125 x)) x)))
double code(double x) {
return pow((1.0 + x), -0.5) * ((0.5 - (0.125 / x)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 + x) ** (-0.5d0)) * ((0.5d0 - (0.125d0 / x)) / x)
end function
public static double code(double x) {
return Math.pow((1.0 + x), -0.5) * ((0.5 - (0.125 / x)) / x);
}
def code(x): return math.pow((1.0 + x), -0.5) * ((0.5 - (0.125 / x)) / x)
function code(x) return Float64((Float64(1.0 + x) ^ -0.5) * Float64(Float64(0.5 - Float64(0.125 / x)) / x)) end
function tmp = code(x) tmp = ((1.0 + x) ^ -0.5) * ((0.5 - (0.125 / x)) / x); end
code[x_] := N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] * N[(N[(0.5 - N[(0.125 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(1 + x\right)}^{-0.5} \cdot \frac{0.5 - \frac{0.125}{x}}{x}
\end{array}
Initial program 35.5%
frac-sub35.5%
div-inv35.5%
*-rgt-identity35.5%
*-un-lft-identity35.5%
+-commutative35.5%
metadata-eval35.5%
frac-times35.5%
associate-*l/35.5%
*-un-lft-identity35.5%
inv-pow35.5%
sqrt-pow235.5%
+-commutative35.5%
metadata-eval35.5%
Applied egg-rr35.5%
associate-*r/35.5%
*-rgt-identity35.5%
times-frac35.5%
div-sub35.5%
sub-neg35.5%
*-inverses35.5%
metadata-eval35.5%
/-rgt-identity35.5%
Simplified35.5%
Taylor expanded in x around inf 99.4%
associate-*r/99.4%
metadata-eval99.4%
Simplified99.4%
Final simplification99.4%
(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 35.5%
Taylor expanded in x around inf 66.8%
pow166.8%
pow-flip67.4%
sqrt-pow199.2%
metadata-eval99.2%
metadata-eval99.2%
Applied egg-rr99.2%
unpow199.2%
Simplified99.2%
(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 35.5%
add-exp-log5.9%
log-rec5.9%
pow1/25.9%
log-pow5.9%
+-commutative5.9%
log1p-define5.9%
Applied egg-rr5.9%
Taylor expanded in x around inf 4.9%
distribute-lft-neg-in4.9%
metadata-eval4.9%
*-commutative4.9%
exp-to-pow34.2%
unpow1/234.2%
+-inverses34.2%
Simplified34.2%
(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 2024110
(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)))))