
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
double code(double x) {
return sqrt((x + 1.0)) - sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((x + 1.0d0)) - sqrt(x)
end function
public static double code(double x) {
return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
def code(x): return math.sqrt((x + 1.0)) - math.sqrt(x)
function code(x) return Float64(sqrt(Float64(x + 1.0)) - sqrt(x)) end
function tmp = code(x) tmp = sqrt((x + 1.0)) - sqrt(x); end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x + 1} - \sqrt{x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
double code(double x) {
return sqrt((x + 1.0)) - sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((x + 1.0d0)) - sqrt(x)
end function
public static double code(double x) {
return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
def code(x): return math.sqrt((x + 1.0)) - math.sqrt(x)
function code(x) return Float64(sqrt(Float64(x + 1.0)) - sqrt(x)) end
function tmp = code(x) tmp = sqrt((x + 1.0)) - sqrt(x); end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x + 1} - \sqrt{x}
\end{array}
(FPCore (x) :precision binary64 (/ 1.0 (+ (sqrt (+ 1.0 x)) (sqrt x))))
double code(double x) {
return 1.0 / (sqrt((1.0 + x)) + sqrt(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (sqrt((1.0d0 + x)) + sqrt(x))
end function
public static double code(double x) {
return 1.0 / (Math.sqrt((1.0 + x)) + Math.sqrt(x));
}
def code(x): return 1.0 / (math.sqrt((1.0 + x)) + math.sqrt(x))
function code(x) return Float64(1.0 / Float64(sqrt(Float64(1.0 + x)) + sqrt(x))) end
function tmp = code(x) tmp = 1.0 / (sqrt((1.0 + x)) + sqrt(x)); end
code[x_] := N[(1.0 / N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{1 + x} + \sqrt{x}}
\end{array}
Initial program 7.6%
flip--8.4%
div-inv8.4%
add-sqr-sqrt9.0%
add-sqr-sqrt11.0%
associate--l+11.0%
Applied egg-rr11.0%
associate-*r/11.0%
*-rgt-identity11.0%
+-commutative11.0%
associate-+l-99.6%
div-sub99.6%
+-inverses99.6%
div099.6%
--rgt-identity99.6%
+-commutative99.6%
Simplified99.6%
(FPCore (x) :precision binary64 (* (pow x -0.5) 0.5))
double code(double x) {
return pow(x, -0.5) * 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.5d0)) * 0.5d0
end function
public static double code(double x) {
return Math.pow(x, -0.5) * 0.5;
}
def code(x): return math.pow(x, -0.5) * 0.5
function code(x) return Float64((x ^ -0.5) * 0.5) end
function tmp = code(x) tmp = (x ^ -0.5) * 0.5; end
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.5} \cdot 0.5
\end{array}
Initial program 7.6%
flip--8.4%
div-inv8.4%
add-sqr-sqrt9.0%
add-sqr-sqrt11.0%
associate--l+11.0%
Applied egg-rr11.0%
associate-*r/11.0%
*-rgt-identity11.0%
+-commutative11.0%
associate-+l-99.6%
div-sub99.6%
+-inverses99.6%
div099.6%
--rgt-identity99.6%
+-commutative99.6%
Simplified99.6%
Taylor expanded in x around inf 97.6%
*-commutative97.6%
unpow1/297.6%
rem-exp-log90.5%
exp-neg90.5%
exp-prod90.5%
distribute-lft-neg-out90.5%
distribute-rgt-neg-in90.5%
metadata-eval90.5%
exp-to-pow97.8%
Simplified97.8%
(FPCore (x) :precision binary64 (sqrt (* x 0.140625)))
double code(double x) {
return sqrt((x * 0.140625));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((x * 0.140625d0))
end function
public static double code(double x) {
return Math.sqrt((x * 0.140625));
}
def code(x): return math.sqrt((x * 0.140625))
function code(x) return sqrt(Float64(x * 0.140625)) end
function tmp = code(x) tmp = sqrt((x * 0.140625)); end
code[x_] := N[Sqrt[N[(x * 0.140625), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot 0.140625}
\end{array}
Initial program 7.6%
Taylor expanded in x around inf 99.1%
*-un-lft-identity99.1%
inv-pow99.1%
sqrt-pow199.1%
metadata-eval99.1%
Applied egg-rr99.1%
*-lft-identity99.1%
Simplified99.1%
*-un-lft-identity99.1%
add-sqr-sqrt99.0%
times-frac99.1%
pow1/299.1%
pow-to-exp92.1%
exp-neg92.1%
distribute-lft-neg-out92.1%
exp-prod92.1%
unpow1/292.1%
add-sqr-sqrt0.0%
sqrt-unprod5.4%
sqr-neg5.4%
sqrt-unprod5.4%
add-sqr-sqrt5.4%
add-exp-log5.4%
Applied egg-rr5.4%
associate-*r/5.4%
*-rgt-identity5.4%
times-frac5.4%
*-inverses5.4%
associate-*r/5.4%
*-lft-identity5.4%
/-rgt-identity5.4%
fma-undefine5.4%
distribute-rgt-out5.4%
metadata-eval5.4%
Simplified5.4%
add-sqr-sqrt5.4%
sqrt-unprod5.4%
swap-sqr5.4%
add-sqr-sqrt5.4%
metadata-eval5.4%
Applied egg-rr5.4%
(FPCore (x) :precision binary64 (/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x))))
double code(double x) {
return 1.0 / (sqrt((x + 1.0)) + sqrt(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (sqrt((x + 1.0d0)) + sqrt(x))
end function
public static double code(double x) {
return 1.0 / (Math.sqrt((x + 1.0)) + Math.sqrt(x));
}
def code(x): return 1.0 / (math.sqrt((x + 1.0)) + math.sqrt(x))
function code(x) return Float64(1.0 / Float64(sqrt(Float64(x + 1.0)) + sqrt(x))) end
function tmp = code(x) tmp = 1.0 / (sqrt((x + 1.0)) + sqrt(x)); end
code[x_] := N[(1.0 / N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x + 1} + \sqrt{x}}
\end{array}
herbie shell --seed 2024089
(FPCore (x)
:name "2sqrt (example 3.1)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:alt
(/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x)))
(- (sqrt (+ x 1.0)) (sqrt x)))