
(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 (let* ((t_0 (fabs (pow x -0.5)))) (/ (- (* t_0 0.5) (* t_0 (/ 0.375 x))) x)))
double code(double x) {
double t_0 = fabs(pow(x, -0.5));
return ((t_0 * 0.5) - (t_0 * (0.375 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = abs((x ** (-0.5d0)))
code = ((t_0 * 0.5d0) - (t_0 * (0.375d0 / x))) / x
end function
public static double code(double x) {
double t_0 = Math.abs(Math.pow(x, -0.5));
return ((t_0 * 0.5) - (t_0 * (0.375 / x))) / x;
}
def code(x): t_0 = math.fabs(math.pow(x, -0.5)) return ((t_0 * 0.5) - (t_0 * (0.375 / x))) / x
function code(x) t_0 = abs((x ^ -0.5)) return Float64(Float64(Float64(t_0 * 0.5) - Float64(t_0 * Float64(0.375 / x))) / x) end
function tmp = code(x) t_0 = abs((x ^ -0.5)); tmp = ((t_0 * 0.5) - (t_0 * (0.375 / x))) / x; end
code[x_] := Block[{t$95$0 = N[Abs[N[Power[x, -0.5], $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(t$95$0 * 0.5), $MachinePrecision] - N[(t$95$0 * N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left|{x}^{-0.5}\right|\\
\frac{t\_0 \cdot 0.5 - t\_0 \cdot \frac{0.375}{x}}{x}
\end{array}
\end{array}
Initial program 42.1%
Taylor expanded in x around inf 80.7%
Taylor expanded in x around inf 98.8%
Simplified98.8%
(FPCore (x) :precision binary64 (/ (* (pow x -0.5) (- 0.5 (/ 0.375 x))) x))
double code(double x) {
return (pow(x, -0.5) * (0.5 - (0.375 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x ** (-0.5d0)) * (0.5d0 - (0.375d0 / x))) / x
end function
public static double code(double x) {
return (Math.pow(x, -0.5) * (0.5 - (0.375 / x))) / x;
}
def code(x): return (math.pow(x, -0.5) * (0.5 - (0.375 / x))) / x
function code(x) return Float64(Float64((x ^ -0.5) * Float64(0.5 - Float64(0.375 / x))) / x) end
function tmp = code(x) tmp = ((x ^ -0.5) * (0.5 - (0.375 / x))) / x; end
code[x_] := N[(N[(N[Power[x, -0.5], $MachinePrecision] * N[(0.5 - N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}
\end{array}
Initial program 42.1%
Taylor expanded in x around inf 80.7%
Taylor expanded in x around inf 98.8%
Simplified98.8%
distribute-lft-out--98.8%
Applied egg-rr98.8%
pow198.8%
add-sqr-sqrt98.5%
fabs-sqr98.5%
add-sqr-sqrt98.8%
Applied egg-rr98.8%
Simplified98.8%
(FPCore (x) :precision binary64 (* (pow x -0.5) (/ (- 0.5 (/ 0.375 x)) x)))
double code(double x) {
return pow(x, -0.5) * ((0.5 - (0.375 / x)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.5d0)) * ((0.5d0 - (0.375d0 / x)) / x)
end function
public static double code(double x) {
return Math.pow(x, -0.5) * ((0.5 - (0.375 / x)) / x);
}
def code(x): return math.pow(x, -0.5) * ((0.5 - (0.375 / x)) / x)
function code(x) return Float64((x ^ -0.5) * Float64(Float64(0.5 - Float64(0.375 / x)) / x)) end
function tmp = code(x) tmp = (x ^ -0.5) * ((0.5 - (0.375 / x)) / x); end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] * N[(N[(0.5 - N[(0.375 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}
\end{array}
Initial program 42.1%
Taylor expanded in x around inf 80.7%
Taylor expanded in x around inf 98.8%
Simplified98.8%
distribute-lft-out--98.8%
Applied egg-rr98.8%
associate-/l*98.8%
add-sqr-sqrt98.5%
fabs-sqr98.5%
add-sqr-sqrt98.8%
Applied egg-rr98.8%
(FPCore (x) :precision binary64 (/ (* 0.5 (sqrt (/ 1.0 x))) x))
double code(double x) {
return (0.5 * sqrt((1.0 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * sqrt((1.0d0 / x))) / x
end function
public static double code(double x) {
return (0.5 * Math.sqrt((1.0 / x))) / x;
}
def code(x): return (0.5 * math.sqrt((1.0 / x))) / x
function code(x) return Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / x) end
function tmp = code(x) tmp = (0.5 * sqrt((1.0 / x))) / x; end
code[x_] := N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 42.1%
Taylor expanded in x around inf 79.5%
distribute-lft-out--79.5%
Simplified79.5%
Taylor expanded in x around inf 97.7%
Taylor expanded in x around inf 97.6%
Simplified97.6%
Final simplification97.6%
(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 42.1%
add-cube-cbrt13.4%
associate-*l*13.4%
frac-2neg13.4%
metadata-eval13.4%
div-inv13.4%
distribute-neg-frac213.4%
prod-diff6.9%
Applied egg-rr6.9%
Simplified7.0%
Taylor expanded in x around inf 39.2%
distribute-rgt1-in39.2%
metadata-eval39.2%
mul0-lft39.2%
Simplified39.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 2024130
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(! :herbie-platform default (/ 1 (+ (* (+ x 1) (sqrt x)) (* x (sqrt (+ x 1))))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))