
(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 3 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 (+ 1.0 x) -0.5)))))
double code(double x) {
return (1.0 / (1.0 + x)) / (sqrt(x) + (x * pow((1.0 + x), -0.5)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (1.0d0 + x)) / (sqrt(x) + (x * ((1.0d0 + x) ** (-0.5d0))))
end function
public static double code(double x) {
return (1.0 / (1.0 + x)) / (Math.sqrt(x) + (x * Math.pow((1.0 + x), -0.5)));
}
def code(x): return (1.0 / (1.0 + x)) / (math.sqrt(x) + (x * math.pow((1.0 + x), -0.5)))
function code(x) return Float64(Float64(1.0 / Float64(1.0 + x)) / Float64(sqrt(x) + Float64(x * (Float64(1.0 + x) ^ -0.5)))) end
function tmp = code(x) tmp = (1.0 / (1.0 + x)) / (sqrt(x) + (x * ((1.0 + x) ^ -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[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{1 + x}}{\sqrt{x} + x \cdot {\left(1 + x\right)}^{-0.5}}
\end{array}
Initial program 39.9%
flip--39.9%
div-inv39.9%
frac-times23.8%
metadata-eval23.8%
add-sqr-sqrt24.5%
frac-times24.2%
metadata-eval24.2%
add-sqr-sqrt40.0%
+-commutative40.0%
inv-pow40.0%
sqrt-pow240.0%
metadata-eval40.0%
pow1/240.0%
pow-flip40.0%
+-commutative40.0%
metadata-eval40.0%
Applied egg-rr40.0%
associate-*r/40.0%
*-rgt-identity40.0%
Simplified40.0%
frac-sub41.9%
*-un-lft-identity41.9%
Applied egg-rr41.9%
*-un-lft-identity41.9%
associate-/r*41.9%
*-rgt-identity41.9%
associate--l+85.7%
+-inverses85.7%
metadata-eval85.7%
+-commutative85.7%
+-commutative85.7%
Applied egg-rr85.7%
*-lft-identity85.7%
associate-/l/99.6%
+-commutative99.6%
+-commutative99.6%
Simplified99.6%
*-un-lft-identity99.6%
associate-/l/98.6%
*-commutative98.6%
associate-*l*98.7%
+-commutative98.7%
*-commutative98.7%
distribute-lft-in98.7%
Applied egg-rr98.7%
*-lft-identity98.7%
associate-/r*99.6%
+-commutative99.6%
+-commutative99.6%
Simplified99.6%
(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 39.9%
Taylor expanded in x around inf 81.9%
distribute-lft-out--81.9%
Simplified81.9%
*-commutative81.9%
unpow281.9%
times-frac97.7%
inv-pow97.7%
sqrt-pow197.7%
metadata-eval97.7%
Applied egg-rr97.7%
Taylor expanded in x around inf 97.5%
neg-mul-197.5%
Simplified97.5%
distribute-frac-neg97.5%
pow1/297.5%
pow197.5%
pow-div97.6%
metadata-eval97.6%
distribute-lft-neg-out97.6%
metadata-eval97.6%
pow-div97.5%
pow1/297.5%
add-sqr-sqrt97.5%
sqr-neg97.5%
sqrt-unprod0.0%
add-sqr-sqrt35.9%
pow135.9%
*-commutative35.9%
div-inv35.9%
associate-*l*35.9%
inv-pow35.9%
metadata-eval35.9%
pow-prod-up35.9%
Applied egg-rr98.0%
distribute-lft-neg-in98.0%
metadata-eval98.0%
Simplified98.0%
(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 39.9%
add-cube-cbrt10.2%
associate-*l*10.2%
frac-2neg10.2%
metadata-eval10.2%
div-inv10.2%
distribute-neg-frac210.2%
prod-diff6.5%
Applied egg-rr6.8%
Simplified6.8%
Taylor expanded in x around inf 37.0%
distribute-rgt1-in37.0%
metadata-eval37.0%
mul0-lft37.0%
Simplified37.0%
(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 2024101
(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)))))