
(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 7 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 (pow (pow (+ (sqrt x) (sqrt (+ 1.0 x))) 2.0) -0.5))
double code(double x) {
return pow(pow((sqrt(x) + sqrt((1.0 + x))), 2.0), -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((sqrt(x) + sqrt((1.0d0 + x))) ** 2.0d0) ** (-0.5d0)
end function
public static double code(double x) {
return Math.pow(Math.pow((Math.sqrt(x) + Math.sqrt((1.0 + x))), 2.0), -0.5);
}
def code(x): return math.pow(math.pow((math.sqrt(x) + math.sqrt((1.0 + x))), 2.0), -0.5)
function code(x) return (Float64(sqrt(x) + sqrt(Float64(1.0 + x))) ^ 2.0) ^ -0.5 end
function tmp = code(x) tmp = ((sqrt(x) + sqrt((1.0 + x))) ^ 2.0) ^ -0.5; end
code[x_] := N[Power[N[Power[N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision], -0.5], $MachinePrecision]
\begin{array}{l}
\\
{\left({\left(\sqrt{x} + \sqrt{1 + x}\right)}^{2}\right)}^{-0.5}
\end{array}
Initial program 6.5%
flip--7.6%
add-sqr-sqrt7.1%
add-sqr-sqrt9.1%
associate--l+9.1%
Applied egg-rr9.1%
Taylor expanded in x around 0 99.6%
add-sqr-sqrt99.1%
sqrt-unprod99.6%
inv-pow99.6%
inv-pow99.6%
pow-prod-up99.5%
+-commutative99.5%
metadata-eval99.5%
Applied egg-rr99.5%
sqrt-pow199.6%
metadata-eval99.6%
metadata-eval99.6%
pow-prod-up99.2%
pow-prod-down99.6%
pow299.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (<= x 4.35e+15) (- (sqrt (+ 1.0 x)) (sqrt x)) (/ 1.0 (sqrt (+ 1.0 (* x 2.0))))))
double code(double x) {
double tmp;
if (x <= 4.35e+15) {
tmp = sqrt((1.0 + x)) - sqrt(x);
} else {
tmp = 1.0 / sqrt((1.0 + (x * 2.0)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.35d+15) then
tmp = sqrt((1.0d0 + x)) - sqrt(x)
else
tmp = 1.0d0 / sqrt((1.0d0 + (x * 2.0d0)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.35e+15) {
tmp = Math.sqrt((1.0 + x)) - Math.sqrt(x);
} else {
tmp = 1.0 / Math.sqrt((1.0 + (x * 2.0)));
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.35e+15: tmp = math.sqrt((1.0 + x)) - math.sqrt(x) else: tmp = 1.0 / math.sqrt((1.0 + (x * 2.0))) return tmp
function code(x) tmp = 0.0 if (x <= 4.35e+15) tmp = Float64(sqrt(Float64(1.0 + x)) - sqrt(x)); else tmp = Float64(1.0 / sqrt(Float64(1.0 + Float64(x * 2.0)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.35e+15) tmp = sqrt((1.0 + x)) - sqrt(x); else tmp = 1.0 / sqrt((1.0 + (x * 2.0))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.35e+15], N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[Sqrt[N[(1.0 + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.35 \cdot 10^{+15}:\\
\;\;\;\;\sqrt{1 + x} - \sqrt{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\sqrt{1 + x \cdot 2}}\\
\end{array}
\end{array}
if x < 4.35e15Initial program 51.4%
if 4.35e15 < x Initial program 3.9%
flip--3.9%
add-sqr-sqrt3.8%
add-sqr-sqrt3.9%
associate--l+3.9%
Applied egg-rr3.9%
frac-2neg3.9%
div-inv3.9%
Applied egg-rr3.9%
associate--r+20.3%
+-inverses20.3%
metadata-eval20.3%
associate-*r/20.3%
metadata-eval20.3%
neg-mul-120.3%
associate-/r*20.3%
metadata-eval20.3%
+-commutative20.3%
+-commutative20.3%
associate-+l+20.3%
count-220.3%
Simplified20.3%
Final simplification22.0%
(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.5%
flip--7.6%
add-sqr-sqrt7.1%
add-sqr-sqrt9.1%
associate--l+9.1%
Applied egg-rr9.1%
Taylor expanded in x around 0 99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ 1.0 (/ (+ x (+ 1.0 x)) (sqrt (* x 2.0)))))
double code(double x) {
return 1.0 / ((x + (1.0 + x)) / sqrt((x * 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / ((x + (1.0d0 + x)) / sqrt((x * 2.0d0)))
end function
public static double code(double x) {
return 1.0 / ((x + (1.0 + x)) / Math.sqrt((x * 2.0)));
}
def code(x): return 1.0 / ((x + (1.0 + x)) / math.sqrt((x * 2.0)))
function code(x) return Float64(1.0 / Float64(Float64(x + Float64(1.0 + x)) / sqrt(Float64(x * 2.0)))) end
function tmp = code(x) tmp = 1.0 / ((x + (1.0 + x)) / sqrt((x * 2.0))); end
code[x_] := N[(1.0 / N[(N[(x + N[(1.0 + x), $MachinePrecision]), $MachinePrecision] / N[Sqrt[N[(x * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{x + \left(1 + x\right)}{\sqrt{x \cdot 2}}}
\end{array}
Initial program 6.5%
flip--7.6%
add-sqr-sqrt7.1%
add-sqr-sqrt9.1%
associate--l+9.1%
Applied egg-rr9.1%
flip-+3.0%
add-sqr-sqrt4.7%
add-sqr-sqrt2.9%
flip--4.0%
add-sqr-sqrt3.6%
add-sqr-sqrt5.5%
associate-+r-5.5%
associate-+r-5.5%
Applied egg-rr4.8%
Taylor expanded in x around inf 4.8%
*-commutative4.8%
Simplified4.8%
Taylor expanded in x around 0 20.3%
Final simplification20.3%
(FPCore (x) :precision binary64 (/ 1.0 (sqrt (+ 1.0 (* x 2.0)))))
double code(double x) {
return 1.0 / sqrt((1.0 + (x * 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / sqrt((1.0d0 + (x * 2.0d0)))
end function
public static double code(double x) {
return 1.0 / Math.sqrt((1.0 + (x * 2.0)));
}
def code(x): return 1.0 / math.sqrt((1.0 + (x * 2.0)))
function code(x) return Float64(1.0 / sqrt(Float64(1.0 + Float64(x * 2.0)))) end
function tmp = code(x) tmp = 1.0 / sqrt((1.0 + (x * 2.0))); end
code[x_] := N[(1.0 / N[Sqrt[N[(1.0 + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\sqrt{1 + x \cdot 2}}
\end{array}
Initial program 6.5%
flip--7.6%
add-sqr-sqrt7.1%
add-sqr-sqrt9.1%
associate--l+9.1%
Applied egg-rr9.1%
frac-2neg9.1%
div-inv9.1%
Applied egg-rr4.8%
associate--r+20.3%
+-inverses20.3%
metadata-eval20.3%
associate-*r/20.3%
metadata-eval20.3%
neg-mul-120.3%
associate-/r*20.3%
metadata-eval20.3%
+-commutative20.3%
+-commutative20.3%
associate-+l+20.3%
count-220.3%
Simplified20.3%
Final simplification20.3%
(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.5%
flip--7.6%
add-sqr-sqrt7.1%
add-sqr-sqrt9.1%
associate--l+9.1%
Applied egg-rr9.1%
Taylor expanded in x around 0 4.4%
Taylor expanded in x around inf 7.1%
Final simplification7.1%
(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.5%
Taylor expanded in x around 0 7.0%
Final simplification7.0%
(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 2024036
(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)))