
(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 6 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 (log1p (expm1 (/ 1.0 (+ (sqrt (+ 1.0 x)) (sqrt x))))))
double code(double x) {
return log1p(expm1((1.0 / (sqrt((1.0 + x)) + sqrt(x)))));
}
public static double code(double x) {
return Math.log1p(Math.expm1((1.0 / (Math.sqrt((1.0 + x)) + Math.sqrt(x)))));
}
def code(x): return math.log1p(math.expm1((1.0 / (math.sqrt((1.0 + x)) + math.sqrt(x)))))
function code(x) return log1p(expm1(Float64(1.0 / Float64(sqrt(Float64(1.0 + x)) + sqrt(x))))) end
code[x_] := N[Log[1 + N[(Exp[N[(1.0 / N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{\sqrt{1 + x} + \sqrt{x}}\right)\right)
\end{array}
Initial program 10.1%
flip--12.2%
div-inv12.2%
add-sqr-sqrt13.2%
add-sqr-sqrt15.8%
associate--l+15.8%
Applied egg-rr15.8%
associate-*r/15.8%
*-rgt-identity15.8%
+-commutative15.8%
associate-+l-99.7%
div-sub99.7%
+-inverses99.7%
div099.7%
--rgt-identity99.7%
+-commutative99.7%
Simplified99.7%
log1p-expm1-u99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= x 68000000.0) (- (sqrt (+ 1.0 x)) (sqrt x)) (* 0.5 (pow x -0.5))))
double code(double x) {
double tmp;
if (x <= 68000000.0) {
tmp = sqrt((1.0 + x)) - sqrt(x);
} else {
tmp = 0.5 * pow(x, -0.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 68000000.0d0) then
tmp = sqrt((1.0d0 + x)) - sqrt(x)
else
tmp = 0.5d0 * (x ** (-0.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 68000000.0) {
tmp = Math.sqrt((1.0 + x)) - Math.sqrt(x);
} else {
tmp = 0.5 * Math.pow(x, -0.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 68000000.0: tmp = math.sqrt((1.0 + x)) - math.sqrt(x) else: tmp = 0.5 * math.pow(x, -0.5) return tmp
function code(x) tmp = 0.0 if (x <= 68000000.0) tmp = Float64(sqrt(Float64(1.0 + x)) - sqrt(x)); else tmp = Float64(0.5 * (x ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 68000000.0) tmp = sqrt((1.0 + x)) - sqrt(x); else tmp = 0.5 * (x ^ -0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 68000000.0], N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 68000000:\\
\;\;\;\;\sqrt{1 + x} - \sqrt{x}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\end{array}
\end{array}
if x < 6.8e7Initial program 76.7%
if 6.8e7 < x Initial program 6.0%
flip--7.7%
div-inv7.7%
add-sqr-sqrt8.6%
add-sqr-sqrt10.6%
associate--l+10.6%
Applied egg-rr10.6%
associate-*r/10.6%
*-rgt-identity10.6%
+-commutative10.6%
associate-+l-99.7%
div-sub99.7%
+-inverses99.7%
div099.7%
--rgt-identity99.7%
+-commutative99.7%
Simplified99.7%
Taylor expanded in x around inf 98.7%
rem-exp-log93.0%
exp-neg93.0%
unpow1/293.0%
exp-prod93.0%
distribute-lft-neg-out93.0%
distribute-rgt-neg-in93.0%
metadata-eval93.0%
exp-to-pow98.9%
Simplified98.9%
Final simplification97.6%
(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 10.1%
flip--12.2%
div-inv12.2%
add-sqr-sqrt13.2%
add-sqr-sqrt15.8%
associate--l+15.8%
Applied egg-rr15.8%
associate-*r/15.8%
*-rgt-identity15.8%
+-commutative15.8%
associate-+l-99.7%
div-sub99.7%
+-inverses99.7%
div099.7%
--rgt-identity99.7%
+-commutative99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (* 0.5 (pow x -0.5)))
double code(double x) {
return 0.5 * pow(x, -0.5);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 * (x ** (-0.5d0))
end function
public static double code(double x) {
return 0.5 * Math.pow(x, -0.5);
}
def code(x): return 0.5 * math.pow(x, -0.5)
function code(x) return Float64(0.5 * (x ^ -0.5)) end
function tmp = code(x) tmp = 0.5 * (x ^ -0.5); end
code[x_] := N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot {x}^{-0.5}
\end{array}
Initial program 10.1%
flip--12.2%
div-inv12.2%
add-sqr-sqrt13.2%
add-sqr-sqrt15.8%
associate--l+15.8%
Applied egg-rr15.8%
associate-*r/15.8%
*-rgt-identity15.8%
+-commutative15.8%
associate-+l-99.7%
div-sub99.7%
+-inverses99.7%
div099.7%
--rgt-identity99.7%
+-commutative99.7%
Simplified99.7%
Taylor expanded in x around inf 95.6%
rem-exp-log90.2%
exp-neg90.2%
unpow1/290.2%
exp-prod90.2%
distribute-lft-neg-out90.2%
distribute-rgt-neg-in90.2%
metadata-eval90.2%
exp-to-pow95.8%
Simplified95.8%
Final simplification95.8%
(FPCore (x) :precision binary64 (/ 0.5 (sqrt x)))
double code(double x) {
return 0.5 / sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 / sqrt(x)
end function
public static double code(double x) {
return 0.5 / Math.sqrt(x);
}
def code(x): return 0.5 / math.sqrt(x)
function code(x) return Float64(0.5 / sqrt(x)) end
function tmp = code(x) tmp = 0.5 / sqrt(x); end
code[x_] := N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{\sqrt{x}}
\end{array}
Initial program 10.1%
Taylor expanded in x around inf 98.0%
Taylor expanded in x around inf 95.4%
*-commutative95.4%
Simplified95.4%
add-sqr-sqrt95.3%
times-frac95.5%
Applied egg-rr95.5%
*-inverses95.5%
*-lft-identity95.5%
Simplified95.5%
Final simplification95.5%
(FPCore (x) :precision binary64 (sqrt x))
double code(double x) {
return sqrt(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt(x)
end function
public static double code(double x) {
return Math.sqrt(x);
}
def code(x): return math.sqrt(x)
function code(x) return sqrt(x) end
function tmp = code(x) tmp = sqrt(x); end
code[x_] := N[Sqrt[x], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x}
\end{array}
Initial program 10.1%
Taylor expanded in x around 0 1.6%
sub-neg1.6%
rem-square-sqrt0.0%
fabs-sqr0.0%
rem-square-sqrt7.0%
rem-sqrt-square7.0%
sqr-neg7.0%
rem-square-sqrt7.0%
Simplified7.0%
Taylor expanded in x around inf 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 2024084
(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)))