
(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
(if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 0.0)
(* (pow x -1.5) (+ 0.5 (/ 0.5 x)))
(/
1.0
(/
(+ (pow x -0.5) (pow (+ 1.0 x) -0.5))
(* (/ 1.0 (+ 1.0 x)) (/ 1.0 x))))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) {
tmp = pow(x, -1.5) * (0.5 + (0.5 / x));
} else {
tmp = 1.0 / ((pow(x, -0.5) + pow((1.0 + x), -0.5)) / ((1.0 / (1.0 + x)) * (1.0 / x)));
}
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)))) <= 0.0d0) then
tmp = (x ** (-1.5d0)) * (0.5d0 + (0.5d0 / x))
else
tmp = 1.0d0 / (((x ** (-0.5d0)) + ((1.0d0 + x) ** (-0.5d0))) / ((1.0d0 / (1.0d0 + x)) * (1.0d0 / x)))
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)))) <= 0.0) {
tmp = Math.pow(x, -1.5) * (0.5 + (0.5 / x));
} else {
tmp = 1.0 / ((Math.pow(x, -0.5) + Math.pow((1.0 + x), -0.5)) / ((1.0 / (1.0 + x)) * (1.0 / x)));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 0.0: tmp = math.pow(x, -1.5) * (0.5 + (0.5 / x)) else: tmp = 1.0 / ((math.pow(x, -0.5) + math.pow((1.0 + x), -0.5)) / ((1.0 / (1.0 + x)) * (1.0 / x))) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(1.0 + x)))) <= 0.0) tmp = Float64((x ^ -1.5) * Float64(0.5 + Float64(0.5 / x))); else tmp = Float64(1.0 / Float64(Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5)) / Float64(Float64(1.0 / Float64(1.0 + x)) * Float64(1.0 / x)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) tmp = (x ^ -1.5) * (0.5 + (0.5 / x)); else tmp = 1.0 / (((x ^ -0.5) + ((1.0 + x) ^ -0.5)) / ((1.0 / (1.0 + x)) * (1.0 / x))); 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], 0.0], N[(N[Power[x, -1.5], $MachinePrecision] * N[(0.5 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot \left(0.5 + \frac{0.5}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}{\frac{1}{1 + x} \cdot \frac{1}{x}}}\\
\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 42.9%
Taylor expanded in x around inf 82.9%
div-sub82.9%
*-commutative82.9%
associate-/l*82.9%
*-commutative82.9%
associate-/l*82.9%
distribute-rgt-out--82.9%
Simplified82.9%
Taylor expanded in x around -inf 0.0%
associate-*r/0.0%
Simplified99.7%
*-un-lft-identity99.7%
distribute-rgt-out99.7%
associate-/l*99.7%
pow-flip99.7%
sqrt-pow199.7%
metadata-eval99.7%
metadata-eval99.7%
inv-pow99.7%
sqrt-pow199.8%
metadata-eval99.8%
Applied egg-rr99.8%
*-lft-identity99.8%
Simplified99.8%
associate-*r/99.8%
associate-*l/99.7%
distribute-rgt-in99.7%
*-commutative99.7%
div-inv99.7%
associate-*l*99.7%
inv-pow99.7%
metadata-eval99.7%
pow-prod-up99.3%
unpow399.3%
pow-pow100.0%
metadata-eval100.0%
Applied egg-rr100.0%
*-commutative100.0%
distribute-lft-out100.0%
Simplified100.0%
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 56.1%
flip--55.2%
clear-num55.2%
inv-pow55.2%
sqrt-pow255.2%
metadata-eval55.2%
inv-pow55.2%
sqrt-pow255.2%
+-commutative55.2%
metadata-eval55.2%
frac-times56.3%
metadata-eval56.3%
add-sqr-sqrt55.9%
frac-times56.9%
metadata-eval56.9%
add-sqr-sqrt57.9%
+-commutative57.9%
Applied egg-rr57.9%
frac-sub93.0%
*-un-lft-identity93.0%
Applied egg-rr93.0%
*-un-lft-identity93.0%
*-commutative93.0%
times-frac93.2%
+-commutative93.2%
*-rgt-identity93.2%
associate--l+99.2%
+-inverses99.2%
metadata-eval99.2%
Applied egg-rr99.2%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 2e-24) (* (pow x -1.5) (+ 0.5 (/ 0.5 x))) (/ 1.0 (* (+ 1.0 x) (* x (+ (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-24) {
tmp = pow(x, -1.5) * (0.5 + (0.5 / x));
} else {
tmp = 1.0 / ((1.0 + x) * (x * (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-24) then
tmp = (x ** (-1.5d0)) * (0.5d0 + (0.5d0 / x))
else
tmp = 1.0d0 / ((1.0d0 + x) * (x * ((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-24) {
tmp = Math.pow(x, -1.5) * (0.5 + (0.5 / x));
} else {
tmp = 1.0 / ((1.0 + x) * (x * (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-24: tmp = math.pow(x, -1.5) * (0.5 + (0.5 / x)) else: tmp = 1.0 / ((1.0 + x) * (x * (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-24) tmp = Float64((x ^ -1.5) * Float64(0.5 + Float64(0.5 / x))); else tmp = Float64(1.0 / Float64(Float64(1.0 + x) * Float64(x * 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-24) tmp = (x ^ -1.5) * (0.5 + (0.5 / x)); else tmp = 1.0 / ((1.0 + x) * (x * ((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-24], N[(N[Power[x, -1.5], $MachinePrecision] * N[(0.5 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(1.0 + x), $MachinePrecision] * N[(x * N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 2 \cdot 10^{-24}:\\
\;\;\;\;{x}^{-1.5} \cdot \left(0.5 + \frac{0.5}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(1 + x\right) \cdot \left(x \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)\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))))) < 1.99999999999999985e-24Initial program 42.8%
Taylor expanded in x around inf 83.0%
div-sub83.0%
*-commutative83.0%
associate-/l*83.0%
*-commutative83.0%
associate-/l*83.0%
distribute-rgt-out--83.0%
Simplified83.0%
Taylor expanded in x around -inf 0.0%
associate-*r/0.0%
Simplified99.7%
*-un-lft-identity99.7%
distribute-rgt-out99.7%
associate-/l*99.7%
pow-flip99.7%
sqrt-pow199.7%
metadata-eval99.7%
metadata-eval99.7%
inv-pow99.7%
sqrt-pow199.7%
metadata-eval99.7%
Applied egg-rr99.7%
*-lft-identity99.7%
Simplified99.7%
associate-*r/99.8%
associate-*l/99.6%
distribute-rgt-in99.6%
*-commutative99.6%
div-inv99.6%
associate-*l*99.6%
inv-pow99.6%
metadata-eval99.6%
pow-prod-up99.3%
unpow399.3%
pow-pow100.0%
metadata-eval100.0%
Applied egg-rr100.0%
*-commutative100.0%
distribute-lft-out100.0%
Simplified100.0%
if 1.99999999999999985e-24 < (-.f64 (/.f64 #s(literal 1 binary64) (sqrt.f64 x)) (/.f64 #s(literal 1 binary64) (sqrt.f64 (+.f64 x #s(literal 1 binary64))))) Initial program 59.1%
flip--58.1%
clear-num58.1%
inv-pow58.1%
sqrt-pow258.1%
metadata-eval58.1%
inv-pow58.1%
sqrt-pow258.1%
+-commutative58.1%
metadata-eval58.1%
frac-times59.2%
metadata-eval59.2%
add-sqr-sqrt58.8%
frac-times60.0%
metadata-eval60.0%
add-sqr-sqrt61.0%
+-commutative61.0%
Applied egg-rr61.0%
frac-sub98.7%
*-un-lft-identity98.7%
Applied egg-rr98.7%
associate-/r/99.1%
associate-*r*99.0%
flip-+61.1%
*-rgt-identity61.1%
associate--l+61.2%
+-inverses61.2%
metadata-eval61.2%
associate-/l/61.2%
*-un-lft-identity61.2%
flip-+99.3%
+-commutative99.3%
+-commutative99.3%
Applied egg-rr99.3%
Final simplification100.0%
(FPCore (x) :precision binary64 (* (pow x -1.5) (+ 0.5 (/ 0.5 x))))
double code(double x) {
return pow(x, -1.5) * (0.5 + (0.5 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-1.5d0)) * (0.5d0 + (0.5d0 / x))
end function
public static double code(double x) {
return Math.pow(x, -1.5) * (0.5 + (0.5 / x));
}
def code(x): return math.pow(x, -1.5) * (0.5 + (0.5 / x))
function code(x) return Float64((x ^ -1.5) * Float64(0.5 + Float64(0.5 / x))) end
function tmp = code(x) tmp = (x ^ -1.5) * (0.5 + (0.5 / x)); end
code[x_] := N[(N[Power[x, -1.5], $MachinePrecision] * N[(0.5 + N[(0.5 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5} \cdot \left(0.5 + \frac{0.5}{x}\right)
\end{array}
Initial program 43.7%
Taylor expanded in x around inf 82.0%
div-sub82.0%
*-commutative82.0%
associate-/l*82.0%
*-commutative82.0%
associate-/l*81.9%
distribute-rgt-out--81.9%
Simplified81.9%
Taylor expanded in x around -inf 0.0%
associate-*r/0.0%
Simplified97.6%
*-un-lft-identity97.6%
distribute-rgt-out97.6%
associate-/l*97.6%
pow-flip97.6%
sqrt-pow197.6%
metadata-eval97.6%
metadata-eval97.6%
inv-pow97.6%
sqrt-pow197.6%
metadata-eval97.6%
Applied egg-rr97.6%
*-lft-identity97.6%
Simplified97.6%
associate-*r/97.7%
associate-*l/97.5%
distribute-rgt-in97.5%
*-commutative97.5%
div-inv97.5%
associate-*l*97.5%
inv-pow97.5%
metadata-eval97.5%
pow-prod-up97.2%
unpow397.2%
pow-pow97.9%
metadata-eval97.9%
Applied egg-rr97.9%
*-commutative97.9%
distribute-lft-out97.9%
Simplified97.9%
Final simplification97.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(0.5 * Float64(sqrt(Float64(1.0 / x)) / x)) end
function tmp = code(x) tmp = 0.5 * (sqrt((1.0 / x)) / x); end
code[x_] := N[(0.5 * N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \frac{\sqrt{\frac{1}{x}}}{x}
\end{array}
Initial program 43.7%
Taylor expanded in x around inf 82.0%
div-sub82.0%
*-commutative82.0%
associate-/l*82.0%
*-commutative82.0%
associate-/l*81.9%
distribute-rgt-out--81.9%
Simplified81.9%
Taylor expanded in x around -inf 0.0%
associate-*r/0.0%
Simplified97.6%
*-un-lft-identity97.6%
distribute-rgt-out97.6%
associate-/l*97.6%
pow-flip97.6%
sqrt-pow197.6%
metadata-eval97.6%
metadata-eval97.6%
inv-pow97.6%
sqrt-pow197.6%
metadata-eval97.6%
Applied egg-rr97.6%
*-lft-identity97.6%
Simplified97.6%
Taylor expanded in x around inf 97.7%
Final simplification97.7%
(FPCore (x) :precision binary64 (pow x -1.5))
double code(double x) {
return pow(x, -1.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-1.5d0)
end function
public static double code(double x) {
return Math.pow(x, -1.5);
}
def code(x): return math.pow(x, -1.5)
function code(x) return x ^ -1.5 end
function tmp = code(x) tmp = x ^ -1.5; end
code[x_] := N[Power[x, -1.5], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1.5}
\end{array}
Initial program 43.7%
flip--43.6%
clear-num43.6%
inv-pow43.6%
sqrt-pow243.6%
metadata-eval43.6%
inv-pow43.6%
sqrt-pow243.6%
+-commutative43.6%
metadata-eval43.6%
frac-times23.8%
metadata-eval23.8%
add-sqr-sqrt21.6%
frac-times26.0%
metadata-eval26.0%
add-sqr-sqrt43.8%
+-commutative43.8%
Applied egg-rr43.8%
Taylor expanded in x around inf 81.7%
Taylor expanded in x around 0 45.1%
metadata-eval45.1%
cube-div45.2%
Simplified45.2%
sqrt-pow148.8%
inv-pow48.8%
pow-pow48.8%
metadata-eval48.8%
metadata-eval48.8%
*-un-lft-identity48.8%
*-commutative48.8%
Applied egg-rr48.8%
*-rgt-identity48.8%
Simplified48.8%
Final simplification48.8%
(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 2024075
(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)))))