
(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 (* (pow x -1.5) (+ 0.5 (/ -0.5 x))))
double code(double x) {
return pow(x, -1.5) * (0.5 + (-0.5 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-1.5d0)) * (0.5d0 + ((-0.5d0) / x))
end function
public static double code(double x) {
return Math.pow(x, -1.5) * (0.5 + (-0.5 / x));
}
def code(x): return math.pow(x, -1.5) * (0.5 + (-0.5 / x))
function code(x) return Float64((x ^ -1.5) * Float64(0.5 + Float64(-0.5 / x))) end
function tmp = code(x) tmp = (x ^ -1.5) * (0.5 + (-0.5 / x)); end
code[x_] := N[(N[Power[x, -1.5], $MachinePrecision] * N[(0.5 + N[(-0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5} \cdot \left(0.5 + \frac{-0.5}{x}\right)
\end{array}
Initial program 37.3%
Taylor expanded in x around inf 83.0%
distribute-lft-out--83.0%
Simplified83.0%
expm1-log1p-u80.1%
expm1-undefine80.1%
inv-pow80.1%
sqrt-pow180.1%
metadata-eval80.1%
Applied egg-rr80.1%
expm1-define80.1%
Simplified80.1%
expm1-log1p-u83.0%
*-commutative83.0%
associate-/l*82.9%
Applied egg-rr82.9%
Simplified97.0%
pow197.0%
metadata-eval97.0%
sqrt-pow165.4%
div-inv65.4%
sqrt-pow197.0%
metadata-eval97.0%
pow-flip98.7%
metadata-eval98.7%
Applied egg-rr98.7%
unpow198.7%
*-commutative98.7%
*-commutative98.7%
associate-*l*98.7%
distribute-lft-in98.7%
metadata-eval98.7%
associate-*r/98.7%
metadata-eval98.7%
Simplified98.7%
(FPCore (x) :precision binary64 (* (pow x -0.5) (/ 0.5 x)))
double code(double x) {
return pow(x, -0.5) * (0.5 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.5d0)) * (0.5d0 / x)
end function
public static double code(double x) {
return Math.pow(x, -0.5) * (0.5 / x);
}
def code(x): return math.pow(x, -0.5) * (0.5 / x)
function code(x) return Float64((x ^ -0.5) * Float64(0.5 / x)) end
function tmp = code(x) tmp = (x ^ -0.5) * (0.5 / x); end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] * N[(0.5 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5} \cdot \frac{0.5}{x}
\end{array}
Initial program 37.3%
Taylor expanded in x around inf 83.0%
distribute-lft-out--83.0%
Simplified83.0%
Taylor expanded in x around inf 83.0%
*-commutative83.0%
Simplified83.0%
unpow283.0%
times-frac98.2%
pow1/298.2%
pow198.2%
pow-div98.3%
metadata-eval98.3%
Applied egg-rr98.3%
(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(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}
\\
\frac{--0.5}{{x}^{1.5}}
\end{array}
Initial program 37.3%
Taylor expanded in x around inf 83.0%
distribute-lft-out--83.0%
Simplified83.0%
expm1-log1p-u80.1%
expm1-undefine80.1%
inv-pow80.1%
sqrt-pow180.1%
metadata-eval80.1%
Applied egg-rr80.1%
expm1-define80.1%
Simplified80.1%
expm1-log1p-u83.0%
*-commutative83.0%
associate-/l*82.9%
Applied egg-rr82.9%
Simplified97.0%
Taylor expanded in x around inf 96.9%
Final simplification96.9%
(FPCore (x) :precision binary64 (pow x -0.5))
double code(double x) {
return pow(x, -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-0.5d0)
end function
public static double code(double x) {
return Math.pow(x, -0.5);
}
def code(x): return math.pow(x, -0.5)
function code(x) return x ^ -0.5 end
function tmp = code(x) tmp = x ^ -0.5; end
code[x_] := N[Power[x, -0.5], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5}
\end{array}
Initial program 37.3%
add-sqr-sqrt18.7%
sqrt-unprod37.3%
frac-times32.4%
metadata-eval32.4%
add-sqr-sqrt29.0%
+-commutative29.0%
Applied egg-rr29.0%
Taylor expanded in x around 0 5.6%
unpow-15.6%
metadata-eval5.6%
pow-sqr5.6%
rem-sqrt-square5.6%
metadata-eval5.6%
pow-sqr5.6%
fabs-sqr5.6%
pow-sqr5.6%
metadata-eval5.6%
Simplified5.6%
(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 2024135
(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)))))