
(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 (* (pow (+ 1.0 x) -0.5) (- (+ (/ 0.0625 (pow x 3.0)) (/ 0.5 x)) (/ 0.125 (pow x 2.0)))))
double code(double x) {
return pow((1.0 + x), -0.5) * (((0.0625 / pow(x, 3.0)) + (0.5 / x)) - (0.125 / pow(x, 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 + x) ** (-0.5d0)) * (((0.0625d0 / (x ** 3.0d0)) + (0.5d0 / x)) - (0.125d0 / (x ** 2.0d0)))
end function
public static double code(double x) {
return Math.pow((1.0 + x), -0.5) * (((0.0625 / Math.pow(x, 3.0)) + (0.5 / x)) - (0.125 / Math.pow(x, 2.0)));
}
def code(x): return math.pow((1.0 + x), -0.5) * (((0.0625 / math.pow(x, 3.0)) + (0.5 / x)) - (0.125 / math.pow(x, 2.0)))
function code(x) return Float64((Float64(1.0 + x) ^ -0.5) * Float64(Float64(Float64(0.0625 / (x ^ 3.0)) + Float64(0.5 / x)) - Float64(0.125 / (x ^ 2.0)))) end
function tmp = code(x) tmp = ((1.0 + x) ^ -0.5) * (((0.0625 / (x ^ 3.0)) + (0.5 / x)) - (0.125 / (x ^ 2.0))); end
code[x_] := N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] * N[(N[(N[(0.0625 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(0.5 / x), $MachinePrecision]), $MachinePrecision] - N[(0.125 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(1 + x\right)}^{-0.5} \cdot \left(\left(\frac{0.0625}{{x}^{3}} + \frac{0.5}{x}\right) - \frac{0.125}{{x}^{2}}\right)
\end{array}
Initial program 40.0%
frac-sub40.0%
div-inv40.0%
*-un-lft-identity40.0%
+-commutative40.0%
*-rgt-identity40.0%
metadata-eval40.0%
frac-times40.0%
associate-*l/40.0%
*-un-lft-identity40.0%
pow1/240.0%
pow-flip40.0%
+-commutative40.0%
metadata-eval40.0%
Applied egg-rr40.0%
*-commutative40.0%
/-rgt-identity40.0%
times-frac40.0%
*-commutative40.0%
times-frac40.0%
div-sub40.0%
sub-neg40.0%
*-inverses40.0%
metadata-eval40.0%
Simplified40.0%
/-rgt-identity40.0%
*-commutative40.0%
add-exp-log40.0%
log-prod40.0%
log-pow40.0%
log1p-define40.0%
sqrt-undiv40.0%
Applied egg-rr40.0%
prod-exp40.0%
*-commutative40.0%
log1p-undefine40.0%
exp-to-pow40.0%
rem-exp-log40.0%
+-commutative40.0%
Simplified40.0%
Taylor expanded in x around inf 99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (* (pow (+ 1.0 x) -0.5) (+ (/ 0.5 x) (/ -0.125 (pow x 2.0)))))
double code(double x) {
return pow((1.0 + x), -0.5) * ((0.5 / x) + (-0.125 / pow(x, 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 + x) ** (-0.5d0)) * ((0.5d0 / x) + ((-0.125d0) / (x ** 2.0d0)))
end function
public static double code(double x) {
return Math.pow((1.0 + x), -0.5) * ((0.5 / x) + (-0.125 / Math.pow(x, 2.0)));
}
def code(x): return math.pow((1.0 + x), -0.5) * ((0.5 / x) + (-0.125 / math.pow(x, 2.0)))
function code(x) return Float64((Float64(1.0 + x) ^ -0.5) * Float64(Float64(0.5 / x) + Float64(-0.125 / (x ^ 2.0)))) end
function tmp = code(x) tmp = ((1.0 + x) ^ -0.5) * ((0.5 / x) + (-0.125 / (x ^ 2.0))); end
code[x_] := N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] * N[(N[(0.5 / x), $MachinePrecision] + N[(-0.125 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(1 + x\right)}^{-0.5} \cdot \left(\frac{0.5}{x} + \frac{-0.125}{{x}^{2}}\right)
\end{array}
Initial program 40.0%
frac-sub40.0%
div-inv40.0%
*-un-lft-identity40.0%
+-commutative40.0%
*-rgt-identity40.0%
metadata-eval40.0%
frac-times40.0%
associate-*l/40.0%
*-un-lft-identity40.0%
pow1/240.0%
pow-flip40.0%
+-commutative40.0%
metadata-eval40.0%
Applied egg-rr40.0%
*-commutative40.0%
/-rgt-identity40.0%
times-frac40.0%
*-commutative40.0%
times-frac40.0%
div-sub40.0%
sub-neg40.0%
*-inverses40.0%
metadata-eval40.0%
Simplified40.0%
/-rgt-identity40.0%
*-commutative40.0%
add-exp-log40.0%
log-prod40.0%
log-pow40.0%
log1p-define40.0%
sqrt-undiv40.0%
Applied egg-rr40.0%
prod-exp40.0%
*-commutative40.0%
log1p-undefine40.0%
exp-to-pow40.0%
rem-exp-log40.0%
+-commutative40.0%
Simplified40.0%
Taylor expanded in x around inf 99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
Taylor expanded in x around inf 98.7%
sub-neg98.7%
associate-*r/98.7%
metadata-eval98.7%
associate-*r/98.7%
metadata-eval98.7%
distribute-neg-frac98.7%
metadata-eval98.7%
Simplified98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (if (<= x 4.6e+153) (/ 1.0 (+ x (sqrt x))) 0.0))
double code(double x) {
double tmp;
if (x <= 4.6e+153) {
tmp = 1.0 / (x + sqrt(x));
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.6d+153) then
tmp = 1.0d0 / (x + sqrt(x))
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.6e+153) {
tmp = 1.0 / (x + Math.sqrt(x));
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.6e+153: tmp = 1.0 / (x + math.sqrt(x)) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 4.6e+153) tmp = Float64(1.0 / Float64(x + sqrt(x))); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.6e+153) tmp = 1.0 / (x + sqrt(x)); else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.6e+153], N[(1.0 / N[(x + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.6 \cdot 10^{+153}:\\
\;\;\;\;\frac{1}{x + \sqrt{x}}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 4.6000000000000003e153Initial program 9.9%
flip--10.1%
div-inv10.1%
frac-times10.2%
metadata-eval10.2%
add-sqr-sqrt9.9%
frac-times9.8%
metadata-eval9.8%
add-sqr-sqrt10.1%
+-commutative10.1%
inv-pow10.1%
sqrt-pow210.1%
metadata-eval10.1%
pow1/210.1%
pow-flip10.1%
+-commutative10.1%
metadata-eval10.1%
Applied egg-rr10.1%
frac-2neg10.1%
metadata-eval10.1%
frac-sub13.0%
*-un-lft-identity13.0%
distribute-neg-in13.0%
metadata-eval13.0%
distribute-neg-in13.0%
metadata-eval13.0%
Applied egg-rr13.0%
Taylor expanded in x around 0 8.2%
distribute-rgt-in8.2%
*-lft-identity8.2%
pow-plus8.2%
metadata-eval8.2%
unpow1/28.2%
Simplified8.2%
if 4.6000000000000003e153 < x Initial program 67.8%
Taylor expanded in x around inf 67.8%
Final simplification39.2%
(FPCore (x) :precision binary64 (* (pow (+ 1.0 x) -0.5) (/ 0.5 x)))
double code(double x) {
return pow((1.0 + x), -0.5) * (0.5 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 + x) ** (-0.5d0)) * (0.5d0 / x)
end function
public static double code(double x) {
return Math.pow((1.0 + x), -0.5) * (0.5 / x);
}
def code(x): return math.pow((1.0 + x), -0.5) * (0.5 / x)
function code(x) return Float64((Float64(1.0 + x) ^ -0.5) * Float64(0.5 / x)) end
function tmp = code(x) tmp = ((1.0 + x) ^ -0.5) * (0.5 / x); end
code[x_] := N[(N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision] * N[(0.5 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(1 + x\right)}^{-0.5} \cdot \frac{0.5}{x}
\end{array}
Initial program 40.0%
frac-sub40.0%
div-inv40.0%
*-un-lft-identity40.0%
+-commutative40.0%
*-rgt-identity40.0%
metadata-eval40.0%
frac-times40.0%
associate-*l/40.0%
*-un-lft-identity40.0%
pow1/240.0%
pow-flip40.0%
+-commutative40.0%
metadata-eval40.0%
Applied egg-rr40.0%
*-commutative40.0%
/-rgt-identity40.0%
times-frac40.0%
*-commutative40.0%
times-frac40.0%
div-sub40.0%
sub-neg40.0%
*-inverses40.0%
metadata-eval40.0%
Simplified40.0%
/-rgt-identity40.0%
*-commutative40.0%
add-exp-log40.0%
log-prod40.0%
log-pow40.0%
log1p-define40.0%
sqrt-undiv40.0%
Applied egg-rr40.0%
prod-exp40.0%
*-commutative40.0%
log1p-undefine40.0%
exp-to-pow40.0%
rem-exp-log40.0%
+-commutative40.0%
Simplified40.0%
Taylor expanded in x around inf 99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
Taylor expanded in x around inf 97.8%
Final simplification97.8%
(FPCore (x) :precision binary64 (if (<= x 4.6e+153) (/ 1.0 x) 0.0))
double code(double x) {
double tmp;
if (x <= 4.6e+153) {
tmp = 1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.6d+153) then
tmp = 1.0d0 / x
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.6e+153) {
tmp = 1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.6e+153: tmp = 1.0 / x else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 4.6e+153) tmp = Float64(1.0 / x); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.6e+153) tmp = 1.0 / x; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.6e+153], N[(1.0 / x), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.6 \cdot 10^{+153}:\\
\;\;\;\;\frac{1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 4.6000000000000003e153Initial program 9.9%
flip--10.1%
div-inv10.1%
frac-times10.2%
metadata-eval10.2%
add-sqr-sqrt9.9%
frac-times9.8%
metadata-eval9.8%
add-sqr-sqrt10.1%
+-commutative10.1%
inv-pow10.1%
sqrt-pow210.1%
metadata-eval10.1%
pow1/210.1%
pow-flip10.1%
+-commutative10.1%
metadata-eval10.1%
Applied egg-rr10.1%
Taylor expanded in x around 0 8.2%
distribute-rgt-in8.2%
*-lft-identity8.2%
pow-plus8.2%
metadata-eval8.2%
Simplified8.2%
Taylor expanded in x around inf 8.2%
if 4.6000000000000003e153 < x Initial program 67.8%
Taylor expanded in x around inf 67.8%
Final simplification39.2%
(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 40.0%
Taylor expanded in x around inf 37.4%
Final simplification37.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 2024046
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:herbie-target
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))