
(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 5 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 x) (hypot 1.0 (sqrt x)))))
double code(double x) {
return 1.0 / (sqrt(x) + hypot(1.0, sqrt(x)));
}
public static double code(double x) {
return 1.0 / (Math.sqrt(x) + Math.hypot(1.0, Math.sqrt(x)));
}
def code(x): return 1.0 / (math.sqrt(x) + math.hypot(1.0, math.sqrt(x)))
function code(x) return Float64(1.0 / Float64(sqrt(x) + hypot(1.0, sqrt(x)))) end
function tmp = code(x) tmp = 1.0 / (sqrt(x) + hypot(1.0, sqrt(x))); end
code[x_] := N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[1.0 ^ 2 + N[Sqrt[x], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x} + \mathsf{hypot}\left(1, \sqrt{x}\right)}
\end{array}
Initial program 6.6%
flip--7.1%
div-inv7.1%
add-sqr-sqrt7.5%
add-sqr-sqrt9.5%
associate--l+9.5%
Applied egg-rr9.5%
associate-*r/9.5%
*-rgt-identity9.5%
+-commutative9.5%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
Simplified99.6%
add-log-exp9.4%
*-un-lft-identity9.4%
log-prod9.4%
metadata-eval9.4%
add-log-exp99.6%
Applied egg-rr99.6%
+-lft-identity99.6%
unpow199.6%
sqr-pow99.6%
hypot-1-def99.6%
metadata-eval99.6%
unpow1/299.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x)))))
double code(double x) {
return 1.0 / (sqrt(x) + sqrt((1.0 + x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (sqrt(x) + sqrt((1.0d0 + x)))
end function
public static double code(double x) {
return 1.0 / (Math.sqrt(x) + Math.sqrt((1.0 + x)));
}
def code(x): return 1.0 / (math.sqrt(x) + math.sqrt((1.0 + x)))
function code(x) return Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) end
function tmp = code(x) tmp = 1.0 / (sqrt(x) + sqrt((1.0 + x))); end
code[x_] := N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{x} + \sqrt{1 + x}}
\end{array}
Initial program 6.6%
flip--7.1%
div-inv7.1%
add-sqr-sqrt7.5%
add-sqr-sqrt9.5%
associate--l+9.5%
Applied egg-rr9.5%
associate-*r/9.5%
*-rgt-identity9.5%
+-commutative9.5%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (* 0.5 (sqrt (/ 1.0 x))))
double code(double x) {
return 0.5 * sqrt((1.0 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * sqrt((1.0d0 / x))
end function
public static double code(double x) {
return 0.5 * Math.sqrt((1.0 / x));
}
def code(x): return 0.5 * math.sqrt((1.0 / x))
function code(x) return Float64(0.5 * sqrt(Float64(1.0 / x))) end
function tmp = code(x) tmp = 0.5 * sqrt((1.0 / x)); end
code[x_] := N[(0.5 * N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{\frac{1}{x}}
\end{array}
Initial program 6.6%
flip--7.1%
div-inv7.1%
add-sqr-sqrt7.5%
add-sqr-sqrt9.5%
associate--l+9.5%
Applied egg-rr9.5%
associate-*r/9.5%
*-rgt-identity9.5%
+-commutative9.5%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
Simplified99.6%
flip3-+68.6%
associate-/r/68.6%
sqrt-pow268.5%
metadata-eval68.5%
sqrt-pow268.4%
metadata-eval68.4%
add-sqr-sqrt68.6%
add-sqr-sqrt68.4%
associate-+r-68.4%
sqrt-unprod47.6%
Applied egg-rr47.6%
Taylor expanded in x around inf 98.0%
Final simplification98.0%
(FPCore (x) :precision binary64 (/ 2.0 x))
double code(double x) {
return 2.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / x
end function
public static double code(double x) {
return 2.0 / x;
}
def code(x): return 2.0 / x
function code(x) return Float64(2.0 / x) end
function tmp = code(x) tmp = 2.0 / x; end
code[x_] := N[(2.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{x}
\end{array}
Initial program 6.6%
flip--7.1%
div-inv7.1%
add-sqr-sqrt7.5%
add-sqr-sqrt9.5%
associate--l+9.5%
Applied egg-rr9.5%
associate-*r/9.5%
*-rgt-identity9.5%
+-commutative9.5%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
Simplified99.6%
Taylor expanded in x around 0 6.9%
Taylor expanded in x around inf 6.9%
Final simplification6.9%
(FPCore (x) :precision binary64 1.0)
double code(double x) {
return 1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0
end function
public static double code(double x) {
return 1.0;
}
def code(x): return 1.0
function code(x) return 1.0 end
function tmp = code(x) tmp = 1.0; end
code[x_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 6.6%
Taylor expanded in x around 0 6.9%
Final simplification6.9%
(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 2024031
(FPCore (x)
:name "2sqrt (example 3.1)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+308))
:herbie-target
(/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x)))
(- (sqrt (+ x 1.0)) (sqrt x)))