
(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
(if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ x 1.0)))) 0.0)
(/ (+ (* -0.5 (pow x -0.5)) (* -0.625 (/ (pow x -0.5) x))) (- x))
(/
(/ (- (+ x 1.0) x) (* x (+ x 1.0)))
(+ (pow x -0.5) (pow (+ x 1.0) -0.5)))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((x + 1.0)))) <= 0.0) {
tmp = ((-0.5 * pow(x, -0.5)) + (-0.625 * (pow(x, -0.5) / x))) / -x;
} else {
tmp = (((x + 1.0) - x) / (x * (x + 1.0))) / (pow(x, -0.5) + pow((x + 1.0), -0.5));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) + ((-1.0d0) / sqrt((x + 1.0d0)))) <= 0.0d0) then
tmp = (((-0.5d0) * (x ** (-0.5d0))) + ((-0.625d0) * ((x ** (-0.5d0)) / x))) / -x
else
tmp = (((x + 1.0d0) - x) / (x * (x + 1.0d0))) / ((x ** (-0.5d0)) + ((x + 1.0d0) ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) + (-1.0 / Math.sqrt((x + 1.0)))) <= 0.0) {
tmp = ((-0.5 * Math.pow(x, -0.5)) + (-0.625 * (Math.pow(x, -0.5) / x))) / -x;
} else {
tmp = (((x + 1.0) - x) / (x * (x + 1.0))) / (Math.pow(x, -0.5) + Math.pow((x + 1.0), -0.5));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((x + 1.0)))) <= 0.0: tmp = ((-0.5 * math.pow(x, -0.5)) + (-0.625 * (math.pow(x, -0.5) / x))) / -x else: tmp = (((x + 1.0) - x) / (x * (x + 1.0))) / (math.pow(x, -0.5) + math.pow((x + 1.0), -0.5)) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(x + 1.0)))) <= 0.0) tmp = Float64(Float64(Float64(-0.5 * (x ^ -0.5)) + Float64(-0.625 * Float64((x ^ -0.5) / x))) / Float64(-x)); else tmp = Float64(Float64(Float64(Float64(x + 1.0) - x) / Float64(x * Float64(x + 1.0))) / Float64((x ^ -0.5) + (Float64(x + 1.0) ^ -0.5))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((x + 1.0)))) <= 0.0) tmp = ((-0.5 * (x ^ -0.5)) + (-0.625 * ((x ^ -0.5) / x))) / -x; else tmp = (((x + 1.0) - x) / (x * (x + 1.0))) / ((x ^ -0.5) + ((x + 1.0) ^ -0.5)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(-0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] + N[(-0.625 * N[(N[Power[x, -0.5], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / (-x)), $MachinePrecision], N[(N[(N[(N[(x + 1.0), $MachinePrecision] - x), $MachinePrecision] / N[(x * N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{x + 1}} \leq 0:\\
\;\;\;\;\frac{-0.5 \cdot {x}^{-0.5} + -0.625 \cdot \frac{{x}^{-0.5}}{x}}{-x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\left(x + 1\right) - x}{x \cdot \left(x + 1\right)}}{{x}^{-0.5} + {\left(x + 1\right)}^{-0.5}}\\
\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.0%
Taylor expanded in x around inf 84.2%
Taylor expanded in x around -inf 0.0%
mul-1-neg0.0%
distribute-neg-frac20.0%
Simplified99.8%
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 51.9%
flip--51.5%
div-inv51.5%
frac-times52.1%
metadata-eval52.1%
add-sqr-sqrt52.1%
frac-times51.2%
metadata-eval51.2%
add-sqr-sqrt53.9%
+-commutative53.9%
inv-pow53.9%
sqrt-pow253.9%
metadata-eval53.9%
pow1/253.9%
pow-flip53.9%
+-commutative53.9%
metadata-eval53.9%
Applied egg-rr53.9%
associate-*r/53.9%
*-rgt-identity53.9%
Simplified53.9%
frac-sub99.4%
*-un-lft-identity99.4%
Applied egg-rr99.4%
Final simplification99.8%
(FPCore (x)
:precision binary64
(/
(/
(-
(* (* 0.5 (fma x 0.25 1.0)) (pow x -1.5))
(fma 0.5 (pow x -0.5) (* -0.5 (sqrt x))))
x)
x))
double code(double x) {
return ((((0.5 * fma(x, 0.25, 1.0)) * pow(x, -1.5)) - fma(0.5, pow(x, -0.5), (-0.5 * sqrt(x)))) / x) / x;
}
function code(x) return Float64(Float64(Float64(Float64(Float64(0.5 * fma(x, 0.25, 1.0)) * (x ^ -1.5)) - fma(0.5, (x ^ -0.5), Float64(-0.5 * sqrt(x)))) / x) / x) end
code[x_] := N[(N[(N[(N[(N[(0.5 * N[(x * 0.25 + 1.0), $MachinePrecision]), $MachinePrecision] * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision] - N[(0.5 * N[Power[x, -0.5], $MachinePrecision] + N[(-0.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(0.5 \cdot \mathsf{fma}\left(x, 0.25, 1\right)\right) \cdot {x}^{-1.5} - \mathsf{fma}\left(0.5, {x}^{-0.5}, -0.5 \cdot \sqrt{x}\right)}{x}}{x}
\end{array}
Initial program 35.0%
Taylor expanded in x around inf 84.3%
*-un-lft-identity84.3%
unpow284.3%
times-frac98.8%
Applied egg-rr98.8%
associate-*l/98.9%
*-lft-identity98.9%
*-commutative98.9%
fma-undefine98.9%
*-commutative98.9%
+-commutative98.9%
associate-*r*98.9%
+-commutative98.9%
*-commutative98.9%
fma-undefine98.9%
*-commutative98.9%
Simplified98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ x 1.0)))) 0.0) (/ (+ (* -0.5 (pow x -0.5)) (* -0.625 (/ (pow x -0.5) x))) (- x)) (/ (/ (/ 1.0 x) (+ x 1.0)) (+ (pow x -0.5) (pow (+ x 1.0) -0.5)))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((x + 1.0)))) <= 0.0) {
tmp = ((-0.5 * pow(x, -0.5)) + (-0.625 * (pow(x, -0.5) / x))) / -x;
} else {
tmp = ((1.0 / x) / (x + 1.0)) / (pow(x, -0.5) + pow((x + 1.0), -0.5));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) + ((-1.0d0) / sqrt((x + 1.0d0)))) <= 0.0d0) then
tmp = (((-0.5d0) * (x ** (-0.5d0))) + ((-0.625d0) * ((x ** (-0.5d0)) / x))) / -x
else
tmp = ((1.0d0 / x) / (x + 1.0d0)) / ((x ** (-0.5d0)) + ((x + 1.0d0) ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) + (-1.0 / Math.sqrt((x + 1.0)))) <= 0.0) {
tmp = ((-0.5 * Math.pow(x, -0.5)) + (-0.625 * (Math.pow(x, -0.5) / x))) / -x;
} else {
tmp = ((1.0 / x) / (x + 1.0)) / (Math.pow(x, -0.5) + Math.pow((x + 1.0), -0.5));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((x + 1.0)))) <= 0.0: tmp = ((-0.5 * math.pow(x, -0.5)) + (-0.625 * (math.pow(x, -0.5) / x))) / -x else: tmp = ((1.0 / x) / (x + 1.0)) / (math.pow(x, -0.5) + math.pow((x + 1.0), -0.5)) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(x + 1.0)))) <= 0.0) tmp = Float64(Float64(Float64(-0.5 * (x ^ -0.5)) + Float64(-0.625 * Float64((x ^ -0.5) / x))) / Float64(-x)); else tmp = Float64(Float64(Float64(1.0 / x) / Float64(x + 1.0)) / Float64((x ^ -0.5) + (Float64(x + 1.0) ^ -0.5))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((x + 1.0)))) <= 0.0) tmp = ((-0.5 * (x ^ -0.5)) + (-0.625 * ((x ^ -0.5) / x))) / -x; else tmp = ((1.0 / x) / (x + 1.0)) / ((x ^ -0.5) + ((x + 1.0) ^ -0.5)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(-0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] + N[(-0.625 * N[(N[Power[x, -0.5], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / (-x)), $MachinePrecision], N[(N[(N[(1.0 / x), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{x + 1}} \leq 0:\\
\;\;\;\;\frac{-0.5 \cdot {x}^{-0.5} + -0.625 \cdot \frac{{x}^{-0.5}}{x}}{-x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\frac{1}{x}}{x + 1}}{{x}^{-0.5} + {\left(x + 1\right)}^{-0.5}}\\
\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.0%
Taylor expanded in x around inf 84.2%
Taylor expanded in x around -inf 0.0%
mul-1-neg0.0%
distribute-neg-frac20.0%
Simplified99.8%
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 51.9%
flip--51.5%
div-inv51.5%
frac-times52.1%
metadata-eval52.1%
add-sqr-sqrt52.1%
frac-times51.2%
metadata-eval51.2%
add-sqr-sqrt53.9%
+-commutative53.9%
inv-pow53.9%
sqrt-pow253.9%
metadata-eval53.9%
pow1/253.9%
pow-flip53.9%
+-commutative53.9%
metadata-eval53.9%
Applied egg-rr53.9%
associate-*r/53.9%
*-rgt-identity53.9%
Simplified53.9%
frac-sub99.4%
*-un-lft-identity99.4%
Applied egg-rr99.4%
/-rgt-identity99.4%
*-lft-identity99.4%
/-rgt-identity99.4%
*-lft-identity99.4%
/-rgt-identity99.4%
*-rgt-identity99.4%
/-rgt-identity99.4%
associate--l+99.4%
+-inverses99.4%
metadata-eval99.4%
associate-/r*99.3%
Simplified99.3%
Final simplification99.8%
(FPCore (x) :precision binary64 (/ (/ (- (* 0.125 (sqrt (/ 1.0 x))) (fma 0.5 (pow x -0.5) (* -0.5 (sqrt x)))) x) x))
double code(double x) {
return (((0.125 * sqrt((1.0 / x))) - fma(0.5, pow(x, -0.5), (-0.5 * sqrt(x)))) / x) / x;
}
function code(x) return Float64(Float64(Float64(Float64(0.125 * sqrt(Float64(1.0 / x))) - fma(0.5, (x ^ -0.5), Float64(-0.5 * sqrt(x)))) / x) / x) end
code[x_] := N[(N[(N[(N[(0.125 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(0.5 * N[Power[x, -0.5], $MachinePrecision] + N[(-0.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{0.125 \cdot \sqrt{\frac{1}{x}} - \mathsf{fma}\left(0.5, {x}^{-0.5}, -0.5 \cdot \sqrt{x}\right)}{x}}{x}
\end{array}
Initial program 35.0%
Taylor expanded in x around inf 84.3%
*-un-lft-identity84.3%
unpow284.3%
times-frac98.8%
Applied egg-rr98.8%
associate-*l/98.9%
*-lft-identity98.9%
*-commutative98.9%
fma-undefine98.9%
*-commutative98.9%
+-commutative98.9%
associate-*r*98.9%
+-commutative98.9%
*-commutative98.9%
fma-undefine98.9%
*-commutative98.9%
Simplified98.9%
Taylor expanded in x around inf 98.9%
Final simplification98.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.0%
Taylor expanded in x around inf 84.3%
*-un-lft-identity84.3%
unpow284.3%
times-frac98.8%
Applied egg-rr98.8%
associate-*l/98.9%
*-lft-identity98.9%
*-commutative98.9%
fma-undefine98.9%
*-commutative98.9%
+-commutative98.9%
associate-*r*98.9%
+-commutative98.9%
*-commutative98.9%
fma-undefine98.9%
*-commutative98.9%
Simplified98.9%
Taylor expanded in x around inf 98.0%
(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 35.0%
add-cube-cbrt10.5%
associate-*l*10.5%
frac-2neg10.5%
metadata-eval10.5%
div-inv10.5%
distribute-neg-frac210.5%
prod-diff6.4%
Applied egg-rr6.4%
+-commutative6.4%
Simplified6.4%
Taylor expanded in x around inf 32.4%
distribute-rgt1-in32.4%
metadata-eval32.4%
mul0-lft32.4%
Simplified32.4%
(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 2024086
(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)))))