
(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 5 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 (+ 1.0 x)) (+ (sqrt x) (* x (pow (+ 1.0 x) -0.5)))))
double code(double x) {
return (1.0 / (1.0 + x)) / (sqrt(x) + (x * pow((1.0 + x), -0.5)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (1.0d0 + x)) / (sqrt(x) + (x * ((1.0d0 + x) ** (-0.5d0))))
end function
public static double code(double x) {
return (1.0 / (1.0 + x)) / (Math.sqrt(x) + (x * Math.pow((1.0 + x), -0.5)));
}
def code(x): return (1.0 / (1.0 + x)) / (math.sqrt(x) + (x * math.pow((1.0 + x), -0.5)))
function code(x) return Float64(Float64(1.0 / Float64(1.0 + x)) / Float64(sqrt(x) + Float64(x * (Float64(1.0 + x) ^ -0.5)))) end
function tmp = code(x) tmp = (1.0 / (1.0 + x)) / (sqrt(x) + (x * ((1.0 + x) ^ -0.5))); end
code[x_] := N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[(x * N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{1 + x}}{\sqrt{x} + x \cdot {\left(1 + x\right)}^{-0.5}}
\end{array}
Initial program 41.4%
add-exp-log41.4%
inv-pow41.4%
sqrt-pow231.2%
metadata-eval31.2%
pow1/231.2%
pow-flip41.4%
+-commutative41.4%
metadata-eval41.4%
Applied egg-rr41.4%
flip--41.4%
add-exp-log41.4%
fmm-def8.8%
metadata-eval8.8%
sqrt-pow18.8%
inv-pow8.8%
metadata-eval8.8%
sqrt-pow18.8%
inv-pow8.8%
pow-prod-up8.7%
metadata-eval8.7%
inv-pow8.7%
fmm-def23.1%
add-sqr-sqrt41.5%
Applied egg-rr42.5%
*-rgt-identity42.5%
*-rgt-identity42.5%
associate--l+81.0%
+-inverses81.0%
metadata-eval81.0%
associate-*l*98.8%
Simplified98.8%
associate-/r*99.5%
*-un-lft-identity99.5%
times-frac99.3%
associate-/l/99.4%
*-commutative99.4%
Applied egg-rr99.4%
associate-*r/99.6%
*-rgt-identity99.6%
distribute-rgt-in99.6%
pow-plus99.6%
metadata-eval99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 2e-13) (* (pow x -1.5) 0.5) (- (pow x -0.5) (pow (+ 1.0 x) -0.5))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 2e-13) {
tmp = pow(x, -1.5) * 0.5;
} else {
tmp = pow(x, -0.5) - pow((1.0 + x), -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((1.0d0 + x)))) <= 2d-13) then
tmp = (x ** (-1.5d0)) * 0.5d0
else
tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-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((1.0 + x)))) <= 2e-13) {
tmp = Math.pow(x, -1.5) * 0.5;
} else {
tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 2e-13: tmp = math.pow(x, -1.5) * 0.5 else: tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(1.0 + x)))) <= 2e-13) tmp = Float64((x ^ -1.5) * 0.5); else tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 2e-13) tmp = (x ^ -1.5) * 0.5; else tmp = (x ^ -0.5) - ((1.0 + x) ^ -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[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-13], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 2 \cdot 10^{-13}:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\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))))) < 2.0000000000000001e-13Initial program 39.7%
flip--39.7%
frac-times21.5%
metadata-eval21.5%
add-sqr-sqrt16.3%
frac-times23.8%
metadata-eval23.8%
add-sqr-sqrt39.7%
+-commutative39.7%
inv-pow39.7%
sqrt-pow239.7%
metadata-eval39.7%
pow1/239.7%
pow-flip39.7%
+-commutative39.7%
metadata-eval39.7%
Applied egg-rr39.7%
Taylor expanded in x around inf 66.6%
*-commutative66.6%
Simplified66.6%
*-un-lft-identity66.6%
pow-flip67.3%
sqrt-pow199.4%
metadata-eval99.4%
metadata-eval99.4%
Applied egg-rr99.4%
*-lft-identity99.4%
Simplified99.4%
if 2.0000000000000001e-13 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 86.9%
sub-neg86.9%
inv-pow86.9%
sqrt-pow287.6%
metadata-eval87.6%
distribute-neg-frac87.6%
metadata-eval87.6%
+-commutative87.6%
Applied egg-rr87.6%
*-rgt-identity87.6%
cancel-sign-sub87.6%
distribute-lft-neg-in87.6%
*-rgt-identity87.6%
distribute-neg-frac87.6%
metadata-eval87.6%
unpow1/287.6%
exp-to-pow86.7%
log1p-undefine86.7%
*-commutative86.7%
exp-neg85.4%
*-commutative85.4%
distribute-rgt-neg-in85.4%
log1p-undefine85.4%
metadata-eval85.4%
exp-to-pow88.0%
Simplified88.0%
Final simplification99.0%
(FPCore (x) :precision binary64 (/ 1.0 (* x (+ (* (+ 1.0 x) (pow x -0.5)) (sqrt (+ 1.0 x))))))
double code(double x) {
return 1.0 / (x * (((1.0 + x) * pow(x, -0.5)) + sqrt((1.0 + x))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (x * (((1.0d0 + x) * (x ** (-0.5d0))) + sqrt((1.0d0 + x))))
end function
public static double code(double x) {
return 1.0 / (x * (((1.0 + x) * Math.pow(x, -0.5)) + Math.sqrt((1.0 + x))));
}
def code(x): return 1.0 / (x * (((1.0 + x) * math.pow(x, -0.5)) + math.sqrt((1.0 + x))))
function code(x) return Float64(1.0 / Float64(x * Float64(Float64(Float64(1.0 + x) * (x ^ -0.5)) + sqrt(Float64(1.0 + x))))) end
function tmp = code(x) tmp = 1.0 / (x * (((1.0 + x) * (x ^ -0.5)) + sqrt((1.0 + x)))); end
code[x_] := N[(1.0 / N[(x * N[(N[(N[(1.0 + x), $MachinePrecision] * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x \cdot \left(\left(1 + x\right) \cdot {x}^{-0.5} + \sqrt{1 + x}\right)}
\end{array}
Initial program 41.4%
add-exp-log41.4%
inv-pow41.4%
sqrt-pow231.2%
metadata-eval31.2%
pow1/231.2%
pow-flip41.4%
+-commutative41.4%
metadata-eval41.4%
Applied egg-rr41.4%
flip--41.4%
add-exp-log41.4%
fmm-def8.8%
metadata-eval8.8%
sqrt-pow18.8%
inv-pow8.8%
metadata-eval8.8%
sqrt-pow18.8%
inv-pow8.8%
pow-prod-up8.7%
metadata-eval8.7%
inv-pow8.7%
fmm-def23.1%
add-sqr-sqrt41.5%
Applied egg-rr42.5%
*-rgt-identity42.5%
*-rgt-identity42.5%
associate--l+81.0%
+-inverses81.0%
metadata-eval81.0%
associate-*l*98.8%
Simplified98.8%
distribute-lft-in98.8%
pow198.8%
pow-prod-up98.8%
metadata-eval98.8%
pow1/298.8%
Applied egg-rr98.8%
(FPCore (x) :precision binary64 (* (pow x -1.5) 0.5))
double code(double x) {
return pow(x, -1.5) * 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-1.5d0)) * 0.5d0
end function
public static double code(double x) {
return Math.pow(x, -1.5) * 0.5;
}
def code(x): return math.pow(x, -1.5) * 0.5
function code(x) return Float64((x ^ -1.5) * 0.5) end
function tmp = code(x) tmp = (x ^ -1.5) * 0.5; end
code[x_] := N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5} \cdot 0.5
\end{array}
Initial program 41.4%
flip--41.4%
frac-times23.9%
metadata-eval23.9%
add-sqr-sqrt18.8%
frac-times26.1%
metadata-eval26.1%
add-sqr-sqrt41.5%
+-commutative41.5%
inv-pow41.5%
sqrt-pow241.5%
metadata-eval41.5%
pow1/241.5%
pow-flip41.5%
+-commutative41.5%
metadata-eval41.5%
Applied egg-rr41.5%
Taylor expanded in x around inf 65.4%
*-commutative65.4%
Simplified65.4%
*-un-lft-identity65.4%
pow-flip66.1%
sqrt-pow197.0%
metadata-eval97.0%
metadata-eval97.0%
Applied egg-rr97.0%
*-lft-identity97.0%
Simplified97.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 41.4%
add-sqr-sqrt23.9%
fmm-def8.1%
inv-pow8.1%
sqrt-pow18.1%
metadata-eval8.1%
inv-pow8.1%
sqrt-pow18.1%
metadata-eval8.1%
distribute-neg-frac8.1%
metadata-eval8.1%
+-commutative8.1%
Applied egg-rr8.1%
Taylor expanded in x around inf 37.6%
distribute-rgt1-in37.6%
metadata-eval37.6%
mul0-lft37.6%
Simplified37.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 2024191
(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)))))