
(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 (/ 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 54.8%
flip--55.1%
div-inv55.1%
add-sqr-sqrt55.1%
add-sqr-sqrt56.0%
associate--l+56.0%
Applied egg-rr56.0%
associate-*r/56.0%
*-rgt-identity56.0%
+-commutative56.0%
associate-+l-99.8%
+-inverses99.8%
metadata-eval99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
(FPCore (x) :precision binary64 (if (<= (- (sqrt (+ 1.0 x)) (sqrt x)) 0.0001) (* 0.5 (pow x -0.5)) (- (+ 1.0 (* x (+ 0.5 (* x -0.125)))) (sqrt x))))
double code(double x) {
double tmp;
if ((sqrt((1.0 + x)) - sqrt(x)) <= 0.0001) {
tmp = 0.5 * pow(x, -0.5);
} else {
tmp = (1.0 + (x * (0.5 + (x * -0.125)))) - sqrt(x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((sqrt((1.0d0 + x)) - sqrt(x)) <= 0.0001d0) then
tmp = 0.5d0 * (x ** (-0.5d0))
else
tmp = (1.0d0 + (x * (0.5d0 + (x * (-0.125d0))))) - sqrt(x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((Math.sqrt((1.0 + x)) - Math.sqrt(x)) <= 0.0001) {
tmp = 0.5 * Math.pow(x, -0.5);
} else {
tmp = (1.0 + (x * (0.5 + (x * -0.125)))) - Math.sqrt(x);
}
return tmp;
}
def code(x): tmp = 0 if (math.sqrt((1.0 + x)) - math.sqrt(x)) <= 0.0001: tmp = 0.5 * math.pow(x, -0.5) else: tmp = (1.0 + (x * (0.5 + (x * -0.125)))) - math.sqrt(x) return tmp
function code(x) tmp = 0.0 if (Float64(sqrt(Float64(1.0 + x)) - sqrt(x)) <= 0.0001) tmp = Float64(0.5 * (x ^ -0.5)); else tmp = Float64(Float64(1.0 + Float64(x * Float64(0.5 + Float64(x * -0.125)))) - sqrt(x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((sqrt((1.0 + x)) - sqrt(x)) <= 0.0001) tmp = 0.5 * (x ^ -0.5); else tmp = (1.0 + (x * (0.5 + (x * -0.125)))) - sqrt(x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], 0.0001], N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 + N[(x * N[(0.5 + N[(x * -0.125), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{1 + x} - \sqrt{x} \leq 0.0001:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\left(1 + x \cdot \left(0.5 + x \cdot -0.125\right)\right) - \sqrt{x}\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 x #s(literal 1 binary64))) (sqrt.f64 x)) < 1.00000000000000005e-4Initial program 5.2%
flip--5.8%
div-inv5.8%
add-sqr-sqrt5.8%
add-sqr-sqrt7.8%
associate--l+7.8%
Applied egg-rr7.8%
associate-*r/7.8%
*-rgt-identity7.8%
+-commutative7.8%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
+-commutative99.6%
Simplified99.6%
Taylor expanded in x around inf 99.1%
unpow-199.1%
metadata-eval99.1%
pow-sqr99.3%
rem-sqrt-square99.3%
rem-square-sqrt98.7%
fabs-sqr98.7%
rem-square-sqrt99.3%
Simplified99.3%
if 1.00000000000000005e-4 < (-.f64 (sqrt.f64 (+.f64 x #s(literal 1 binary64))) (sqrt.f64 x)) Initial program 100.0%
Taylor expanded in x around 0 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= x 1.0) (+ 1.0 (- (* x 0.5) (sqrt x))) (* 0.5 (pow x -0.5))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = 1.0 + ((x * 0.5) - 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 <= 1.0d0) then
tmp = 1.0d0 + ((x * 0.5d0) - 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 <= 1.0) {
tmp = 1.0 + ((x * 0.5) - Math.sqrt(x));
} else {
tmp = 0.5 * Math.pow(x, -0.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.0: tmp = 1.0 + ((x * 0.5) - math.sqrt(x)) else: tmp = 0.5 * math.pow(x, -0.5) return tmp
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(1.0 + Float64(Float64(x * 0.5) - sqrt(x))); else tmp = Float64(0.5 * (x ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.0) tmp = 1.0 + ((x * 0.5) - sqrt(x)); else tmp = 0.5 * (x ^ -0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.0], N[(1.0 + N[(N[(x * 0.5), $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;1 + \left(x \cdot 0.5 - \sqrt{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\end{array}
\end{array}
if x < 1Initial program 100.0%
Taylor expanded in x around 0 99.8%
associate--l+99.8%
*-commutative99.8%
Simplified99.8%
if 1 < x Initial program 5.2%
flip--5.8%
div-inv5.8%
add-sqr-sqrt5.8%
add-sqr-sqrt7.8%
associate--l+7.8%
Applied egg-rr7.8%
associate-*r/7.8%
*-rgt-identity7.8%
+-commutative7.8%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
+-commutative99.6%
Simplified99.6%
Taylor expanded in x around inf 99.1%
unpow-199.1%
metadata-eval99.1%
pow-sqr99.3%
rem-sqrt-square99.3%
rem-square-sqrt98.7%
fabs-sqr98.7%
rem-square-sqrt99.3%
Simplified99.3%
(FPCore (x) :precision binary64 (if (<= x 0.36) (- 1.0 (sqrt x)) (* 0.5 (pow x -0.5))))
double code(double x) {
double tmp;
if (x <= 0.36) {
tmp = 1.0 - 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 <= 0.36d0) then
tmp = 1.0d0 - 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 <= 0.36) {
tmp = 1.0 - Math.sqrt(x);
} else {
tmp = 0.5 * Math.pow(x, -0.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.36: tmp = 1.0 - math.sqrt(x) else: tmp = 0.5 * math.pow(x, -0.5) return tmp
function code(x) tmp = 0.0 if (x <= 0.36) tmp = Float64(1.0 - sqrt(x)); else tmp = Float64(0.5 * (x ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.36) tmp = 1.0 - sqrt(x); else tmp = 0.5 * (x ^ -0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.36], N[(1.0 - 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 0.36:\\
\;\;\;\;1 - \sqrt{x}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\end{array}
\end{array}
if x < 0.35999999999999999Initial program 100.0%
Taylor expanded in x around 0 98.5%
if 0.35999999999999999 < x Initial program 5.2%
flip--5.8%
div-inv5.8%
add-sqr-sqrt5.8%
add-sqr-sqrt7.8%
associate--l+7.8%
Applied egg-rr7.8%
associate-*r/7.8%
*-rgt-identity7.8%
+-commutative7.8%
associate-+l-99.6%
+-inverses99.6%
metadata-eval99.6%
+-commutative99.6%
+-commutative99.6%
Simplified99.6%
Taylor expanded in x around inf 99.1%
unpow-199.1%
metadata-eval99.1%
pow-sqr99.3%
rem-sqrt-square99.3%
rem-square-sqrt98.7%
fabs-sqr98.7%
rem-square-sqrt99.3%
Simplified99.3%
(FPCore (x) :precision binary64 (if (<= x 0.36) (- 1.0 (sqrt x)) (sqrt (/ 0.25 x))))
double code(double x) {
double tmp;
if (x <= 0.36) {
tmp = 1.0 - sqrt(x);
} else {
tmp = sqrt((0.25 / x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.36d0) then
tmp = 1.0d0 - sqrt(x)
else
tmp = sqrt((0.25d0 / x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.36) {
tmp = 1.0 - Math.sqrt(x);
} else {
tmp = Math.sqrt((0.25 / x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.36: tmp = 1.0 - math.sqrt(x) else: tmp = math.sqrt((0.25 / x)) return tmp
function code(x) tmp = 0.0 if (x <= 0.36) tmp = Float64(1.0 - sqrt(x)); else tmp = sqrt(Float64(0.25 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.36) tmp = 1.0 - sqrt(x); else tmp = sqrt((0.25 / x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.36], N[(1.0 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.25 / x), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.36:\\
\;\;\;\;1 - \sqrt{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\frac{0.25}{x}}\\
\end{array}
\end{array}
if x < 0.35999999999999999Initial program 100.0%
Taylor expanded in x around 0 98.5%
if 0.35999999999999999 < x Initial program 5.2%
Taylor expanded in x around inf 99.1%
add-sqr-sqrt98.5%
sqrt-unprod99.1%
*-commutative99.1%
*-commutative99.1%
swap-sqr99.1%
add-sqr-sqrt99.1%
metadata-eval99.1%
Applied egg-rr99.1%
associate-*l/99.1%
metadata-eval99.1%
Simplified99.1%
(FPCore (x) :precision binary64 (if (<= x 1.0) 0.5 (sqrt (/ 0.25 x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = 0.5;
} else {
tmp = sqrt((0.25 / x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.0d0) then
tmp = 0.5d0
else
tmp = sqrt((0.25d0 / x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = 0.5;
} else {
tmp = Math.sqrt((0.25 / x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.0: tmp = 0.5 else: tmp = math.sqrt((0.25 / x)) return tmp
function code(x) tmp = 0.0 if (x <= 1.0) tmp = 0.5; else tmp = sqrt(Float64(0.25 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.0) tmp = 0.5; else tmp = sqrt((0.25 / x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.0], 0.5, N[Sqrt[N[(0.25 / x), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\frac{0.25}{x}}\\
\end{array}
\end{array}
if x < 1Initial program 100.0%
Taylor expanded in x around inf 6.8%
sqrt-div6.8%
metadata-eval6.8%
un-div-inv6.8%
Applied egg-rr6.8%
clear-num6.8%
associate-/r/6.8%
pow1/26.8%
pow-flip6.8%
metadata-eval6.8%
Applied egg-rr6.8%
metadata-eval6.8%
metadata-eval6.8%
pow-pow6.8%
pow1/36.8%
pow-flip6.8%
sqr-pow6.8%
associate-/r*6.8%
Applied egg-rr18.8%
*-inverses18.8%
Simplified18.8%
if 1 < x Initial program 5.2%
Taylor expanded in x around inf 99.1%
add-sqr-sqrt98.5%
sqrt-unprod99.1%
*-commutative99.1%
*-commutative99.1%
swap-sqr99.1%
add-sqr-sqrt99.1%
metadata-eval99.1%
Applied egg-rr99.1%
associate-*l/99.1%
metadata-eval99.1%
Simplified99.1%
Final simplification57.0%
(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 54.8%
Taylor expanded in x around inf 50.8%
sqrt-div50.7%
metadata-eval50.7%
un-div-inv50.7%
Applied egg-rr50.7%
clear-num50.7%
associate-/r/50.7%
pow1/250.7%
pow-flip50.9%
metadata-eval50.9%
Applied egg-rr50.9%
metadata-eval50.9%
metadata-eval50.9%
pow-pow46.8%
pow1/350.3%
pow-flip50.3%
sqr-pow50.3%
associate-/r*50.3%
Applied egg-rr13.1%
*-inverses13.1%
Simplified13.1%
Final simplification13.1%
(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 2024130
(FPCore (x)
:name "Main:bigenough3 from C"
:precision binary64
:alt
(! :herbie-platform default (/ 1 (+ (sqrt (+ x 1)) (sqrt x))))
(- (sqrt (+ x 1.0)) (sqrt x)))