
(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 6 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.3125 (pow x -3.5)) (fma -0.5 (/ (pow x -0.5) x) (* (/ (pow x -0.5) (pow x 2.0)) 0.375))))
double code(double x) {
return (0.3125 * pow(x, -3.5)) - fma(-0.5, (pow(x, -0.5) / x), ((pow(x, -0.5) / pow(x, 2.0)) * 0.375));
}
function code(x) return Float64(Float64(0.3125 * (x ^ -3.5)) - fma(-0.5, Float64((x ^ -0.5) / x), Float64(Float64((x ^ -0.5) / (x ^ 2.0)) * 0.375))) end
code[x_] := N[(N[(0.3125 * N[Power[x, -3.5], $MachinePrecision]), $MachinePrecision] - N[(-0.5 * N[(N[Power[x, -0.5], $MachinePrecision] / x), $MachinePrecision] + N[(N[(N[Power[x, -0.5], $MachinePrecision] / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] * 0.375), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.3125 \cdot {x}^{-3.5} - \mathsf{fma}\left(-0.5, \frac{{x}^{-0.5}}{x}, \frac{{x}^{-0.5}}{{x}^{2}} \cdot 0.375\right)
\end{array}
Initial program 46.0%
add-exp-log7.4%
log-rec7.4%
pow1/27.4%
log-pow7.4%
+-commutative7.4%
log1p-define7.4%
Applied egg-rr7.4%
Taylor expanded in x around inf 7.1%
associate--r+7.8%
Simplified99.0%
sub-neg99.0%
pow-div99.0%
metadata-eval99.0%
Applied egg-rr99.0%
fma-undefine99.0%
associate-+r+99.4%
sub-neg99.4%
+-inverses99.4%
+-commutative99.4%
+-lft-identity99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x) :precision binary64 (/ (/ (- (+ 0.5 (/ 0.3125 (pow x 2.0))) (+ (/ 0.375 x) (/ 0.2734375 (pow x 3.0)))) x) (sqrt x)))
double code(double x) {
return (((0.5 + (0.3125 / pow(x, 2.0))) - ((0.375 / x) + (0.2734375 / pow(x, 3.0)))) / x) / sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((0.5d0 + (0.3125d0 / (x ** 2.0d0))) - ((0.375d0 / x) + (0.2734375d0 / (x ** 3.0d0)))) / x) / sqrt(x)
end function
public static double code(double x) {
return (((0.5 + (0.3125 / Math.pow(x, 2.0))) - ((0.375 / x) + (0.2734375 / Math.pow(x, 3.0)))) / x) / Math.sqrt(x);
}
def code(x): return (((0.5 + (0.3125 / math.pow(x, 2.0))) - ((0.375 / x) + (0.2734375 / math.pow(x, 3.0)))) / x) / math.sqrt(x)
function code(x) return Float64(Float64(Float64(Float64(0.5 + Float64(0.3125 / (x ^ 2.0))) - Float64(Float64(0.375 / x) + Float64(0.2734375 / (x ^ 3.0)))) / x) / sqrt(x)) end
function tmp = code(x) tmp = (((0.5 + (0.3125 / (x ^ 2.0))) - ((0.375 / x) + (0.2734375 / (x ^ 3.0)))) / x) / sqrt(x); end
code[x_] := N[(N[(N[(N[(0.5 + N[(0.3125 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(0.375 / x), $MachinePrecision] + N[(0.2734375 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(0.5 + \frac{0.3125}{{x}^{2}}\right) - \left(\frac{0.375}{x} + \frac{0.2734375}{{x}^{3}}\right)}{x}}{\sqrt{x}}
\end{array}
Initial program 46.0%
expm1-log1p-u46.0%
expm1-undefine5.6%
inv-pow5.6%
sqrt-pow25.6%
metadata-eval5.6%
Applied egg-rr5.6%
Simplified33.1%
metadata-eval33.1%
pow-flip45.7%
pow1/246.0%
clear-num46.0%
+-commutative46.0%
frac-sub46.1%
*-un-lft-identity46.1%
Applied egg-rr46.1%
div-sub46.0%
/-rgt-identity46.0%
*-rgt-identity46.0%
/-rgt-identity46.0%
/-rgt-identity46.0%
div-sub46.1%
rem-square-sqrt46.1%
times-frac46.1%
associate-*l/46.1%
Simplified46.1%
Taylor expanded in x around inf 99.3%
associate-*r/99.3%
metadata-eval99.3%
associate-*r/99.3%
metadata-eval99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x) :precision binary64 (/ (/ (+ 0.5 (- (/ 0.3125 (pow x 2.0)) (/ 0.375 x))) x) (sqrt x)))
double code(double x) {
return ((0.5 + ((0.3125 / pow(x, 2.0)) - (0.375 / x))) / x) / sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 + ((0.3125d0 / (x ** 2.0d0)) - (0.375d0 / x))) / x) / sqrt(x)
end function
public static double code(double x) {
return ((0.5 + ((0.3125 / Math.pow(x, 2.0)) - (0.375 / x))) / x) / Math.sqrt(x);
}
def code(x): return ((0.5 + ((0.3125 / math.pow(x, 2.0)) - (0.375 / x))) / x) / math.sqrt(x)
function code(x) return Float64(Float64(Float64(0.5 + Float64(Float64(0.3125 / (x ^ 2.0)) - Float64(0.375 / x))) / x) / sqrt(x)) end
function tmp = code(x) tmp = ((0.5 + ((0.3125 / (x ^ 2.0)) - (0.375 / x))) / x) / sqrt(x); end
code[x_] := N[(N[(N[(0.5 + N[(N[(0.3125 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] - N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5 + \left(\frac{0.3125}{{x}^{2}} - \frac{0.375}{x}\right)}{x}}{\sqrt{x}}
\end{array}
Initial program 46.0%
expm1-log1p-u46.0%
expm1-undefine5.6%
inv-pow5.6%
sqrt-pow25.6%
metadata-eval5.6%
Applied egg-rr5.6%
Simplified33.1%
metadata-eval33.1%
pow-flip45.7%
pow1/246.0%
clear-num46.0%
+-commutative46.0%
frac-sub46.1%
*-un-lft-identity46.1%
Applied egg-rr46.1%
div-sub46.0%
/-rgt-identity46.0%
*-rgt-identity46.0%
/-rgt-identity46.0%
/-rgt-identity46.0%
div-sub46.1%
rem-square-sqrt46.1%
times-frac46.1%
associate-*l/46.1%
Simplified46.1%
Taylor expanded in x around inf 99.2%
associate--l+99.2%
associate-*r/99.2%
metadata-eval99.2%
Simplified99.2%
Final simplification99.2%
(FPCore (x) :precision binary64 (/ (/ (- 0.5 (/ 0.375 x)) x) (sqrt x)))
double code(double x) {
return ((0.5 - (0.375 / x)) / x) / sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.5d0 - (0.375d0 / x)) / x) / sqrt(x)
end function
public static double code(double x) {
return ((0.5 - (0.375 / x)) / x) / Math.sqrt(x);
}
def code(x): return ((0.5 - (0.375 / x)) / x) / math.sqrt(x)
function code(x) return Float64(Float64(Float64(0.5 - Float64(0.375 / x)) / x) / sqrt(x)) end
function tmp = code(x) tmp = ((0.5 - (0.375 / x)) / x) / sqrt(x); end
code[x_] := N[(N[(N[(0.5 - N[(0.375 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5 - \frac{0.375}{x}}{x}}{\sqrt{x}}
\end{array}
Initial program 46.0%
expm1-log1p-u46.0%
expm1-undefine5.6%
inv-pow5.6%
sqrt-pow25.6%
metadata-eval5.6%
Applied egg-rr5.6%
Simplified33.1%
metadata-eval33.1%
pow-flip45.7%
pow1/246.0%
clear-num46.0%
+-commutative46.0%
frac-sub46.1%
*-un-lft-identity46.1%
Applied egg-rr46.1%
div-sub46.0%
/-rgt-identity46.0%
*-rgt-identity46.0%
/-rgt-identity46.0%
/-rgt-identity46.0%
div-sub46.1%
rem-square-sqrt46.1%
times-frac46.1%
associate-*l/46.1%
Simplified46.1%
Taylor expanded in x around inf 98.8%
associate-*r/98.8%
metadata-eval98.8%
Simplified98.8%
Final simplification98.8%
(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 46.0%
frac-sub46.1%
*-un-lft-identity46.1%
*-rgt-identity46.1%
+-commutative46.1%
sqrt-unprod46.1%
+-commutative46.1%
Applied egg-rr46.1%
Taylor expanded in x around inf 45.3%
Taylor expanded in x around inf 70.0%
*-commutative70.0%
Simplified70.0%
*-un-lft-identity70.0%
pow-flip70.6%
sqrt-pow197.8%
metadata-eval97.8%
metadata-eval97.8%
Applied egg-rr97.8%
*-lft-identity97.8%
Simplified97.8%
Final simplification97.8%
(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 46.0%
expm1-log1p-u46.0%
expm1-undefine5.6%
inv-pow5.6%
sqrt-pow25.6%
metadata-eval5.6%
Applied egg-rr5.6%
Simplified33.1%
Taylor expanded in x around 0 5.5%
unpow1/25.5%
exp-to-pow5.5%
*-commutative5.5%
*-commutative5.5%
log-rec5.5%
distribute-lft-neg-out5.5%
distribute-rgt-neg-in5.5%
metadata-eval5.5%
exp-to-pow5.5%
Simplified5.5%
Final simplification5.5%
(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 2024112
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))