
(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 7 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) (fma (pow x -0.5) (+ 1.0 x) (pow (+ 1.0 x) 0.5))))
double code(double x) {
return (1.0 / x) / fma(pow(x, -0.5), (1.0 + x), pow((1.0 + x), 0.5));
}
function code(x) return Float64(Float64(1.0 / x) / fma((x ^ -0.5), Float64(1.0 + x), (Float64(1.0 + x) ^ 0.5))) end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] * N[(1.0 + x), $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{\mathsf{fma}\left({x}^{-0.5}, 1 + x, {\left(1 + x\right)}^{0.5}\right)}
\end{array}
Initial program 38.9%
flip--38.9%
frac-times22.7%
metadata-eval22.7%
add-sqr-sqrt19.9%
frac-times26.4%
metadata-eval26.4%
add-sqr-sqrt39.1%
+-commutative39.1%
inv-pow39.1%
sqrt-pow239.1%
metadata-eval39.1%
pow1/239.1%
pow-flip39.1%
+-commutative39.1%
metadata-eval39.1%
Applied egg-rr39.1%
frac-sub41.8%
div-inv41.8%
*-un-lft-identity41.8%
Applied egg-rr41.8%
associate-*r/41.8%
*-rgt-identity41.8%
*-rgt-identity41.8%
associate--l+81.1%
+-inverses81.1%
metadata-eval81.1%
associate-/r*83.0%
Simplified83.0%
div-inv82.9%
associate-/l*99.4%
Applied egg-rr99.4%
associate-/r*99.5%
associate-*r/99.6%
*-rgt-identity99.6%
distribute-lft-in99.5%
*-commutative99.5%
fma-define99.6%
*-commutative99.6%
pow-plus99.7%
metadata-eval99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (+ (* (pow x -0.5) (+ 1.0 x)) (sqrt (+ 1.0 x)))))
double code(double x) {
return (1.0 / x) / ((pow(x, -0.5) * (1.0 + x)) + sqrt((1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (((x ** (-0.5d0)) * (1.0d0 + x)) + sqrt((1.0d0 + x)))
end function
public static double code(double x) {
return (1.0 / x) / ((Math.pow(x, -0.5) * (1.0 + x)) + Math.sqrt((1.0 + x)));
}
def code(x): return (1.0 / x) / ((math.pow(x, -0.5) * (1.0 + x)) + math.sqrt((1.0 + x)))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64((x ^ -0.5) * Float64(1.0 + x)) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = (1.0 / x) / (((x ^ -0.5) * (1.0 + x)) + sqrt((1.0 + x))); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(N[Power[x, -0.5], $MachinePrecision] * N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{{x}^{-0.5} \cdot \left(1 + x\right) + \sqrt{1 + x}}
\end{array}
Initial program 38.9%
flip--38.9%
frac-times22.7%
metadata-eval22.7%
add-sqr-sqrt19.9%
frac-times26.4%
metadata-eval26.4%
add-sqr-sqrt39.1%
+-commutative39.1%
inv-pow39.1%
sqrt-pow239.1%
metadata-eval39.1%
pow1/239.1%
pow-flip39.1%
+-commutative39.1%
metadata-eval39.1%
Applied egg-rr39.1%
frac-sub41.8%
div-inv41.8%
*-un-lft-identity41.8%
Applied egg-rr41.8%
associate-*r/41.8%
*-rgt-identity41.8%
*-rgt-identity41.8%
associate--l+81.1%
+-inverses81.1%
metadata-eval81.1%
associate-/r*83.0%
Simplified83.0%
div-inv82.9%
associate-/l*99.4%
Applied egg-rr99.4%
associate-/r*99.5%
associate-*r/99.6%
*-rgt-identity99.6%
distribute-lft-in99.5%
*-commutative99.5%
fma-define99.6%
*-commutative99.6%
pow-plus99.7%
metadata-eval99.7%
Simplified99.7%
fma-undefine99.6%
unpow1/299.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (+ (* 2.0 (sqrt x)) (* (sqrt (/ 1.0 x)) 1.5))))
double code(double x) {
return (1.0 / x) / ((2.0 * sqrt(x)) + (sqrt((1.0 / x)) * 1.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / ((2.0d0 * sqrt(x)) + (sqrt((1.0d0 / x)) * 1.5d0))
end function
public static double code(double x) {
return (1.0 / x) / ((2.0 * Math.sqrt(x)) + (Math.sqrt((1.0 / x)) * 1.5));
}
def code(x): return (1.0 / x) / ((2.0 * math.sqrt(x)) + (math.sqrt((1.0 / x)) * 1.5))
function code(x) return Float64(Float64(1.0 / x) / Float64(Float64(2.0 * sqrt(x)) + Float64(sqrt(Float64(1.0 / x)) * 1.5))) end
function tmp = code(x) tmp = (1.0 / x) / ((2.0 * sqrt(x)) + (sqrt((1.0 / x)) * 1.5)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[(2.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 1.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{2 \cdot \sqrt{x} + \sqrt{\frac{1}{x}} \cdot 1.5}
\end{array}
Initial program 38.9%
sub-neg38.9%
inv-pow38.9%
sqrt-pow230.0%
metadata-eval30.0%
distribute-neg-frac30.0%
metadata-eval30.0%
+-commutative30.0%
Applied egg-rr30.0%
*-rgt-identity30.0%
metadata-eval30.0%
distribute-rgt-neg-in30.0%
unsub-neg30.0%
associate-*l/30.0%
metadata-eval30.0%
unpow1/230.0%
exp-to-pow7.5%
log1p-undefine7.5%
*-commutative7.5%
exp-neg7.5%
*-commutative7.5%
distribute-rgt-neg-in7.5%
log1p-undefine7.5%
metadata-eval7.5%
exp-to-pow39.0%
Simplified39.0%
flip--39.0%
pow-prod-up20.1%
metadata-eval20.1%
inv-pow20.4%
pow-prod-up38.8%
metadata-eval38.8%
inv-pow39.1%
frac-sub41.8%
*-un-lft-identity41.8%
*-rgt-identity41.8%
associate-/r*41.8%
*-rgt-identity41.8%
*-rgt-identity41.8%
associate--l+81.1%
+-inverses81.1%
metadata-eval81.1%
Applied egg-rr99.6%
Taylor expanded in x around inf 98.9%
+-commutative98.9%
associate-+l+98.9%
distribute-rgt-out98.9%
metadata-eval98.9%
Simplified98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (* (/ 1.0 (pow x 0.5)) (/ 1.0 (+ 2.0 (* x 2.0)))))
double code(double x) {
return (1.0 / pow(x, 0.5)) * (1.0 / (2.0 + (x * 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x ** 0.5d0)) * (1.0d0 / (2.0d0 + (x * 2.0d0)))
end function
public static double code(double x) {
return (1.0 / Math.pow(x, 0.5)) * (1.0 / (2.0 + (x * 2.0)));
}
def code(x): return (1.0 / math.pow(x, 0.5)) * (1.0 / (2.0 + (x * 2.0)))
function code(x) return Float64(Float64(1.0 / (x ^ 0.5)) * Float64(1.0 / Float64(2.0 + Float64(x * 2.0)))) end
function tmp = code(x) tmp = (1.0 / (x ^ 0.5)) * (1.0 / (2.0 + (x * 2.0))); end
code[x_] := N[(N[(1.0 / N[Power[x, 0.5], $MachinePrecision]), $MachinePrecision] * N[(1.0 / N[(2.0 + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{{x}^{0.5}} \cdot \frac{1}{2 + x \cdot 2}
\end{array}
Initial program 38.9%
flip--38.9%
frac-times22.7%
metadata-eval22.7%
add-sqr-sqrt19.9%
frac-times26.4%
metadata-eval26.4%
add-sqr-sqrt39.1%
+-commutative39.1%
inv-pow39.1%
sqrt-pow239.1%
metadata-eval39.1%
pow1/239.1%
pow-flip39.1%
+-commutative39.1%
metadata-eval39.1%
Applied egg-rr39.1%
frac-sub41.8%
div-inv41.8%
*-un-lft-identity41.8%
Applied egg-rr41.8%
associate-*r/41.8%
*-rgt-identity41.8%
*-rgt-identity41.8%
associate--l+81.1%
+-inverses81.1%
metadata-eval81.1%
associate-/r*83.0%
Simplified83.0%
Taylor expanded in x around inf 81.0%
div-inv80.9%
inv-pow80.9%
metadata-eval80.9%
pow-prod-up81.0%
*-commutative81.0%
inv-pow81.0%
sqrt-pow181.0%
metadata-eval81.0%
times-frac97.6%
pow-prod-up97.4%
metadata-eval97.4%
inv-pow97.4%
Applied egg-rr97.4%
associate-/l/97.5%
pow-plus97.6%
metadata-eval97.6%
associate-/l/97.6%
distribute-lft-in97.6%
metadata-eval97.6%
Simplified97.6%
Final simplification97.6%
(FPCore (x) :precision binary64 (if (<= x 4.8e+153) (/ 1.0 (+ x (sqrt x))) 0.0))
double code(double x) {
double tmp;
if (x <= 4.8e+153) {
tmp = 1.0 / (x + sqrt(x));
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.8d+153) then
tmp = 1.0d0 / (x + sqrt(x))
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.8e+153) {
tmp = 1.0 / (x + Math.sqrt(x));
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.8e+153: tmp = 1.0 / (x + math.sqrt(x)) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 4.8e+153) tmp = Float64(1.0 / Float64(x + sqrt(x))); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.8e+153) tmp = 1.0 / (x + sqrt(x)); else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.8e+153], N[(1.0 / N[(x + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.8 \cdot 10^{+153}:\\
\;\;\;\;\frac{1}{x + \sqrt{x}}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 4.79999999999999985e153Initial program 10.8%
flip--10.8%
frac-times11.0%
metadata-eval11.0%
add-sqr-sqrt11.2%
frac-times11.3%
metadata-eval11.3%
add-sqr-sqrt11.3%
+-commutative11.3%
inv-pow11.3%
sqrt-pow211.3%
metadata-eval11.3%
pow1/211.3%
pow-flip11.3%
+-commutative11.3%
metadata-eval11.3%
Applied egg-rr11.3%
frac-sub16.8%
div-inv16.8%
*-un-lft-identity16.8%
Applied egg-rr16.8%
associate-*r/16.8%
*-rgt-identity16.8%
*-rgt-identity16.8%
associate--l+99.2%
+-inverses99.2%
metadata-eval99.2%
associate-/r*99.3%
Simplified99.3%
Taylor expanded in x around 0 8.6%
distribute-rgt-in8.6%
*-lft-identity8.6%
pow-plus8.6%
metadata-eval8.6%
unpow1/28.6%
Simplified8.6%
if 4.79999999999999985e153 < x Initial program 64.5%
Taylor expanded in x around inf 64.5%
Final simplification37.8%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (* 2.0 (sqrt x))))
double code(double x) {
return (1.0 / x) / (2.0 * sqrt(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (2.0d0 * sqrt(x))
end function
public static double code(double x) {
return (1.0 / x) / (2.0 * Math.sqrt(x));
}
def code(x): return (1.0 / x) / (2.0 * math.sqrt(x))
function code(x) return Float64(Float64(1.0 / x) / Float64(2.0 * sqrt(x))) end
function tmp = code(x) tmp = (1.0 / x) / (2.0 * sqrt(x)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(2.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{2 \cdot \sqrt{x}}
\end{array}
Initial program 38.9%
sub-neg38.9%
inv-pow38.9%
sqrt-pow230.0%
metadata-eval30.0%
distribute-neg-frac30.0%
metadata-eval30.0%
+-commutative30.0%
Applied egg-rr30.0%
*-rgt-identity30.0%
metadata-eval30.0%
distribute-rgt-neg-in30.0%
unsub-neg30.0%
associate-*l/30.0%
metadata-eval30.0%
unpow1/230.0%
exp-to-pow7.5%
log1p-undefine7.5%
*-commutative7.5%
exp-neg7.5%
*-commutative7.5%
distribute-rgt-neg-in7.5%
log1p-undefine7.5%
metadata-eval7.5%
exp-to-pow39.0%
Simplified39.0%
flip--39.0%
pow-prod-up20.1%
metadata-eval20.1%
inv-pow20.4%
pow-prod-up38.8%
metadata-eval38.8%
inv-pow39.1%
frac-sub41.8%
*-un-lft-identity41.8%
*-rgt-identity41.8%
associate-/r*41.8%
*-rgt-identity41.8%
*-rgt-identity41.8%
associate--l+81.1%
+-inverses81.1%
metadata-eval81.1%
Applied egg-rr99.6%
Taylor expanded in x around inf 97.6%
Final simplification97.6%
(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 38.9%
Taylor expanded in x around inf 35.8%
Final simplification35.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 2024048
(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)))))