
(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 7 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 (sqrt (+ x 1.0))))
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 t_0)) 0.0)
(/ (* 0.5 (sqrt (/ 1.0 x))) x)
(/ (- (+ x 1.0) x) (* (+ t_0 (sqrt x)) (sqrt (fma x x x)))))))
double code(double x) {
double t_0 = sqrt((x + 1.0));
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / t_0)) <= 0.0) {
tmp = (0.5 * sqrt((1.0 / x))) / x;
} else {
tmp = ((x + 1.0) - x) / ((t_0 + sqrt(x)) * sqrt(fma(x, x, x)));
}
return tmp;
}
function code(x) t_0 = sqrt(Float64(x + 1.0)) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / t_0)) <= 0.0) tmp = Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / x); else tmp = Float64(Float64(Float64(x + 1.0) - x) / Float64(Float64(t_0 + sqrt(x)) * sqrt(fma(x, x, x)))); end return tmp end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / t$95$0), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(N[(x + 1.0), $MachinePrecision] - x), $MachinePrecision] / N[(N[(t$95$0 + N[Sqrt[x], $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(x * x + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{x + 1}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t\_0} \leq 0:\\
\;\;\;\;\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(x + 1\right) - x}{\left(t\_0 + \sqrt{x}\right) \cdot \sqrt{\mathsf{fma}\left(x, x, x\right)}}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) < 0.0Initial program 34.5%
Taylor expanded in x around inf
div-subN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-out--N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower--.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
lower-sqrt.f6481.9
Applied rewrites81.9%
Applied rewrites99.6%
Taylor expanded in x around inf
Applied rewrites99.7%
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 60.4%
Applied rewrites99.4%
Final simplification99.7%
(FPCore (x)
:precision binary64
(let* ((t_0 (sqrt (+ x 1.0))))
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 t_0)) 0.0)
(/ (* 0.5 (sqrt (/ 1.0 x))) x)
(/ (/ (- (+ x 1.0) x) (+ (sqrt (fma x x x)) x)) t_0))))
double code(double x) {
double t_0 = sqrt((x + 1.0));
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / t_0)) <= 0.0) {
tmp = (0.5 * sqrt((1.0 / x))) / x;
} else {
tmp = (((x + 1.0) - x) / (sqrt(fma(x, x, x)) + x)) / t_0;
}
return tmp;
}
function code(x) t_0 = sqrt(Float64(x + 1.0)) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / t_0)) <= 0.0) tmp = Float64(Float64(0.5 * sqrt(Float64(1.0 / x))) / x); else tmp = Float64(Float64(Float64(Float64(x + 1.0) - x) / Float64(sqrt(fma(x, x, x)) + x)) / t_0); end return tmp end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / t$95$0), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(N[(N[(x + 1.0), $MachinePrecision] - x), $MachinePrecision] / N[(N[Sqrt[N[(x * x + x), $MachinePrecision]], $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{x + 1}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t\_0} \leq 0:\\
\;\;\;\;\frac{0.5 \cdot \sqrt{\frac{1}{x}}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\left(x + 1\right) - x}{\sqrt{\mathsf{fma}\left(x, x, x\right)} + x}}{t\_0}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) < 0.0Initial program 34.5%
Taylor expanded in x around inf
div-subN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-out--N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower--.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
lower-sqrt.f6481.9
Applied rewrites81.9%
Applied rewrites99.6%
Taylor expanded in x around inf
Applied rewrites99.7%
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 60.4%
Applied rewrites99.2%
Final simplification99.7%
(FPCore (x) :precision binary64 (/ (/ (- (/ -0.5 x) -0.5) (sqrt x)) x))
double code(double x) {
return (((-0.5 / x) - -0.5) / sqrt(x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((((-0.5d0) / x) - (-0.5d0)) / sqrt(x)) / x
end function
public static double code(double x) {
return (((-0.5 / x) - -0.5) / Math.sqrt(x)) / x;
}
def code(x): return (((-0.5 / x) - -0.5) / math.sqrt(x)) / x
function code(x) return Float64(Float64(Float64(Float64(-0.5 / x) - -0.5) / sqrt(x)) / x) end
function tmp = code(x) tmp = (((-0.5 / x) - -0.5) / sqrt(x)) / x; end
code[x_] := N[(N[(N[(N[(-0.5 / x), $MachinePrecision] - -0.5), $MachinePrecision] / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\frac{-0.5}{x} - -0.5}{\sqrt{x}}}{x}
\end{array}
Initial program 35.7%
Taylor expanded in x around inf
div-subN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-out--N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower--.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
lower-sqrt.f6481.0
Applied rewrites81.0%
Applied rewrites97.8%
Applied rewrites97.9%
Taylor expanded in x around 0
Applied rewrites97.9%
(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 35.7%
Taylor expanded in x around inf
div-subN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-out--N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower--.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
lower-sqrt.f6481.0
Applied rewrites81.0%
Applied rewrites97.8%
Taylor expanded in x around inf
Applied rewrites97.9%
Final simplification97.9%
(FPCore (x) :precision binary64 (/ (/ 0.5 (sqrt x)) x))
double code(double x) {
return (0.5 / sqrt(x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 / sqrt(x)) / x
end function
public static double code(double x) {
return (0.5 / Math.sqrt(x)) / x;
}
def code(x): return (0.5 / math.sqrt(x)) / x
function code(x) return Float64(Float64(0.5 / sqrt(x)) / x) end
function tmp = code(x) tmp = (0.5 / sqrt(x)) / x; end
code[x_] := N[(N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.5}{\sqrt{x}}}{x}
\end{array}
Initial program 35.7%
Taylor expanded in x around inf
div-subN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-out--N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower--.f64N/A
lower-sqrt.f64N/A
lower-/.f64N/A
lower-sqrt.f6481.0
Applied rewrites81.0%
Applied rewrites97.8%
Applied rewrites97.9%
Taylor expanded in x around inf
Applied rewrites97.8%
(FPCore (x) :precision binary64 (/ (* 0.5 (sqrt x)) (* x x)))
double code(double x) {
return (0.5 * sqrt(x)) / (x * x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.5d0 * sqrt(x)) / (x * x)
end function
public static double code(double x) {
return (0.5 * Math.sqrt(x)) / (x * x);
}
def code(x): return (0.5 * math.sqrt(x)) / (x * x)
function code(x) return Float64(Float64(0.5 * sqrt(x)) / Float64(x * x)) end
function tmp = code(x) tmp = (0.5 * sqrt(x)) / (x * x); end
code[x_] := N[(N[(0.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \sqrt{x}}{x \cdot x}
\end{array}
Initial program 35.7%
Taylor expanded in x around inf
Applied rewrites82.1%
Taylor expanded in x around inf
Applied rewrites80.9%
Final simplification80.9%
(FPCore (x) :precision binary64 (sqrt (/ 1.0 x)))
double code(double x) {
return sqrt((1.0 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((1.0d0 / x))
end function
public static double code(double x) {
return Math.sqrt((1.0 / x));
}
def code(x): return math.sqrt((1.0 / x))
function code(x) return sqrt(Float64(1.0 / x)) end
function tmp = code(x) tmp = sqrt((1.0 / x)); end
code[x_] := N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{1}{x}}
\end{array}
Initial program 35.7%
Taylor expanded in x around 0
lower-sqrt.f64N/A
lower-/.f645.6
Applied rewrites5.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}
(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 2024235
(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)))))