
(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)))) 1e-23)
(* 0.5 (pow x -1.5))
(*
(/ (+ x (- 1.0 x)) (* x (+ 1.0 x)))
(exp (- (log (+ (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)))) <= 1e-23) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = ((x + (1.0 - x)) / (x * (1.0 + x))) * exp(-log((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)))) <= 1d-23) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = ((x + (1.0d0 - x)) / (x * (1.0d0 + x))) * exp(-log(((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)))) <= 1e-23) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = ((x + (1.0 - x)) / (x * (1.0 + x))) * Math.exp(-Math.log((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)))) <= 1e-23: tmp = 0.5 * math.pow(x, -1.5) else: tmp = ((x + (1.0 - x)) / (x * (1.0 + x))) * math.exp(-math.log((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)))) <= 1e-23) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64(Float64(Float64(x + Float64(1.0 - x)) / Float64(x * Float64(1.0 + x))) * exp(Float64(-log(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)))) <= 1e-23) tmp = 0.5 * (x ^ -1.5); else tmp = ((x + (1.0 - x)) / (x * (1.0 + x))) * exp(-log(((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], 1e-23], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(N[(N[(x + N[(1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[Log[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 10^{-23}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{x + \left(1 - x\right)}{x \cdot \left(1 + x\right)} \cdot e^{-\log \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\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))))) < 9.9999999999999996e-24Initial program 35.8%
Taylor expanded in x around inf 62.3%
pow162.3%
pow-flip64.6%
sqrt-pow1100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
unpow1100.0%
Simplified100.0%
if 9.9999999999999996e-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 61.7%
add-exp-log58.4%
log-rec58.4%
pow1/258.4%
log-pow58.2%
+-commutative58.2%
log1p-define58.2%
Applied egg-rr58.2%
Applied egg-rr63.8%
frac-sub97.3%
*-un-lft-identity97.3%
Applied egg-rr97.3%
*-rgt-identity97.3%
associate--l+97.3%
Simplified97.3%
Final simplification99.8%
(FPCore (x)
:precision binary64
(if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 0.0)
(* 0.5 (pow x -1.5))
(exp
(- (- (/ (- 0.75) x) (log (* 2.0 (sqrt (/ 1.0 x))))) (* 2.0 (log x))))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = exp((((-0.75 / x) - log((2.0 * sqrt((1.0 / x))))) - (2.0 * log(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 = 0.5d0 * (x ** (-1.5d0))
else
tmp = exp((((-0.75d0 / x) - log((2.0d0 * sqrt((1.0d0 / x))))) - (2.0d0 * log(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 = 0.5 * Math.pow(x, -1.5);
} else {
tmp = Math.exp((((-0.75 / x) - Math.log((2.0 * Math.sqrt((1.0 / x))))) - (2.0 * Math.log(x))));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 0.0: tmp = 0.5 * math.pow(x, -1.5) else: tmp = math.exp((((-0.75 / x) - math.log((2.0 * math.sqrt((1.0 / x))))) - (2.0 * math.log(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(0.5 * (x ^ -1.5)); else tmp = exp(Float64(Float64(Float64(Float64(-0.75) / x) - log(Float64(2.0 * sqrt(Float64(1.0 / x))))) - Float64(2.0 * log(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 = 0.5 * (x ^ -1.5); else tmp = exp((((-0.75 / x) - log((2.0 * sqrt((1.0 / x))))) - (2.0 * log(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[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(N[((-0.75) / x), $MachinePrecision] - N[Log[N[(2.0 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(2.0 * N[Log[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;e^{\left(\frac{-0.75}{x} - \log \left(2 \cdot \sqrt{\frac{1}{x}}\right)\right) - 2 \cdot \log 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 35.9%
Taylor expanded in x around inf 62.1%
pow162.1%
pow-flip64.5%
sqrt-pow1100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
unpow1100.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 59.8%
add-exp-log59.6%
inv-pow59.6%
sqrt-pow259.8%
metadata-eval59.8%
inv-pow59.8%
sqrt-pow260.4%
+-commutative60.4%
metadata-eval60.4%
Applied egg-rr60.4%
Applied egg-rr62.0%
unpow-162.0%
Simplified62.0%
Taylor expanded in x around inf 82.3%
associate--l+82.3%
log-rec82.3%
neg-mul-182.3%
associate-*r*82.3%
metadata-eval82.3%
associate-*r/82.3%
metadata-eval82.3%
Simplified82.3%
Taylor expanded in x around inf 81.6%
associate-*r/81.6%
metadata-eval81.6%
log-rec81.6%
Simplified81.6%
Final simplification98.6%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 0.0) (* 0.5 (pow x -1.5)) (exp (+ (- (* (log x) -2.0) (log (* 2.0 (sqrt (/ 1.0 x))))) (/ -0.75 x)))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 0.0) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = exp((((log(x) * -2.0) - log((2.0 * sqrt((1.0 / x))))) + (-0.75 / 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 = 0.5d0 * (x ** (-1.5d0))
else
tmp = exp((((log(x) * (-2.0d0)) - log((2.0d0 * sqrt((1.0d0 / x))))) + ((-0.75d0) / 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 = 0.5 * Math.pow(x, -1.5);
} else {
tmp = Math.exp((((Math.log(x) * -2.0) - Math.log((2.0 * Math.sqrt((1.0 / x))))) + (-0.75 / x)));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 0.0: tmp = 0.5 * math.pow(x, -1.5) else: tmp = math.exp((((math.log(x) * -2.0) - math.log((2.0 * math.sqrt((1.0 / x))))) + (-0.75 / 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(0.5 * (x ^ -1.5)); else tmp = exp(Float64(Float64(Float64(log(x) * -2.0) - log(Float64(2.0 * sqrt(Float64(1.0 / x))))) + Float64(-0.75 / 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 = 0.5 * (x ^ -1.5); else tmp = exp((((log(x) * -2.0) - log((2.0 * sqrt((1.0 / x))))) + (-0.75 / 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[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(N[(N[Log[x], $MachinePrecision] * -2.0), $MachinePrecision] - N[Log[N[(2.0 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(-0.75 / x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 0:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;e^{\left(\log x \cdot -2 - \log \left(2 \cdot \sqrt{\frac{1}{x}}\right)\right) + \frac{-0.75}{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 35.9%
Taylor expanded in x around inf 62.1%
pow162.1%
pow-flip64.5%
sqrt-pow1100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
unpow1100.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 59.8%
add-exp-log59.6%
inv-pow59.6%
sqrt-pow259.8%
metadata-eval59.8%
inv-pow59.8%
sqrt-pow260.4%
+-commutative60.4%
metadata-eval60.4%
Applied egg-rr60.4%
Applied egg-rr62.0%
unpow-162.0%
Simplified62.0%
Taylor expanded in x around inf 82.3%
associate--l+82.3%
log-rec82.3%
neg-mul-182.3%
associate-*r*82.3%
metadata-eval82.3%
associate-*r/82.3%
metadata-eval82.3%
Simplified82.3%
Taylor expanded in x around inf 81.6%
associate--r+81.5%
associate-*r/81.5%
metadata-eval81.5%
sub-neg81.5%
log-rec81.5%
neg-mul-181.5%
associate-*r*81.5%
metadata-eval81.5%
distribute-neg-frac81.5%
metadata-eval81.5%
Simplified81.5%
Final simplification98.6%
(FPCore (x) :precision binary64 (* 0.5 (pow x -1.5)))
double code(double x) {
return 0.5 * pow(x, -1.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * (x ** (-1.5d0))
end function
public static double code(double x) {
return 0.5 * Math.pow(x, -1.5);
}
def code(x): return 0.5 * math.pow(x, -1.5)
function code(x) return Float64(0.5 * (x ^ -1.5)) end
function tmp = code(x) tmp = 0.5 * (x ^ -1.5); end
code[x_] := N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-1.5}
\end{array}
Initial program 37.6%
Taylor expanded in x around inf 62.0%
pow162.0%
pow-flip64.1%
sqrt-pow197.0%
metadata-eval97.0%
metadata-eval97.0%
Applied egg-rr97.0%
unpow197.0%
Simplified97.0%
Final simplification97.0%
(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.6%
frac-2neg37.6%
metadata-eval37.6%
div-inv37.6%
frac-2neg37.6%
metadata-eval37.6%
div-inv37.6%
distribute-neg-frac237.6%
prod-diff37.6%
distribute-neg-frac37.6%
metadata-eval37.6%
+-commutative37.6%
Applied egg-rr31.2%
Simplified31.2%
Taylor expanded in x around 0 5.7%
unpow1/25.7%
exp-to-pow5.7%
log-rec5.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 2024076
(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)))))