
(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 (/ (- (fma 2.0 x 1.0) (sqrt (* x (+ x 1.0)))) (+ (pow (+ x 1.0) 1.5) (pow x 1.5))))
double code(double x) {
return (fma(2.0, x, 1.0) - sqrt((x * (x + 1.0)))) / (pow((x + 1.0), 1.5) + pow(x, 1.5));
}
function code(x) return Float64(Float64(fma(2.0, x, 1.0) - sqrt(Float64(x * Float64(x + 1.0)))) / Float64((Float64(x + 1.0) ^ 1.5) + (x ^ 1.5))) end
code[x_] := N[(N[(N[(2.0 * x + 1.0), $MachinePrecision] - N[Sqrt[N[(x * N[(x + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(N[Power[N[(x + 1.0), $MachinePrecision], 1.5], $MachinePrecision] + N[Power[x, 1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(2, x, 1\right) - \sqrt{x \cdot \left(x + 1\right)}}{{\left(x + 1\right)}^{1.5} + {x}^{1.5}}
\end{array}
Initial program 13.1%
flip--15.6%
div-inv15.6%
add-sqr-sqrt17.5%
add-sqr-sqrt18.7%
associate--l+18.7%
Applied egg-rr18.7%
associate-*r/18.7%
*-rgt-identity18.7%
+-commutative18.7%
associate-+l-99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Applied egg-rr99.4%
associate-*l/99.6%
*-lft-identity99.6%
+-commutative99.6%
count-299.6%
fma-def99.6%
*-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (let* ((t_0 (- (sqrt (+ x 1.0)) (sqrt x)))) (if (<= t_0 5e-5) (* 0.5 (pow x -0.5)) t_0)))
double code(double x) {
double t_0 = sqrt((x + 1.0)) - sqrt(x);
double tmp;
if (t_0 <= 5e-5) {
tmp = 0.5 * pow(x, -0.5);
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((x + 1.0d0)) - sqrt(x)
if (t_0 <= 5d-5) then
tmp = 0.5d0 * (x ** (-0.5d0))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.sqrt((x + 1.0)) - Math.sqrt(x);
double tmp;
if (t_0 <= 5e-5) {
tmp = 0.5 * Math.pow(x, -0.5);
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = math.sqrt((x + 1.0)) - math.sqrt(x) tmp = 0 if t_0 <= 5e-5: tmp = 0.5 * math.pow(x, -0.5) else: tmp = t_0 return tmp
function code(x) t_0 = Float64(sqrt(Float64(x + 1.0)) - sqrt(x)) tmp = 0.0 if (t_0 <= 5e-5) tmp = Float64(0.5 * (x ^ -0.5)); else tmp = t_0; end return tmp end
function tmp_2 = code(x) t_0 = sqrt((x + 1.0)) - sqrt(x); tmp = 0.0; if (t_0 <= 5e-5) tmp = 0.5 * (x ^ -0.5); else tmp = t_0; end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-5], N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{x + 1} - \sqrt{x}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 x 1)) (sqrt.f64 x)) < 5.00000000000000024e-5Initial program 6.4%
flip--8.6%
div-inv8.6%
add-sqr-sqrt10.0%
add-sqr-sqrt10.8%
associate--l+10.8%
Applied egg-rr10.8%
associate-*r/10.8%
*-rgt-identity10.8%
+-commutative10.8%
associate-+l-99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Applied egg-rr99.4%
associate-*l/99.6%
*-lft-identity99.6%
+-commutative99.6%
count-299.6%
fma-def99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in x around inf 98.2%
expm1-log1p-u98.2%
expm1-udef15.9%
inv-pow15.9%
sqrt-pow115.9%
metadata-eval15.9%
Applied egg-rr15.9%
expm1-def98.3%
expm1-log1p98.3%
Simplified98.3%
if 5.00000000000000024e-5 < (-.f64 (sqrt.f64 (+.f64 x 1)) (sqrt.f64 x)) Initial program 81.0%
Final simplification96.8%
(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}
Initial program 13.1%
flip--15.6%
div-inv15.6%
add-sqr-sqrt17.5%
add-sqr-sqrt18.7%
associate--l+18.7%
Applied egg-rr18.7%
associate-*r/18.7%
*-rgt-identity18.7%
+-commutative18.7%
associate-+l-99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Final simplification99.5%
(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 13.1%
flip--15.6%
div-inv15.6%
add-sqr-sqrt17.5%
add-sqr-sqrt18.7%
associate--l+18.7%
Applied egg-rr18.7%
associate-*r/18.7%
*-rgt-identity18.7%
+-commutative18.7%
associate-+l-99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Applied egg-rr99.4%
associate-*l/99.6%
*-lft-identity99.6%
+-commutative99.6%
count-299.6%
fma-def99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in x around inf 93.1%
expm1-log1p-u93.1%
expm1-udef18.2%
inv-pow18.2%
sqrt-pow118.2%
metadata-eval18.2%
Applied egg-rr18.2%
expm1-def93.3%
expm1-log1p93.3%
Simplified93.3%
Final simplification93.3%
(FPCore (x) :precision binary64 0.5)
double code(double x) {
return 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0
end function
public static double code(double x) {
return 0.5;
}
def code(x): return 0.5
function code(x) return 0.5 end
function tmp = code(x) tmp = 0.5; end
code[x_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 13.1%
flip--15.6%
div-inv15.6%
add-sqr-sqrt17.5%
add-sqr-sqrt18.7%
associate--l+18.7%
Applied egg-rr18.7%
associate-*r/18.7%
*-rgt-identity18.7%
+-commutative18.7%
associate-+l-99.5%
+-inverses99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
flip-+10.5%
associate-/r/10.5%
Applied egg-rr20.3%
Taylor expanded in x around 0 9.5%
Taylor expanded in x around inf 9.5%
Final simplification9.5%
(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 2023333
(FPCore (x)
:name "2sqrt (example 3.1)"
:precision binary64
:pre (and (> x 1.0) (< x 1e+100))
:herbie-target
(/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x)))
(- (sqrt (+ x 1.0)) (sqrt x)))