
(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
(let* ((t_0 (sqrt (/ 1.0 x))))
(/
(+
(- (fma (sqrt (/ 1.0 (pow x 5.0))) -0.25 (* 0.5 t_0)) (/ (* t_0 0.375) x))
(* 0.5 (log (exp (pow x -2.5)))))
x)))
double code(double x) {
double t_0 = sqrt((1.0 / x));
return ((fma(sqrt((1.0 / pow(x, 5.0))), -0.25, (0.5 * t_0)) - ((t_0 * 0.375) / x)) + (0.5 * log(exp(pow(x, -2.5))))) / x;
}
function code(x) t_0 = sqrt(Float64(1.0 / x)) return Float64(Float64(Float64(fma(sqrt(Float64(1.0 / (x ^ 5.0))), -0.25, Float64(0.5 * t_0)) - Float64(Float64(t_0 * 0.375) / x)) + Float64(0.5 * log(exp((x ^ -2.5))))) / x) end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(N[Sqrt[N[(1.0 / N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -0.25 + N[(0.5 * t$95$0), $MachinePrecision]), $MachinePrecision] - N[(N[(t$95$0 * 0.375), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[Log[N[Exp[N[Power[x, -2.5], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{x}}\\
\frac{\left(\mathsf{fma}\left(\sqrt{\frac{1}{{x}^{5}}}, -0.25, 0.5 \cdot t\_0\right) - \frac{t\_0 \cdot 0.375}{x}\right) + 0.5 \cdot \log \left(e^{{x}^{-2.5}}\right)}{x}
\end{array}
\end{array}
Initial program 37.7%
Taylor expanded in x around inf 85.3%
*-un-lft-identity85.3%
pow-flip85.3%
sqrt-pow185.3%
metadata-eval85.3%
metadata-eval85.3%
Applied egg-rr85.3%
*-lft-identity85.3%
Simplified85.3%
Taylor expanded in x around inf 98.8%
Simplified98.8%
*-rgt-identity98.8%
add-log-exp98.8%
*-rgt-identity98.8%
pow-flip98.8%
sqrt-pow198.8%
metadata-eval98.8%
metadata-eval98.8%
Applied egg-rr98.8%
Final simplification98.8%
(FPCore (x)
:precision binary64
(let* ((t_0 (sqrt (/ 1.0 x))))
(/
(+
(- (fma (pow x -2.5) -0.25 (* 0.5 t_0)) (/ (* t_0 0.375) x))
(* (sqrt (/ 1.0 (pow x 5.0))) 0.5))
x)))
double code(double x) {
double t_0 = sqrt((1.0 / x));
return ((fma(pow(x, -2.5), -0.25, (0.5 * t_0)) - ((t_0 * 0.375) / x)) + (sqrt((1.0 / pow(x, 5.0))) * 0.5)) / x;
}
function code(x) t_0 = sqrt(Float64(1.0 / x)) return Float64(Float64(Float64(fma((x ^ -2.5), -0.25, Float64(0.5 * t_0)) - Float64(Float64(t_0 * 0.375) / x)) + Float64(sqrt(Float64(1.0 / (x ^ 5.0))) * 0.5)) / x) end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(N[(N[Power[x, -2.5], $MachinePrecision] * -0.25 + N[(0.5 * t$95$0), $MachinePrecision]), $MachinePrecision] - N[(N[(t$95$0 * 0.375), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] + N[(N[Sqrt[N[(1.0 / N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{x}}\\
\frac{\left(\mathsf{fma}\left({x}^{-2.5}, -0.25, 0.5 \cdot t\_0\right) - \frac{t\_0 \cdot 0.375}{x}\right) + \sqrt{\frac{1}{{x}^{5}}} \cdot 0.5}{x}
\end{array}
\end{array}
Initial program 37.7%
Taylor expanded in x around inf 85.3%
*-un-lft-identity85.3%
pow-flip85.3%
sqrt-pow185.3%
metadata-eval85.3%
metadata-eval85.3%
Applied egg-rr85.3%
*-lft-identity85.3%
Simplified85.3%
Taylor expanded in x around inf 98.8%
Simplified98.8%
*-un-lft-identity85.3%
pow-flip85.3%
sqrt-pow185.3%
metadata-eval85.3%
metadata-eval85.3%
Applied egg-rr98.8%
*-lft-identity85.3%
Simplified98.8%
(FPCore (x) :precision binary64 (let* ((t_0 (sqrt (/ 1.0 x)))) (/ (- (* 0.5 t_0) (/ (* t_0 0.375) x)) x)))
double code(double x) {
double t_0 = sqrt((1.0 / x));
return ((0.5 * t_0) - ((t_0 * 0.375) / x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = sqrt((1.0d0 / x))
code = ((0.5d0 * t_0) - ((t_0 * 0.375d0) / x)) / x
end function
public static double code(double x) {
double t_0 = Math.sqrt((1.0 / x));
return ((0.5 * t_0) - ((t_0 * 0.375) / x)) / x;
}
def code(x): t_0 = math.sqrt((1.0 / x)) return ((0.5 * t_0) - ((t_0 * 0.375) / x)) / x
function code(x) t_0 = sqrt(Float64(1.0 / x)) return Float64(Float64(Float64(0.5 * t_0) - Float64(Float64(t_0 * 0.375) / x)) / x) end
function tmp = code(x) t_0 = sqrt((1.0 / x)); tmp = ((0.5 * t_0) - ((t_0 * 0.375) / x)) / x; end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(0.5 * t$95$0), $MachinePrecision] - N[(N[(t$95$0 * 0.375), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{1}{x}}\\
\frac{0.5 \cdot t\_0 - \frac{t\_0 \cdot 0.375}{x}}{x}
\end{array}
\end{array}
Initial program 37.7%
Taylor expanded in x around inf 85.3%
*-un-lft-identity85.3%
pow-flip85.3%
sqrt-pow185.3%
metadata-eval85.3%
metadata-eval85.3%
Applied egg-rr85.3%
*-lft-identity85.3%
Simplified85.3%
Taylor expanded in x around inf 98.7%
+-commutative98.7%
mul-1-neg98.7%
unsub-neg98.7%
distribute-rgt-out98.7%
metadata-eval98.7%
Simplified98.7%
(FPCore (x) :precision binary64 (* -0.5 (- (pow x -2.5) (pow x -1.5))))
double code(double x) {
return -0.5 * (pow(x, -2.5) - pow(x, -1.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-0.5d0) * ((x ** (-2.5d0)) - (x ** (-1.5d0)))
end function
public static double code(double x) {
return -0.5 * (Math.pow(x, -2.5) - Math.pow(x, -1.5));
}
def code(x): return -0.5 * (math.pow(x, -2.5) - math.pow(x, -1.5))
function code(x) return Float64(-0.5 * Float64((x ^ -2.5) - (x ^ -1.5))) end
function tmp = code(x) tmp = -0.5 * ((x ^ -2.5) - (x ^ -1.5)); end
code[x_] := N[(-0.5 * N[(N[Power[x, -2.5], $MachinePrecision] - N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \left({x}^{-2.5} - {x}^{-1.5}\right)
\end{array}
Initial program 37.7%
add-sqr-sqrt20.3%
sqrt-unprod37.7%
frac-times33.4%
metadata-eval33.4%
add-sqr-sqrt29.4%
+-commutative29.4%
Applied egg-rr29.4%
Taylor expanded in x around inf 84.4%
distribute-lft-out--84.4%
associate-/l*84.4%
unpow1/284.4%
rem-exp-log84.4%
exp-neg84.4%
exp-prod84.4%
distribute-lft-neg-out84.4%
distribute-rgt-neg-in84.4%
metadata-eval84.4%
exp-to-pow84.4%
Simplified84.4%
div-sub84.4%
pow-div84.4%
metadata-eval84.4%
pow1/284.4%
pow-div98.1%
metadata-eval98.1%
Applied egg-rr98.1%
(FPCore (x) :precision binary64 (* 0.5 (sqrt (/ 1.0 (pow x 3.0)))))
double code(double x) {
return 0.5 * sqrt((1.0 / pow(x, 3.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * sqrt((1.0d0 / (x ** 3.0d0)))
end function
public static double code(double x) {
return 0.5 * Math.sqrt((1.0 / Math.pow(x, 3.0)));
}
def code(x): return 0.5 * math.sqrt((1.0 / math.pow(x, 3.0)))
function code(x) return Float64(0.5 * sqrt(Float64(1.0 / (x ^ 3.0)))) end
function tmp = code(x) tmp = 0.5 * sqrt((1.0 / (x ^ 3.0))); end
code[x_] := N[(0.5 * N[Sqrt[N[(1.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{\frac{1}{{x}^{3}}}
\end{array}
Initial program 37.7%
Taylor expanded in x around inf 67.3%
(FPCore (x) :precision binary64 (cbrt (pow x -1.5)))
double code(double x) {
return cbrt(pow(x, -1.5));
}
public static double code(double x) {
return Math.cbrt(Math.pow(x, -1.5));
}
function code(x) return cbrt((x ^ -1.5)) end
code[x_] := N[Power[N[Power[x, -1.5], $MachinePrecision], 1/3], $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{{x}^{-1.5}}
\end{array}
Initial program 37.7%
Taylor expanded in x around 0 5.8%
add-cbrt-cube34.9%
add-sqr-sqrt34.9%
sqrt-div34.9%
metadata-eval34.9%
frac-times35.6%
metadata-eval35.6%
add-sqr-sqrt35.6%
sqrt-unprod36.3%
sqrt-prod36.3%
unpow336.3%
metadata-eval36.3%
sqrt-div36.3%
pow1/336.3%
pow-flip36.3%
sqrt-pow134.9%
metadata-eval34.9%
metadata-eval34.9%
Applied egg-rr34.9%
unpow1/334.9%
Simplified34.9%
(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 37.7%
sub-neg37.7%
+-commutative37.7%
add-cube-cbrt12.3%
distribute-lft-neg-in12.3%
fma-define7.8%
Applied egg-rr7.9%
Taylor expanded in x around inf 34.8%
distribute-rgt1-in34.8%
metadata-eval34.8%
mul0-lft34.8%
Simplified34.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 2024108
(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)))))