
(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 (+ x 1.0)))) 0.0)
(* (pow x -1.5) 0.5)
(/
(/ (+ x (- -1.0 x)) (* x (- -1.0 x)))
(+ (pow x -0.5) (pow (+ x 1.0) -0.5)))))
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) + (-1.0 / sqrt((x + 1.0)))) <= 0.0) {
tmp = pow(x, -1.5) * 0.5;
} else {
tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / (pow(x, -0.5) + pow((x + 1.0), -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((x + 1.0d0)))) <= 0.0d0) then
tmp = (x ** (-1.5d0)) * 0.5d0
else
tmp = ((x + ((-1.0d0) - x)) / (x * ((-1.0d0) - x))) / ((x ** (-0.5d0)) + ((x + 1.0d0) ** (-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((x + 1.0)))) <= 0.0) {
tmp = Math.pow(x, -1.5) * 0.5;
} else {
tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / (Math.pow(x, -0.5) + Math.pow((x + 1.0), -0.5));
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((x + 1.0)))) <= 0.0: tmp = math.pow(x, -1.5) * 0.5 else: tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / (math.pow(x, -0.5) + math.pow((x + 1.0), -0.5)) return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(x + 1.0)))) <= 0.0) tmp = Float64((x ^ -1.5) * 0.5); else tmp = Float64(Float64(Float64(x + Float64(-1.0 - x)) / Float64(x * Float64(-1.0 - x))) / Float64((x ^ -0.5) + (Float64(x + 1.0) ^ -0.5))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) + (-1.0 / sqrt((x + 1.0)))) <= 0.0) tmp = (x ^ -1.5) * 0.5; else tmp = ((x + (-1.0 - x)) / (x * (-1.0 - x))) / ((x ^ -0.5) + ((x + 1.0) ^ -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[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(x + N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{x + 1}} \leq 0:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{x + \left(-1 - x\right)}{x \cdot \left(-1 - x\right)}}{{x}^{-0.5} + {\left(x + 1\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))))) < 0.0Initial program 34.7%
add-exp-log5.0%
log-rec5.0%
pow1/25.0%
log-pow5.0%
+-commutative5.0%
log1p-define5.0%
Applied egg-rr5.0%
Taylor expanded in x around inf 4.5%
associate--r+5.7%
Simplified99.2%
Taylor expanded in x around inf 63.2%
exp-to-pow60.7%
*-commutative60.7%
exp-neg63.3%
distribute-lft-neg-in63.3%
metadata-eval63.3%
*-rgt-identity63.3%
*-rgt-identity63.3%
*-commutative63.3%
exp-to-pow65.9%
unpow1/265.9%
exp-to-pow63.3%
*-commutative63.3%
exp-prod93.0%
*-commutative93.0%
associate-*r*93.0%
metadata-eval93.0%
*-rgt-identity93.0%
*-rgt-identity93.0%
*-commutative93.0%
exp-to-pow100.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 60.2%
flip--59.9%
frac-times61.2%
metadata-eval61.2%
add-sqr-sqrt60.8%
frac-times62.1%
metadata-eval62.1%
add-sqr-sqrt62.3%
+-commutative62.3%
inv-pow62.3%
sqrt-pow262.3%
metadata-eval62.3%
inv-pow62.3%
sqrt-pow262.3%
+-commutative62.3%
metadata-eval62.3%
Applied egg-rr62.3%
frac-2neg62.3%
metadata-eval62.3%
frac-sub99.7%
*-un-lft-identity99.7%
distribute-neg-in99.7%
metadata-eval99.7%
distribute-neg-in99.7%
metadata-eval99.7%
Applied egg-rr99.7%
Final simplification100.0%
(FPCore (x) :precision binary64 (- (* 0.3125 (pow x -3.5)) (fma 0.375 (pow x -2.5) (* -0.5 (pow x -1.5)))))
double code(double x) {
return (0.3125 * pow(x, -3.5)) - fma(0.375, pow(x, -2.5), (-0.5 * pow(x, -1.5)));
}
function code(x) return Float64(Float64(0.3125 * (x ^ -3.5)) - fma(0.375, (x ^ -2.5), Float64(-0.5 * (x ^ -1.5)))) end
code[x_] := N[(N[(0.3125 * N[Power[x, -3.5], $MachinePrecision]), $MachinePrecision] - N[(0.375 * N[Power[x, -2.5], $MachinePrecision] + N[(-0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.3125 \cdot {x}^{-3.5} - \mathsf{fma}\left(0.375, {x}^{-2.5}, -0.5 \cdot {x}^{-1.5}\right)
\end{array}
Initial program 35.8%
add-exp-log7.2%
log-rec7.2%
pow1/27.2%
log-pow7.2%
+-commutative7.2%
log1p-define7.2%
Applied egg-rr7.2%
Taylor expanded in x around inf 6.3%
associate--r+7.4%
Simplified98.6%
sub-neg98.6%
+-rgt-identity98.6%
pow-div98.6%
metadata-eval98.6%
pow-pow99.4%
metadata-eval99.4%
associate-/l*99.4%
pow-div99.4%
metadata-eval99.4%
Applied egg-rr99.4%
sub-neg99.4%
fma-define99.4%
+-commutative99.4%
fma-define99.4%
Simplified99.4%
(FPCore (x) :precision binary64 (/ (fma -0.375 (pow x -1.5) (* 0.5 (sqrt (/ 1.0 x)))) x))
double code(double x) {
return fma(-0.375, pow(x, -1.5), (0.5 * sqrt((1.0 / x)))) / x;
}
function code(x) return Float64(fma(-0.375, (x ^ -1.5), Float64(0.5 * sqrt(Float64(1.0 / x)))) / x) end
code[x_] := N[(N[(-0.375 * N[Power[x, -1.5], $MachinePrecision] + N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-0.375, {x}^{-1.5}, 0.5 \cdot \sqrt{\frac{1}{x}}\right)}{x}
\end{array}
Initial program 35.8%
add-exp-log7.2%
log-rec7.2%
pow1/27.2%
log-pow7.2%
+-commutative7.2%
log1p-define7.2%
Applied egg-rr7.2%
Taylor expanded in x around inf 6.3%
associate--r+7.4%
Simplified98.6%
Taylor expanded in x around inf 98.8%
Simplified98.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 35.8%
add-exp-log7.2%
log-rec7.2%
pow1/27.2%
log-pow7.2%
+-commutative7.2%
log1p-define7.2%
Applied egg-rr7.2%
Taylor expanded in x around inf 6.3%
associate--r+7.4%
Simplified98.6%
Taylor expanded in x around inf 63.1%
exp-to-pow60.6%
*-commutative60.6%
exp-neg63.1%
distribute-lft-neg-in63.1%
metadata-eval63.1%
*-rgt-identity63.1%
*-rgt-identity63.1%
*-commutative63.1%
exp-to-pow65.6%
unpow1/265.6%
exp-to-pow63.1%
*-commutative63.1%
exp-prod91.6%
*-commutative91.6%
associate-*r*91.6%
metadata-eval91.6%
*-rgt-identity91.6%
*-rgt-identity91.6%
*-commutative91.6%
exp-to-pow98.2%
Simplified98.2%
Final simplification98.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 35.8%
add-exp-log7.2%
log-rec7.2%
pow1/27.2%
log-pow7.2%
+-commutative7.2%
log1p-define7.2%
Applied egg-rr7.2%
Taylor expanded in x around inf 4.6%
distribute-lft-neg-in4.6%
metadata-eval4.6%
*-commutative4.6%
exp-to-pow33.0%
unpow1/233.4%
+-inverses33.4%
Simplified33.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 2024150
(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))))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))