
(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 (/ 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 53.0%
flip--53.4%
div-inv53.4%
add-sqr-sqrt53.3%
add-sqr-sqrt53.8%
associate--l+53.8%
Applied egg-rr53.8%
associate-*r/53.8%
*-rgt-identity53.8%
+-commutative53.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 (let* ((t_0 (- (sqrt (+ 1.0 x)) (sqrt x)))) (if (<= t_0 4e-5) (* 0.5 (pow x -0.5)) t_0)))
double code(double x) {
double t_0 = sqrt((1.0 + x)) - sqrt(x);
double tmp;
if (t_0 <= 4e-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((1.0d0 + x)) - sqrt(x)
if (t_0 <= 4d-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((1.0 + x)) - Math.sqrt(x);
double tmp;
if (t_0 <= 4e-5) {
tmp = 0.5 * Math.pow(x, -0.5);
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = math.sqrt((1.0 + x)) - math.sqrt(x) tmp = 0 if t_0 <= 4e-5: tmp = 0.5 * math.pow(x, -0.5) else: tmp = t_0 return tmp
function code(x) t_0 = Float64(sqrt(Float64(1.0 + x)) - sqrt(x)) tmp = 0.0 if (t_0 <= 4e-5) tmp = Float64(0.5 * (x ^ -0.5)); else tmp = t_0; end return tmp end
function tmp_2 = code(x) t_0 = sqrt((1.0 + x)) - sqrt(x); tmp = 0.0; if (t_0 <= 4e-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[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 4e-5], N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{1 + x} - \sqrt{x}\\
\mathbf{if}\;t\_0 \leq 4 \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)) < 4.00000000000000033e-5Initial program 4.9%
flip--5.7%
div-inv5.7%
add-sqr-sqrt5.3%
add-sqr-sqrt6.2%
associate--l+6.2%
Applied egg-rr6.2%
associate-*r/6.2%
*-rgt-identity6.2%
+-commutative6.2%
associate-+l-99.6%
div-sub99.6%
+-inverses99.6%
div099.6%
--rgt-identity99.6%
+-commutative99.6%
Simplified99.6%
flip3-+62.4%
associate-/r/62.4%
sqrt-pow262.3%
metadata-eval62.3%
sqrt-pow262.2%
metadata-eval62.2%
add-sqr-sqrt62.5%
add-sqr-sqrt62.2%
associate-+r-62.2%
sqrt-unprod43.6%
Applied egg-rr43.6%
associate-*l/43.6%
*-lft-identity43.6%
associate-+l+43.6%
associate--l+43.6%
count-243.6%
*-commutative43.6%
Simplified43.6%
Taylor expanded in x around inf 62.5%
Taylor expanded in x around inf 99.4%
rem-exp-log91.8%
exp-neg91.8%
unpow1/291.8%
exp-prod91.8%
distribute-lft-neg-out91.8%
distribute-rgt-neg-in91.8%
metadata-eval91.8%
exp-to-pow99.6%
Simplified99.6%
if 4.00000000000000033e-5 < (-.f64 (sqrt.f64 (+.f64 x 1)) (sqrt.f64 x)) Initial program 99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= x 0.385) (/ 1.0 (+ 1.0 (pow x 1.5))) (* 0.5 (pow x -0.5))))
double code(double x) {
double tmp;
if (x <= 0.385) {
tmp = 1.0 / (1.0 + pow(x, 1.5));
} 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.385d0) then
tmp = 1.0d0 / (1.0d0 + (x ** 1.5d0))
else
tmp = 0.5d0 * (x ** (-0.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.385) {
tmp = 1.0 / (1.0 + Math.pow(x, 1.5));
} else {
tmp = 0.5 * Math.pow(x, -0.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.385: tmp = 1.0 / (1.0 + math.pow(x, 1.5)) else: tmp = 0.5 * math.pow(x, -0.5) return tmp
function code(x) tmp = 0.0 if (x <= 0.385) tmp = Float64(1.0 / Float64(1.0 + (x ^ 1.5))); else tmp = Float64(0.5 * (x ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.385) tmp = 1.0 / (1.0 + (x ^ 1.5)); else tmp = 0.5 * (x ^ -0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.385], N[(1.0 / N[(1.0 + N[Power[x, 1.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.385:\\
\;\;\;\;\frac{1}{1 + {x}^{1.5}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\end{array}
\end{array}
if x < 0.38500000000000001Initial program 100.0%
flip--99.9%
div-inv99.9%
add-sqr-sqrt99.9%
add-sqr-sqrt99.9%
associate--l+99.9%
Applied egg-rr99.9%
associate-*r/99.9%
*-rgt-identity99.9%
+-commutative99.9%
associate-+l-99.9%
div-sub99.9%
+-inverses99.9%
div099.9%
--rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
flip3-+99.9%
associate-/r/99.9%
sqrt-pow2100.0%
metadata-eval100.0%
sqrt-pow2100.0%
metadata-eval100.0%
add-sqr-sqrt100.0%
add-sqr-sqrt100.0%
associate-+r-100.0%
sqrt-unprod100.0%
Applied egg-rr100.0%
associate-*l/100.0%
*-lft-identity100.0%
associate-+l+99.9%
associate--l+99.9%
count-299.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in x around 0 95.7%
if 0.38500000000000001 < x Initial program 6.1%
flip--6.9%
div-inv6.9%
add-sqr-sqrt6.8%
add-sqr-sqrt7.7%
associate--l+7.7%
Applied egg-rr7.7%
associate-*r/7.7%
*-rgt-identity7.7%
+-commutative7.7%
associate-+l-99.6%
div-sub99.6%
+-inverses99.6%
div099.6%
--rgt-identity99.6%
+-commutative99.6%
Simplified99.6%
flip3-+63.0%
associate-/r/63.0%
sqrt-pow262.9%
metadata-eval62.9%
sqrt-pow262.8%
metadata-eval62.8%
add-sqr-sqrt63.1%
add-sqr-sqrt62.8%
associate-+r-62.8%
sqrt-unprod44.5%
Applied egg-rr44.5%
associate-*l/44.4%
*-lft-identity44.4%
associate-+l+44.4%
associate--l+44.4%
count-244.4%
*-commutative44.4%
Simplified44.4%
Taylor expanded in x around inf 62.4%
Taylor expanded in x around inf 98.4%
rem-exp-log91.0%
exp-neg91.0%
unpow1/291.0%
exp-prod91.0%
distribute-lft-neg-out91.0%
distribute-rgt-neg-in91.0%
metadata-eval91.0%
exp-to-pow98.6%
Simplified98.6%
Final simplification97.2%
(FPCore (x) :precision binary64 (if (<= x 0.25) 1.0 (* 0.5 (pow x -0.5))))
double code(double x) {
double tmp;
if (x <= 0.25) {
tmp = 1.0;
} 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.25d0) then
tmp = 1.0d0
else
tmp = 0.5d0 * (x ** (-0.5d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.25) {
tmp = 1.0;
} else {
tmp = 0.5 * Math.pow(x, -0.5);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.25: tmp = 1.0 else: tmp = 0.5 * math.pow(x, -0.5) return tmp
function code(x) tmp = 0.0 if (x <= 0.25) tmp = 1.0; else tmp = Float64(0.5 * (x ^ -0.5)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.25) tmp = 1.0; else tmp = 0.5 * (x ^ -0.5); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.25], 1.0, N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.25:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-0.5}\\
\end{array}
\end{array}
if x < 0.25Initial program 100.0%
Taylor expanded in x around 0 95.7%
if 0.25 < x Initial program 6.1%
flip--6.9%
div-inv6.9%
add-sqr-sqrt6.8%
add-sqr-sqrt7.7%
associate--l+7.7%
Applied egg-rr7.7%
associate-*r/7.7%
*-rgt-identity7.7%
+-commutative7.7%
associate-+l-99.6%
div-sub99.6%
+-inverses99.6%
div099.6%
--rgt-identity99.6%
+-commutative99.6%
Simplified99.6%
flip3-+63.0%
associate-/r/63.0%
sqrt-pow262.9%
metadata-eval62.9%
sqrt-pow262.8%
metadata-eval62.8%
add-sqr-sqrt63.1%
add-sqr-sqrt62.8%
associate-+r-62.8%
sqrt-unprod44.5%
Applied egg-rr44.5%
associate-*l/44.4%
*-lft-identity44.4%
associate-+l+44.4%
associate--l+44.4%
count-244.4%
*-commutative44.4%
Simplified44.4%
Taylor expanded in x around inf 62.4%
Taylor expanded in x around inf 98.4%
rem-exp-log91.0%
exp-neg91.0%
unpow1/291.0%
exp-prod91.0%
distribute-lft-neg-out91.0%
distribute-rgt-neg-in91.0%
metadata-eval91.0%
exp-to-pow98.6%
Simplified98.6%
Final simplification97.2%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 53.0%
Taylor expanded in x around inf 3.5%
Final simplification3.5%
(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 53.0%
Taylor expanded in x around 0 51.1%
Final simplification51.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 2024044
(FPCore (x)
:name "Main:bigenough3 from C"
:precision binary64
:herbie-target
(/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x)))
(- (sqrt (+ x 1.0)) (sqrt x)))