
(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 4 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) (sqrt (+ 1.0 x)))))
double code(double x) {
return (1.0 / x) / fma(pow(x, -0.5), (1.0 + x), sqrt((1.0 + x)));
}
function code(x) return Float64(Float64(1.0 / x) / fma((x ^ -0.5), Float64(1.0 + x), sqrt(Float64(1.0 + x)))) end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[Power[x, -0.5], $MachinePrecision] * N[(1.0 + x), $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{\mathsf{fma}\left({x}^{-0.5}, 1 + x, \sqrt{1 + x}\right)}
\end{array}
Initial program 39.1%
flip--39.1%
div-inv39.1%
frac-times22.9%
metadata-eval22.9%
add-sqr-sqrt20.6%
frac-times27.0%
metadata-eval27.0%
add-sqr-sqrt39.3%
+-commutative39.3%
inv-pow39.3%
sqrt-pow239.3%
metadata-eval39.3%
pow1/239.3%
pow-flip39.3%
+-commutative39.3%
metadata-eval39.3%
Applied egg-rr39.3%
frac-sub41.2%
associate-/r*41.2%
*-un-lft-identity41.2%
*-rgt-identity41.2%
associate--l+82.9%
Applied egg-rr82.9%
clear-num81.1%
frac-times81.1%
metadata-eval81.1%
div-inv81.1%
+-inverses81.1%
metadata-eval81.1%
clear-num81.1%
/-rgt-identity81.1%
*-commutative81.1%
+-commutative81.1%
+-commutative81.1%
Applied egg-rr81.1%
associate-*l*97.8%
associate-/r*99.5%
distribute-rgt-in99.5%
fma-define99.5%
+-commutative99.5%
pow-plus99.6%
metadata-eval99.6%
unpow1/299.6%
+-commutative99.6%
Simplified99.6%
(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 39.1%
flip--39.1%
div-inv39.1%
frac-times22.9%
metadata-eval22.9%
add-sqr-sqrt20.6%
frac-times27.0%
metadata-eval27.0%
add-sqr-sqrt39.3%
+-commutative39.3%
inv-pow39.3%
sqrt-pow239.3%
metadata-eval39.3%
pow1/239.3%
pow-flip39.3%
+-commutative39.3%
metadata-eval39.3%
Applied egg-rr39.3%
frac-sub41.2%
associate-/r*41.2%
*-un-lft-identity41.2%
*-rgt-identity41.2%
associate--l+82.9%
Applied egg-rr82.9%
clear-num82.9%
frac-times99.5%
*-rgt-identity99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
/-rgt-identity99.5%
+-commutative99.5%
Applied egg-rr99.5%
distribute-rgt-in99.5%
+-commutative99.5%
sqr-pow99.4%
+-commutative99.4%
sqr-pow99.5%
pow-plus99.6%
metadata-eval99.6%
pow1/299.6%
+-commutative99.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (* (sqrt x) 2.0)))
double code(double x) {
return (1.0 / x) / (sqrt(x) * 2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (sqrt(x) * 2.0d0)
end function
public static double code(double x) {
return (1.0 / x) / (Math.sqrt(x) * 2.0);
}
def code(x): return (1.0 / x) / (math.sqrt(x) * 2.0)
function code(x) return Float64(Float64(1.0 / x) / Float64(sqrt(x) * 2.0)) end
function tmp = code(x) tmp = (1.0 / x) / (sqrt(x) * 2.0); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{\sqrt{x} \cdot 2}
\end{array}
Initial program 39.1%
flip--39.1%
div-inv39.1%
frac-times22.9%
metadata-eval22.9%
add-sqr-sqrt20.6%
frac-times27.0%
metadata-eval27.0%
add-sqr-sqrt39.3%
+-commutative39.3%
inv-pow39.3%
sqrt-pow239.3%
metadata-eval39.3%
pow1/239.3%
pow-flip39.3%
+-commutative39.3%
metadata-eval39.3%
Applied egg-rr39.3%
frac-sub41.2%
associate-/r*41.2%
*-un-lft-identity41.2%
*-rgt-identity41.2%
associate--l+82.9%
Applied egg-rr82.9%
clear-num82.9%
frac-times99.5%
*-rgt-identity99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
/-rgt-identity99.5%
+-commutative99.5%
Applied egg-rr99.5%
Taylor expanded in x around inf 97.6%
*-commutative97.6%
Simplified97.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 39.1%
add-sqr-sqrt27.9%
fma-neg7.4%
inv-pow7.4%
sqrt-pow17.4%
metadata-eval7.4%
inv-pow7.4%
sqrt-pow17.4%
metadata-eval7.4%
distribute-neg-frac7.4%
metadata-eval7.4%
+-commutative7.4%
Applied egg-rr7.4%
Taylor expanded in x around inf 36.3%
distribute-rgt1-in36.3%
metadata-eval36.3%
mul0-lft36.3%
Simplified36.3%
(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 2024111
(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)))))