
(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 8 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 (fabs (pow x -2.5))))
(/
(+
(- (fma t_0 -0.25 (* (pow x -0.5) 0.5)) (* (pow x -0.5) (/ 0.375 x)))
(* t_0 0.5))
x)))
double code(double x) {
double t_0 = fabs(pow(x, -2.5));
return ((fma(t_0, -0.25, (pow(x, -0.5) * 0.5)) - (pow(x, -0.5) * (0.375 / x))) + (t_0 * 0.5)) / x;
}
function code(x) t_0 = abs((x ^ -2.5)) return Float64(Float64(Float64(fma(t_0, -0.25, Float64((x ^ -0.5) * 0.5)) - Float64((x ^ -0.5) * Float64(0.375 / x))) + Float64(t_0 * 0.5)) / x) end
code[x_] := Block[{t$95$0 = N[Abs[N[Power[x, -2.5], $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(t$95$0 * -0.25 + N[(N[Power[x, -0.5], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision] - N[(N[Power[x, -0.5], $MachinePrecision] * N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * 0.5), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left|{x}^{-2.5}\right|\\
\frac{\left(\mathsf{fma}\left(t\_0, -0.25, {x}^{-0.5} \cdot 0.5\right) - {x}^{-0.5} \cdot \frac{0.375}{x}\right) + t\_0 \cdot 0.5}{x}
\end{array}
\end{array}
Initial program 37.9%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around inf 99.1%
Simplified99.1%
(FPCore (x) :precision binary64 (/ (fma -1.0 (/ 0.375 (pow x 1.5)) (* 0.5 (sqrt (/ 1.0 x)))) x))
double code(double x) {
return fma(-1.0, (0.375 / pow(x, 1.5)), (0.5 * sqrt((1.0 / x)))) / x;
}
function code(x) return Float64(fma(-1.0, Float64(0.375 / (x ^ 1.5)), Float64(0.5 * sqrt(Float64(1.0 / x)))) / x) end
code[x_] := N[(N[(-1.0 * N[(0.375 / N[Power[x, 1.5], $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-1, \frac{0.375}{{x}^{1.5}}, 0.5 \cdot \sqrt{\frac{1}{x}}\right)}{x}
\end{array}
Initial program 37.9%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around inf 99.0%
fma-define99.0%
distribute-rgt-out99.0%
metadata-eval99.0%
*-commutative99.0%
Simplified99.0%
associate-/l*99.0%
inv-pow99.0%
sqrt-pow199.0%
metadata-eval99.0%
*-un-lft-identity99.0%
metadata-eval99.0%
pow-flip99.0%
pow1/299.0%
frac-times99.0%
metadata-eval99.0%
*-commutative99.0%
pow199.0%
pow1/299.0%
pow-prod-up99.0%
metadata-eval99.0%
Applied egg-rr99.0%
*-lft-identity99.0%
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (/ (- (* (pow x -0.5) 0.5) (* (pow x -0.5) (/ 0.375 x))) x))
double code(double x) {
return ((pow(x, -0.5) * 0.5) - (pow(x, -0.5) * (0.375 / x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (((x ** (-0.5d0)) * 0.5d0) - ((x ** (-0.5d0)) * (0.375d0 / x))) / x
end function
public static double code(double x) {
return ((Math.pow(x, -0.5) * 0.5) - (Math.pow(x, -0.5) * (0.375 / x))) / x;
}
def code(x): return ((math.pow(x, -0.5) * 0.5) - (math.pow(x, -0.5) * (0.375 / x))) / x
function code(x) return Float64(Float64(Float64((x ^ -0.5) * 0.5) - Float64((x ^ -0.5) * Float64(0.375 / x))) / x) end
function tmp = code(x) tmp = (((x ^ -0.5) * 0.5) - ((x ^ -0.5) * (0.375 / x))) / x; end
code[x_] := N[(N[(N[(N[Power[x, -0.5], $MachinePrecision] * 0.5), $MachinePrecision] - N[(N[Power[x, -0.5], $MachinePrecision] * N[(0.375 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-0.5} \cdot 0.5 - {x}^{-0.5} \cdot \frac{0.375}{x}}{x}
\end{array}
Initial program 37.9%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around inf 99.0%
Simplified99.0%
(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}} + \frac{-0.375}{{x}^{1.5}}}{x}
\end{array}
Initial program 37.9%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around inf 99.0%
fma-define99.0%
distribute-rgt-out99.0%
metadata-eval99.0%
*-commutative99.0%
Simplified99.0%
*-un-lft-identity99.0%
associate-/l*99.0%
sqrt-div99.0%
metadata-eval99.0%
frac-times99.0%
metadata-eval99.0%
*-commutative99.0%
pow199.0%
pow1/299.0%
pow-prod-up99.0%
metadata-eval99.0%
*-commutative99.0%
sqrt-div98.9%
metadata-eval98.9%
un-div-inv98.9%
Applied egg-rr98.9%
*-lft-identity98.9%
fma-undefine98.9%
neg-mul-198.9%
+-commutative98.9%
distribute-neg-frac98.9%
metadata-eval98.9%
Simplified98.9%
(FPCore (x) :precision binary64 (if (<= x 8.5e+122) (pow x -0.5) 0.0))
double code(double x) {
double tmp;
if (x <= 8.5e+122) {
tmp = pow(x, -0.5);
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 8.5d+122) then
tmp = x ** (-0.5d0)
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 8.5e+122) {
tmp = Math.pow(x, -0.5);
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 8.5e+122: tmp = math.pow(x, -0.5) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 8.5e+122) tmp = x ^ -0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 8.5e+122) tmp = x ^ -0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 8.5e+122], N[Power[x, -0.5], $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 8.5 \cdot 10^{+122}:\\
\;\;\;\;{x}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 8.50000000000000003e122Initial program 11.5%
frac-2neg11.5%
metadata-eval11.5%
div-inv11.5%
frac-2neg11.5%
metadata-eval11.5%
div-inv11.5%
distribute-neg-frac211.5%
prod-diff11.5%
distribute-neg-frac11.5%
metadata-eval11.5%
+-commutative11.5%
Applied egg-rr11.8%
Simplified11.8%
Taylor expanded in x around 0 7.7%
unpow1/27.7%
rem-exp-log7.7%
exp-neg7.7%
exp-prod7.7%
distribute-lft-neg-out7.7%
distribute-rgt-neg-in7.7%
metadata-eval7.7%
exp-to-pow7.7%
Simplified7.7%
if 8.50000000000000003e122 < x Initial program 57.2%
sub-neg57.2%
+-commutative57.2%
add-cube-cbrt7.6%
distribute-lft-neg-in7.6%
fma-define4.6%
Applied egg-rr4.5%
Taylor expanded in x around inf 57.2%
distribute-rgt1-in57.2%
metadata-eval57.2%
mul0-lft57.2%
Simplified57.2%
(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 37.9%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around inf 99.0%
fma-define99.0%
distribute-rgt-out99.0%
metadata-eval99.0%
*-commutative99.0%
Simplified99.0%
Taylor expanded in x around inf 97.6%
(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(Float64((x ^ -0.5) * 0.5) / x) end
function tmp = code(x) tmp = ((x ^ -0.5) * 0.5) / x; end
code[x_] := N[(N[(N[Power[x, -0.5], $MachinePrecision] * 0.5), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{{x}^{-0.5} \cdot 0.5}{x}
\end{array}
Initial program 37.9%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around inf 99.0%
fma-define99.0%
distribute-rgt-out99.0%
metadata-eval99.0%
*-commutative99.0%
Simplified99.0%
Taylor expanded in x around inf 97.6%
unpow1/297.6%
rem-exp-log93.6%
exp-neg93.6%
exp-prod93.6%
distribute-lft-neg-out93.6%
distribute-rgt-neg-in93.6%
metadata-eval93.6%
exp-to-pow97.6%
Simplified97.6%
Final simplification97.6%
(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.9%
sub-neg37.9%
+-commutative37.9%
add-cube-cbrt9.7%
distribute-lft-neg-in9.7%
fma-define8.1%
Applied egg-rr8.2%
Taylor expanded in x around inf 34.7%
distribute-rgt1-in34.7%
metadata-eval34.7%
mul0-lft34.7%
Simplified34.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}
(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)))))