
(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 (- (* 0.5 (pow x -1.5)) (* 0.375 (/ (pow x -1.5) x))))
double code(double x) {
return (0.5 * pow(x, -1.5)) - (0.375 * (pow(x, -1.5) / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * (x ** (-1.5d0))) - (0.375d0 * ((x ** (-1.5d0)) / x))
end function
public static double code(double x) {
return (0.5 * Math.pow(x, -1.5)) - (0.375 * (Math.pow(x, -1.5) / x));
}
def code(x): return (0.5 * math.pow(x, -1.5)) - (0.375 * (math.pow(x, -1.5) / x))
function code(x) return Float64(Float64(0.5 * (x ^ -1.5)) - Float64(0.375 * Float64((x ^ -1.5) / x))) end
function tmp = code(x) tmp = (0.5 * (x ^ -1.5)) - (0.375 * ((x ^ -1.5) / x)); end
code[x_] := N[(N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision] - N[(0.375 * N[(N[Power[x, -1.5], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-1.5} - 0.375 \cdot \frac{{x}^{-1.5}}{x}
\end{array}
Initial program 43.0%
Taylor expanded in x around inf 84.8%
*-un-lft-identity84.8%
pow1/284.8%
pow-flip84.8%
pow-pow84.8%
metadata-eval84.8%
metadata-eval84.8%
Applied egg-rr84.8%
*-lft-identity84.8%
Simplified84.8%
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%
*-rgt-identity98.6%
times-frac98.6%
metadata-eval98.6%
Simplified98.6%
div-sub98.6%
Applied egg-rr98.9%
(FPCore (x) :precision binary64 (/ (+ (/ 0.5 (sqrt x)) (* (pow x -1.5) -0.375)) x))
double code(double x) {
return ((0.5 / sqrt(x)) + (pow(x, -1.5) * -0.375)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 / sqrt(x)) + ((x ** (-1.5d0)) * (-0.375d0))) / x
end function
public static double code(double x) {
return ((0.5 / Math.sqrt(x)) + (Math.pow(x, -1.5) * -0.375)) / x;
}
def code(x): return ((0.5 / math.sqrt(x)) + (math.pow(x, -1.5) * -0.375)) / x
function code(x) return Float64(Float64(Float64(0.5 / sqrt(x)) + Float64((x ^ -1.5) * -0.375)) / x) end
function tmp = code(x) tmp = ((0.5 / sqrt(x)) + ((x ^ -1.5) * -0.375)) / x; end
code[x_] := N[(N[(N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(N[Power[x, -1.5], $MachinePrecision] * -0.375), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{\sqrt{x}} + {x}^{-1.5} \cdot -0.375}{x}
\end{array}
Initial program 43.0%
Taylor expanded in x around inf 84.8%
*-un-lft-identity84.8%
pow1/284.8%
pow-flip84.8%
pow-pow84.8%
metadata-eval84.8%
metadata-eval84.8%
Applied egg-rr84.8%
*-lft-identity84.8%
Simplified84.8%
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%
*-rgt-identity98.6%
times-frac98.6%
metadata-eval98.6%
Simplified98.6%
*-un-lft-identity98.6%
Applied egg-rr98.5%
*-lft-identity98.5%
Simplified98.5%
Final simplification98.5%
(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 43.0%
Taylor expanded in x around inf 84.8%
*-un-lft-identity84.8%
pow1/284.8%
pow-flip84.8%
pow-pow84.8%
metadata-eval84.8%
metadata-eval84.8%
Applied egg-rr84.8%
*-lft-identity84.8%
Simplified84.8%
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%
*-rgt-identity98.6%
times-frac98.6%
metadata-eval98.6%
Simplified98.6%
Taylor expanded in x around inf 97.7%
(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 43.0%
add-sqr-sqrt25.9%
sqrt-unprod43.0%
frac-times37.2%
metadata-eval37.2%
add-sqr-sqrt32.7%
+-commutative32.7%
Applied egg-rr32.7%
Taylor expanded in x around 0 5.7%
unpow-15.7%
metadata-eval5.7%
pow-sqr5.7%
rem-sqrt-square5.7%
rem-square-sqrt5.7%
fabs-sqr5.7%
rem-square-sqrt5.7%
Simplified5.7%
(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 2024148
(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)))))