
(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 (/ (- (* (sqrt (/ 1.0 x)) 0.5) (* 0.375 (pow x -1.5))) x))
double code(double x) {
return ((sqrt((1.0 / x)) * 0.5) - (0.375 * pow(x, -1.5))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((sqrt((1.0d0 / x)) * 0.5d0) - (0.375d0 * (x ** (-1.5d0)))) / x
end function
public static double code(double x) {
return ((Math.sqrt((1.0 / x)) * 0.5) - (0.375 * Math.pow(x, -1.5))) / x;
}
def code(x): return ((math.sqrt((1.0 / x)) * 0.5) - (0.375 * math.pow(x, -1.5))) / x
function code(x) return Float64(Float64(Float64(sqrt(Float64(1.0 / x)) * 0.5) - Float64(0.375 * (x ^ -1.5))) / x) end
function tmp = code(x) tmp = ((sqrt((1.0 / x)) * 0.5) - (0.375 * (x ^ -1.5))) / x; end
code[x_] := N[(N[(N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision] - N[(0.375 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sqrt{\frac{1}{x}} \cdot 0.5 - 0.375 \cdot {x}^{-1.5}}{x}
\end{array}
Initial program 37.5%
Taylor expanded in x around inf 80.2%
Taylor expanded in x around inf 98.6%
+-commutative98.6%
mul-1-neg98.6%
unsub-neg98.6%
*-commutative98.6%
distribute-rgt-out98.6%
metadata-eval98.6%
Simplified98.6%
*-un-lft-identity98.6%
div-inv98.6%
*-commutative98.6%
associate-*l*98.6%
add-sqr-sqrt98.6%
cube-mult98.6%
inv-pow98.6%
sqrt-pow198.6%
metadata-eval98.6%
pow-pow98.6%
metadata-eval98.6%
Applied egg-rr98.6%
*-lft-identity98.6%
Simplified98.6%
(FPCore (x) :precision binary64 (/ (- (/ 0.5 (sqrt x)) (* 0.375 (pow x -1.5))) x))
double code(double x) {
return ((0.5 / sqrt(x)) - (0.375 * pow(x, -1.5))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 / sqrt(x)) - (0.375d0 * (x ** (-1.5d0)))) / x
end function
public static double code(double x) {
return ((0.5 / Math.sqrt(x)) - (0.375 * Math.pow(x, -1.5))) / x;
}
def code(x): return ((0.5 / math.sqrt(x)) - (0.375 * math.pow(x, -1.5))) / x
function code(x) return Float64(Float64(Float64(0.5 / sqrt(x)) - Float64(0.375 * (x ^ -1.5))) / x) end
function tmp = code(x) tmp = ((0.5 / sqrt(x)) - (0.375 * (x ^ -1.5))) / x; end
code[x_] := N[(N[(N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(0.375 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{\sqrt{x}} - 0.375 \cdot {x}^{-1.5}}{x}
\end{array}
Initial program 37.5%
Taylor expanded in x around inf 80.2%
Taylor expanded in x around inf 98.6%
+-commutative98.6%
mul-1-neg98.6%
unsub-neg98.6%
*-commutative98.6%
distribute-rgt-out98.6%
metadata-eval98.6%
Simplified98.6%
*-un-lft-identity98.6%
div-inv98.6%
*-commutative98.6%
associate-*l*98.6%
add-sqr-sqrt98.6%
cube-mult98.6%
inv-pow98.6%
sqrt-pow198.6%
metadata-eval98.6%
pow-pow98.6%
metadata-eval98.6%
Applied egg-rr98.6%
*-lft-identity98.6%
Simplified98.6%
sqrt-div98.5%
metadata-eval98.5%
associate-*l/98.5%
metadata-eval98.5%
Applied egg-rr98.5%
(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 37.5%
Taylor expanded in x around inf 66.9%
add-cbrt-cube56.3%
pow1/354.8%
pow354.8%
*-commutative54.8%
pow-flip54.8%
sqrt-pow154.8%
metadata-eval54.8%
metadata-eval54.8%
Applied egg-rr54.8%
unpow1/356.4%
rem-cbrt-cube97.4%
*-commutative97.4%
Simplified97.4%
(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 37.5%
sub-neg37.5%
+-commutative37.5%
add-cube-cbrt13.6%
distribute-lft-neg-in13.6%
fma-define8.5%
Applied egg-rr8.8%
Taylor expanded in x around inf 33.9%
distribute-rgt1-in33.9%
metadata-eval33.9%
mul0-lft33.9%
Simplified33.9%
(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 2024117
(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)))))