
(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
(let* ((t_0 (sqrt (/ 1.0 x))))
(+
(* 0.5 (/ t_0 x))
(* (* 0.5 (/ t_0 (pow x 2.0))) (+ -0.75 (/ 0.78125 x))))))
double code(double x) {
double t_0 = sqrt((1.0 / x));
return (0.5 * (t_0 / x)) + ((0.5 * (t_0 / pow(x, 2.0))) * (-0.75 + (0.78125 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = sqrt((1.0d0 / x))
code = (0.5d0 * (t_0 / x)) + ((0.5d0 * (t_0 / (x ** 2.0d0))) * ((-0.75d0) + (0.78125d0 / x)))
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 / x));
return (0.5 * (t_0 / x)) + ((0.5 * (t_0 / Math.pow(x, 2.0))) * (-0.75 + (0.78125 / x)));
}
def code(x): t_0 = math.sqrt((1.0 / x)) return (0.5 * (t_0 / x)) + ((0.5 * (t_0 / math.pow(x, 2.0))) * (-0.75 + (0.78125 / x)))
function code(x) t_0 = sqrt(Float64(1.0 / x)) return Float64(Float64(0.5 * Float64(t_0 / x)) + Float64(Float64(0.5 * Float64(t_0 / (x ^ 2.0))) * Float64(-0.75 + Float64(0.78125 / x)))) end
function tmp = code(x) t_0 = sqrt((1.0 / x)); tmp = (0.5 * (t_0 / x)) + ((0.5 * (t_0 / (x ^ 2.0))) * (-0.75 + (0.78125 / x))); end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(0.5 * N[(t$95$0 / x), $MachinePrecision]), $MachinePrecision] + N[(N[(0.5 * N[(t$95$0 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-0.75 + N[(0.78125 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{x}}\\
0.5 \cdot \frac{t\_0}{x} + \left(0.5 \cdot \frac{t\_0}{{x}^{2}}\right) \cdot \left(-0.75 + \frac{0.78125}{x}\right)
\end{array}
\end{array}
Initial program 41.0%
add-exp-log41.0%
inv-pow41.0%
sqrt-pow230.1%
metadata-eval30.1%
inv-pow30.1%
sqrt-pow241.0%
+-commutative41.0%
metadata-eval41.0%
Applied egg-rr41.0%
Taylor expanded in x around inf 93.8%
Taylor expanded in x around inf 93.8%
associate-*r/93.8%
metadata-eval93.8%
Simplified93.8%
Taylor expanded in x around inf 93.9%
Simplified99.3%
(FPCore (x) :precision binary64 (/ (+ (* -0.375 (sqrt (/ 1.0 (pow x 3.0)))) (* 0.5 (sqrt (/ 1.0 x)))) x))
double code(double x) {
return ((-0.375 * sqrt((1.0 / pow(x, 3.0)))) + (0.5 * sqrt((1.0 / x)))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((-0.375d0) * sqrt((1.0d0 / (x ** 3.0d0)))) + (0.5d0 * sqrt((1.0d0 / x)))) / x
end function
public static double code(double x) {
return ((-0.375 * Math.sqrt((1.0 / Math.pow(x, 3.0)))) + (0.5 * Math.sqrt((1.0 / x)))) / x;
}
def code(x): return ((-0.375 * math.sqrt((1.0 / math.pow(x, 3.0)))) + (0.5 * math.sqrt((1.0 / x)))) / x
function code(x) return Float64(Float64(Float64(-0.375 * sqrt(Float64(1.0 / (x ^ 3.0)))) + Float64(0.5 * sqrt(Float64(1.0 / x)))) / x) end
function tmp = code(x) tmp = ((-0.375 * sqrt((1.0 / (x ^ 3.0)))) + (0.5 * sqrt((1.0 / x)))) / x; end
code[x_] := N[(N[(N[(-0.375 * N[Sqrt[N[(1.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-0.375 \cdot \sqrt{\frac{1}{{x}^{3}}} + 0.5 \cdot \sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 41.0%
add-exp-log41.0%
inv-pow41.0%
sqrt-pow230.1%
metadata-eval30.1%
inv-pow30.1%
sqrt-pow241.0%
+-commutative41.0%
metadata-eval41.0%
Applied egg-rr41.0%
Taylor expanded in x around inf 93.8%
Taylor expanded in x around inf 93.9%
Simplified99.2%
Taylor expanded in x around inf 99.2%
(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 41.0%
add-exp-log6.9%
log-rec6.9%
pow1/26.9%
log-pow6.9%
+-commutative6.9%
log1p-define6.9%
Applied egg-rr6.9%
Taylor expanded in x around inf 6.5%
distribute-lft-neg-in6.5%
metadata-eval6.5%
*-commutative6.5%
exp-to-pow40.7%
unpow1/240.7%
+-commutative40.7%
associate--l+94.3%
Simplified69.7%
+-rgt-identity69.7%
*-commutative69.7%
sqrt-pow198.3%
metadata-eval98.3%
Applied egg-rr98.3%
Final simplification98.3%
(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 41.0%
add-exp-log6.9%
log-rec6.9%
pow1/26.9%
log-pow6.9%
+-commutative6.9%
log1p-define6.9%
Applied egg-rr6.9%
Taylor expanded in x around inf 4.6%
distribute-lft-neg-in4.6%
metadata-eval4.6%
*-commutative4.6%
exp-to-pow38.4%
unpow1/238.4%
+-inverses38.4%
Simplified38.4%
(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}
(FPCore (x) :precision binary64 (- (pow x -0.5) (pow (+ x 1.0) -0.5)))
double code(double x) {
return pow(x, -0.5) - pow((x + 1.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.5d0)) - ((x + 1.0d0) ** (-0.5d0))
end function
public static double code(double x) {
return Math.pow(x, -0.5) - Math.pow((x + 1.0), -0.5);
}
def code(x): return math.pow(x, -0.5) - math.pow((x + 1.0), -0.5)
function code(x) return Float64((x ^ -0.5) - (Float64(x + 1.0) ^ -0.5)) end
function tmp = code(x) tmp = (x ^ -0.5) - ((x + 1.0) ^ -0.5); end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}
\end{array}
herbie shell --seed 2024170
(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))))))
:alt
(! :herbie-platform default (- (pow x -1/2) (pow (+ x 1) -1/2)))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))