
(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 (* (/ (/ 1.0 x) (+ 1.0 (sqrt (/ (+ x 1.0) x)))) (pow (+ x 1.0) -0.5)))
double code(double x) {
return ((1.0 / x) / (1.0 + sqrt(((x + 1.0) / x)))) * pow((x + 1.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / x) / (1.0d0 + sqrt(((x + 1.0d0) / x)))) * ((x + 1.0d0) ** (-0.5d0))
end function
public static double code(double x) {
return ((1.0 / x) / (1.0 + Math.sqrt(((x + 1.0) / x)))) * Math.pow((x + 1.0), -0.5);
}
def code(x): return ((1.0 / x) / (1.0 + math.sqrt(((x + 1.0) / x)))) * math.pow((x + 1.0), -0.5)
function code(x) return Float64(Float64(Float64(1.0 / x) / Float64(1.0 + sqrt(Float64(Float64(x + 1.0) / x)))) * (Float64(x + 1.0) ^ -0.5)) end
function tmp = code(x) tmp = ((1.0 / x) / (1.0 + sqrt(((x + 1.0) / x)))) * ((x + 1.0) ^ -0.5); end
code[x_] := N[(N[(N[(1.0 / x), $MachinePrecision] / N[(1.0 + N[Sqrt[N[(N[(x + 1.0), $MachinePrecision] / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{1 + \sqrt{\frac{x + 1}{x}}} \cdot {\left(x + 1\right)}^{-0.5}
\end{array}
Initial program 37.1%
frac-sub37.2%
div-inv37.2%
*-un-lft-identity37.2%
+-commutative37.2%
*-rgt-identity37.2%
metadata-eval37.2%
frac-times37.2%
associate-*l/37.2%
*-un-lft-identity37.2%
inv-pow37.2%
sqrt-pow237.2%
+-commutative37.2%
metadata-eval37.2%
Applied egg-rr37.2%
*-commutative37.2%
/-rgt-identity37.2%
times-frac37.2%
*-commutative37.2%
times-frac37.2%
div-sub37.1%
sub-neg37.1%
*-inverses37.1%
metadata-eval37.1%
Simplified37.1%
flip-+37.1%
frac-times37.1%
Applied egg-rr37.3%
*-commutative37.3%
*-rgt-identity37.3%
associate-/l*37.3%
+-commutative37.3%
+-commutative37.3%
Simplified37.3%
Taylor expanded in x around 0 99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (* (/ (+ (/ (- (/ (+ 0.0625 (* 0.0390625 (/ -1.0 x))) x) 0.125) x) 0.5) x) (pow (+ x 1.0) -0.5)))
double code(double x) {
return ((((((0.0625 + (0.0390625 * (-1.0 / x))) / x) - 0.125) / x) + 0.5) / x) * pow((x + 1.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((((((0.0625d0 + (0.0390625d0 * ((-1.0d0) / x))) / x) - 0.125d0) / x) + 0.5d0) / x) * ((x + 1.0d0) ** (-0.5d0))
end function
public static double code(double x) {
return ((((((0.0625 + (0.0390625 * (-1.0 / x))) / x) - 0.125) / x) + 0.5) / x) * Math.pow((x + 1.0), -0.5);
}
def code(x): return ((((((0.0625 + (0.0390625 * (-1.0 / x))) / x) - 0.125) / x) + 0.5) / x) * math.pow((x + 1.0), -0.5)
function code(x) return Float64(Float64(Float64(Float64(Float64(Float64(Float64(0.0625 + Float64(0.0390625 * Float64(-1.0 / x))) / x) - 0.125) / x) + 0.5) / x) * (Float64(x + 1.0) ^ -0.5)) end
function tmp = code(x) tmp = ((((((0.0625 + (0.0390625 * (-1.0 / x))) / x) - 0.125) / x) + 0.5) / x) * ((x + 1.0) ^ -0.5); end
code[x_] := N[(N[(N[(N[(N[(N[(N[(0.0625 + N[(0.0390625 * N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] - 0.125), $MachinePrecision] / x), $MachinePrecision] + 0.5), $MachinePrecision] / x), $MachinePrecision] * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\frac{0.0625 + 0.0390625 \cdot \frac{-1}{x}}{x} - 0.125}{x} + 0.5}{x} \cdot {\left(x + 1\right)}^{-0.5}
\end{array}
Initial program 37.1%
frac-sub37.2%
div-inv37.2%
*-un-lft-identity37.2%
+-commutative37.2%
*-rgt-identity37.2%
metadata-eval37.2%
frac-times37.2%
associate-*l/37.2%
*-un-lft-identity37.2%
inv-pow37.2%
sqrt-pow237.2%
+-commutative37.2%
metadata-eval37.2%
Applied egg-rr37.2%
*-commutative37.2%
/-rgt-identity37.2%
times-frac37.2%
*-commutative37.2%
times-frac37.2%
div-sub37.1%
sub-neg37.1%
*-inverses37.1%
metadata-eval37.1%
Simplified37.1%
Taylor expanded in x around inf 99.2%
associate--r+99.2%
+-commutative99.2%
associate--l+99.2%
sub-neg99.2%
associate-*r/99.2%
metadata-eval99.2%
distribute-neg-frac99.2%
metadata-eval99.2%
associate-*r/99.2%
metadata-eval99.2%
Simplified99.2%
Taylor expanded in x around -inf 99.2%
Final simplification99.2%
(FPCore (x) :precision binary64 (* (pow (+ x 1.0) -0.5) (/ (- 0.5 (/ (+ 0.125 (* 0.0625 (/ -1.0 x))) x)) x)))
double code(double x) {
return pow((x + 1.0), -0.5) * ((0.5 - ((0.125 + (0.0625 * (-1.0 / x))) / x)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x + 1.0d0) ** (-0.5d0)) * ((0.5d0 - ((0.125d0 + (0.0625d0 * ((-1.0d0) / x))) / x)) / x)
end function
public static double code(double x) {
return Math.pow((x + 1.0), -0.5) * ((0.5 - ((0.125 + (0.0625 * (-1.0 / x))) / x)) / x);
}
def code(x): return math.pow((x + 1.0), -0.5) * ((0.5 - ((0.125 + (0.0625 * (-1.0 / x))) / x)) / x)
function code(x) return Float64((Float64(x + 1.0) ^ -0.5) * Float64(Float64(0.5 - Float64(Float64(0.125 + Float64(0.0625 * Float64(-1.0 / x))) / x)) / x)) end
function tmp = code(x) tmp = ((x + 1.0) ^ -0.5) * ((0.5 - ((0.125 + (0.0625 * (-1.0 / x))) / x)) / x); end
code[x_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision] * N[(N[(0.5 - N[(N[(0.125 + N[(0.0625 * N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + 1\right)}^{-0.5} \cdot \frac{0.5 - \frac{0.125 + 0.0625 \cdot \frac{-1}{x}}{x}}{x}
\end{array}
Initial program 37.1%
frac-sub37.2%
div-inv37.2%
*-un-lft-identity37.2%
+-commutative37.2%
*-rgt-identity37.2%
metadata-eval37.2%
frac-times37.2%
associate-*l/37.2%
*-un-lft-identity37.2%
inv-pow37.2%
sqrt-pow237.2%
+-commutative37.2%
metadata-eval37.2%
Applied egg-rr37.2%
*-commutative37.2%
/-rgt-identity37.2%
times-frac37.2%
*-commutative37.2%
times-frac37.2%
div-sub37.1%
sub-neg37.1%
*-inverses37.1%
metadata-eval37.1%
Simplified37.1%
Taylor expanded in x around inf 99.2%
associate--r+99.2%
+-commutative99.2%
associate--l+99.2%
sub-neg99.2%
associate-*r/99.2%
metadata-eval99.2%
distribute-neg-frac99.2%
metadata-eval99.2%
associate-*r/99.2%
metadata-eval99.2%
Simplified99.2%
Taylor expanded in x around -inf 98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (* (pow (+ x 1.0) -0.5) (/ (- 0.5 (/ 0.125 x)) x)))
double code(double x) {
return pow((x + 1.0), -0.5) * ((0.5 - (0.125 / x)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x + 1.0d0) ** (-0.5d0)) * ((0.5d0 - (0.125d0 / x)) / x)
end function
public static double code(double x) {
return Math.pow((x + 1.0), -0.5) * ((0.5 - (0.125 / x)) / x);
}
def code(x): return math.pow((x + 1.0), -0.5) * ((0.5 - (0.125 / x)) / x)
function code(x) return Float64((Float64(x + 1.0) ^ -0.5) * Float64(Float64(0.5 - Float64(0.125 / x)) / x)) end
function tmp = code(x) tmp = ((x + 1.0) ^ -0.5) * ((0.5 - (0.125 / x)) / x); end
code[x_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision] * N[(N[(0.5 - N[(0.125 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + 1\right)}^{-0.5} \cdot \frac{0.5 - \frac{0.125}{x}}{x}
\end{array}
Initial program 37.1%
frac-sub37.2%
div-inv37.2%
*-un-lft-identity37.2%
+-commutative37.2%
*-rgt-identity37.2%
metadata-eval37.2%
frac-times37.2%
associate-*l/37.2%
*-un-lft-identity37.2%
inv-pow37.2%
sqrt-pow237.2%
+-commutative37.2%
metadata-eval37.2%
Applied egg-rr37.2%
*-commutative37.2%
/-rgt-identity37.2%
times-frac37.2%
*-commutative37.2%
times-frac37.2%
div-sub37.1%
sub-neg37.1%
*-inverses37.1%
metadata-eval37.1%
Simplified37.1%
Taylor expanded in x around inf 98.5%
associate-*r/98.5%
metadata-eval98.5%
Simplified98.5%
Final simplification98.5%
(FPCore (x) :precision binary64 (if (<= x 8.5e+122) (pow x -0.5) (/ -0.0390625 (pow x 4.0))))
double code(double x) {
double tmp;
if (x <= 8.5e+122) {
tmp = pow(x, -0.5);
} else {
tmp = -0.0390625 / pow(x, 4.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.0390625d0) / (x ** 4.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.0390625 / Math.pow(x, 4.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 8.5e+122: tmp = math.pow(x, -0.5) else: tmp = -0.0390625 / math.pow(x, 4.0) return tmp
function code(x) tmp = 0.0 if (x <= 8.5e+122) tmp = x ^ -0.5; else tmp = Float64(-0.0390625 / (x ^ 4.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.0390625 / (x ^ 4.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 8.5e+122], N[Power[x, -0.5], $MachinePrecision], N[(-0.0390625 / N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 8.5 \cdot 10^{+122}:\\
\;\;\;\;{x}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{-0.0390625}{{x}^{4}}\\
\end{array}
\end{array}
if x < 8.50000000000000003e122Initial program 12.0%
add-sqr-sqrt12.5%
sqrt-unprod12.0%
frac-times12.2%
metadata-eval12.2%
add-sqr-sqrt12.2%
+-commutative12.2%
Applied egg-rr12.2%
Taylor expanded in x around 0 7.5%
rem-exp-log7.5%
exp-neg7.5%
unpow1/27.5%
exp-prod7.5%
distribute-lft-neg-out7.5%
distribute-rgt-neg-in7.5%
metadata-eval7.5%
exp-to-pow7.5%
Simplified7.5%
if 8.50000000000000003e122 < x Initial program 53.8%
frac-sub53.8%
div-inv53.8%
*-un-lft-identity53.8%
+-commutative53.8%
*-rgt-identity53.8%
metadata-eval53.8%
frac-times53.8%
associate-*l/53.8%
*-un-lft-identity53.8%
inv-pow53.8%
sqrt-pow253.8%
+-commutative53.8%
metadata-eval53.8%
Applied egg-rr53.8%
*-commutative53.8%
/-rgt-identity53.8%
times-frac53.8%
*-commutative53.8%
times-frac53.8%
div-sub53.8%
sub-neg53.8%
*-inverses53.8%
metadata-eval53.8%
Simplified53.8%
Taylor expanded in x around inf 99.7%
associate--r+99.7%
+-commutative99.7%
associate--l+99.7%
sub-neg99.7%
associate-*r/99.7%
metadata-eval99.7%
distribute-neg-frac99.7%
metadata-eval99.7%
associate-*r/99.7%
metadata-eval99.7%
Simplified99.7%
Taylor expanded in x around 0 53.8%
Final simplification35.3%
(FPCore (x) :precision binary64 (* (pow (+ x 1.0) -0.5) (/ 0.5 x)))
double code(double x) {
return pow((x + 1.0), -0.5) * (0.5 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x + 1.0d0) ** (-0.5d0)) * (0.5d0 / x)
end function
public static double code(double x) {
return Math.pow((x + 1.0), -0.5) * (0.5 / x);
}
def code(x): return math.pow((x + 1.0), -0.5) * (0.5 / x)
function code(x) return Float64((Float64(x + 1.0) ^ -0.5) * Float64(0.5 / x)) end
function tmp = code(x) tmp = ((x + 1.0) ^ -0.5) * (0.5 / x); end
code[x_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision] * N[(0.5 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + 1\right)}^{-0.5} \cdot \frac{0.5}{x}
\end{array}
Initial program 37.1%
frac-sub37.2%
div-inv37.2%
*-un-lft-identity37.2%
+-commutative37.2%
*-rgt-identity37.2%
metadata-eval37.2%
frac-times37.2%
associate-*l/37.2%
*-un-lft-identity37.2%
inv-pow37.2%
sqrt-pow237.2%
+-commutative37.2%
metadata-eval37.2%
Applied egg-rr37.2%
*-commutative37.2%
/-rgt-identity37.2%
times-frac37.2%
*-commutative37.2%
times-frac37.2%
div-sub37.1%
sub-neg37.1%
*-inverses37.1%
metadata-eval37.1%
Simplified37.1%
Taylor expanded in x around inf 97.5%
Final simplification97.5%
(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.1%
Taylor expanded in x around inf 80.3%
Taylor expanded in x around inf 98.5%
Simplified98.6%
Taylor expanded in x around inf 97.4%
Final simplification97.4%
(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 37.1%
add-sqr-sqrt23.6%
sqrt-unprod37.1%
frac-times32.8%
metadata-eval32.8%
add-sqr-sqrt29.1%
+-commutative29.1%
Applied egg-rr29.1%
Taylor expanded in x around 0 5.7%
rem-exp-log5.7%
exp-neg5.7%
unpow1/25.7%
exp-prod5.7%
distribute-lft-neg-out5.7%
distribute-rgt-neg-in5.7%
metadata-eval5.7%
exp-to-pow5.7%
Simplified5.7%
Final simplification5.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}
herbie shell --seed 2024074
(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)))))