
(FPCore (x eps) :precision binary64 (- x (sqrt (- (* x x) eps))))
double code(double x, double eps) {
return x - sqrt(((x * x) - eps));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x - sqrt(((x * x) - eps))
end function
public static double code(double x, double eps) {
return x - Math.sqrt(((x * x) - eps));
}
def code(x, eps): return x - math.sqrt(((x * x) - eps))
function code(x, eps) return Float64(x - sqrt(Float64(Float64(x * x) - eps))) end
function tmp = code(x, eps) tmp = x - sqrt(((x * x) - eps)); end
code[x_, eps_] := N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \sqrt{x \cdot x - \varepsilon}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- x (sqrt (- (* x x) eps))))
double code(double x, double eps) {
return x - sqrt(((x * x) - eps));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x - sqrt(((x * x) - eps))
end function
public static double code(double x, double eps) {
return x - Math.sqrt(((x * x) - eps));
}
def code(x, eps): return x - math.sqrt(((x * x) - eps))
function code(x, eps) return Float64(x - sqrt(Float64(Float64(x * x) - eps))) end
function tmp = code(x, eps) tmp = x - sqrt(((x * x) - eps)); end
code[x_, eps_] := N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \sqrt{x \cdot x - \varepsilon}
\end{array}
(FPCore (x eps)
:precision binary64
(if (<= eps -7.9e-146)
(/
1.0
(/ (+ x (hypot x (sqrt (- eps)))) (+ eps (- (pow x 2.0) (pow x 2.0)))))
(- x (sqrt (- (* x x) eps)))))
double code(double x, double eps) {
double tmp;
if (eps <= -7.9e-146) {
tmp = 1.0 / ((x + hypot(x, sqrt(-eps))) / (eps + (pow(x, 2.0) - pow(x, 2.0))));
} else {
tmp = x - sqrt(((x * x) - eps));
}
return tmp;
}
public static double code(double x, double eps) {
double tmp;
if (eps <= -7.9e-146) {
tmp = 1.0 / ((x + Math.hypot(x, Math.sqrt(-eps))) / (eps + (Math.pow(x, 2.0) - Math.pow(x, 2.0))));
} else {
tmp = x - Math.sqrt(((x * x) - eps));
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -7.9e-146: tmp = 1.0 / ((x + math.hypot(x, math.sqrt(-eps))) / (eps + (math.pow(x, 2.0) - math.pow(x, 2.0)))) else: tmp = x - math.sqrt(((x * x) - eps)) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -7.9e-146) tmp = Float64(1.0 / Float64(Float64(x + hypot(x, sqrt(Float64(-eps)))) / Float64(eps + Float64((x ^ 2.0) - (x ^ 2.0))))); else tmp = Float64(x - sqrt(Float64(Float64(x * x) - eps))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -7.9e-146) tmp = 1.0 / ((x + hypot(x, sqrt(-eps))) / (eps + ((x ^ 2.0) - (x ^ 2.0)))); else tmp = x - sqrt(((x * x) - eps)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -7.9e-146], N[(1.0 / N[(N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] / N[(eps + N[(N[Power[x, 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -7.9 \cdot 10^{-146}:\\
\;\;\;\;\frac{1}{\frac{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}{\varepsilon + \left({x}^{2} - {x}^{2}\right)}}\\
\mathbf{else}:\\
\;\;\;\;x - \sqrt{x \cdot x - \varepsilon}\\
\end{array}
\end{array}
if eps < -7.90000000000000013e-146Initial program 82.3%
flip--82.1%
clear-num81.9%
sub-neg81.9%
add-sqr-sqrt81.9%
hypot-define81.9%
add-sqr-sqrt81.9%
associate--r-98.3%
pow298.3%
pow298.3%
Applied egg-rr98.3%
if -7.90000000000000013e-146 < eps Initial program 93.4%
Final simplification95.0%
(FPCore (x eps) :precision binary64 (if (<= eps -6.3e-146) (/ eps (+ x (hypot x (sqrt (- eps))))) (- x (sqrt (- (* x x) eps)))))
double code(double x, double eps) {
double tmp;
if (eps <= -6.3e-146) {
tmp = eps / (x + hypot(x, sqrt(-eps)));
} else {
tmp = x - sqrt(((x * x) - eps));
}
return tmp;
}
public static double code(double x, double eps) {
double tmp;
if (eps <= -6.3e-146) {
tmp = eps / (x + Math.hypot(x, Math.sqrt(-eps)));
} else {
tmp = x - Math.sqrt(((x * x) - eps));
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -6.3e-146: tmp = eps / (x + math.hypot(x, math.sqrt(-eps))) else: tmp = x - math.sqrt(((x * x) - eps)) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -6.3e-146) tmp = Float64(eps / Float64(x + hypot(x, sqrt(Float64(-eps))))); else tmp = Float64(x - sqrt(Float64(Float64(x * x) - eps))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -6.3e-146) tmp = eps / (x + hypot(x, sqrt(-eps))); else tmp = x - sqrt(((x * x) - eps)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -6.3e-146], N[(eps / N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -6.3 \cdot 10^{-146}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}\\
\mathbf{else}:\\
\;\;\;\;x - \sqrt{x \cdot x - \varepsilon}\\
\end{array}
\end{array}
if eps < -6.2999999999999997e-146Initial program 82.3%
flip--82.1%
div-inv81.8%
add-sqr-sqrt81.9%
associate--r-98.2%
pow298.2%
pow298.2%
sub-neg98.2%
add-sqr-sqrt98.2%
hypot-define98.2%
Applied egg-rr98.2%
associate-*r/98.2%
+-inverses98.2%
+-lft-identity98.2%
*-rgt-identity98.2%
Simplified98.2%
if -6.2999999999999997e-146 < eps Initial program 93.4%
Final simplification95.0%
(FPCore (x eps) :precision binary64 (if (<= x 2.7e-107) (- x (sqrt (- eps))) (- x (+ x (/ (* eps -0.5) x)))))
double code(double x, double eps) {
double tmp;
if (x <= 2.7e-107) {
tmp = x - sqrt(-eps);
} else {
tmp = x - (x + ((eps * -0.5) / x));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= 2.7d-107) then
tmp = x - sqrt(-eps)
else
tmp = x - (x + ((eps * (-0.5d0)) / x))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= 2.7e-107) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = x - (x + ((eps * -0.5) / x));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 2.7e-107: tmp = x - math.sqrt(-eps) else: tmp = x - (x + ((eps * -0.5) / x)) return tmp
function code(x, eps) tmp = 0.0 if (x <= 2.7e-107) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(x - Float64(x + Float64(Float64(eps * -0.5) / x))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= 2.7e-107) tmp = x - sqrt(-eps); else tmp = x - (x + ((eps * -0.5) / x)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 2.7e-107], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(x - N[(x + N[(N[(eps * -0.5), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.7 \cdot 10^{-107}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;x - \left(x + \frac{\varepsilon \cdot -0.5}{x}\right)\\
\end{array}
\end{array}
if x < 2.7e-107Initial program 98.0%
Taylor expanded in x around 0 94.7%
neg-mul-194.7%
Simplified94.7%
if 2.7e-107 < x Initial program 81.4%
Taylor expanded in x around inf 71.4%
associate-*r/71.4%
Simplified71.4%
Final simplification83.3%
(FPCore (x eps) :precision binary64 (- x (sqrt (- (* x x) eps))))
double code(double x, double eps) {
return x - sqrt(((x * x) - eps));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x - sqrt(((x * x) - eps))
end function
public static double code(double x, double eps) {
return x - Math.sqrt(((x * x) - eps));
}
def code(x, eps): return x - math.sqrt(((x * x) - eps))
function code(x, eps) return Float64(x - sqrt(Float64(Float64(x * x) - eps))) end
function tmp = code(x, eps) tmp = x - sqrt(((x * x) - eps)); end
code[x_, eps_] := N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \sqrt{x \cdot x - \varepsilon}
\end{array}
Initial program 89.9%
Final simplification89.9%
(FPCore (x eps) :precision binary64 (if (<= eps -3.5e-157) (/ 1.0 (+ (/ -0.5 x) (* x (/ 2.0 eps)))) (- x (+ x (/ (* eps -0.5) x)))))
double code(double x, double eps) {
double tmp;
if (eps <= -3.5e-157) {
tmp = 1.0 / ((-0.5 / x) + (x * (2.0 / eps)));
} else {
tmp = x - (x + ((eps * -0.5) / x));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-3.5d-157)) then
tmp = 1.0d0 / (((-0.5d0) / x) + (x * (2.0d0 / eps)))
else
tmp = x - (x + ((eps * (-0.5d0)) / x))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -3.5e-157) {
tmp = 1.0 / ((-0.5 / x) + (x * (2.0 / eps)));
} else {
tmp = x - (x + ((eps * -0.5) / x));
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -3.5e-157: tmp = 1.0 / ((-0.5 / x) + (x * (2.0 / eps))) else: tmp = x - (x + ((eps * -0.5) / x)) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -3.5e-157) tmp = Float64(1.0 / Float64(Float64(-0.5 / x) + Float64(x * Float64(2.0 / eps)))); else tmp = Float64(x - Float64(x + Float64(Float64(eps * -0.5) / x))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -3.5e-157) tmp = 1.0 / ((-0.5 / x) + (x * (2.0 / eps))); else tmp = x - (x + ((eps * -0.5) / x)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -3.5e-157], N[(1.0 / N[(N[(-0.5 / x), $MachinePrecision] + N[(x * N[(2.0 / eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x - N[(x + N[(N[(eps * -0.5), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -3.5 \cdot 10^{-157}:\\
\;\;\;\;\frac{1}{\frac{-0.5}{x} + x \cdot \frac{2}{\varepsilon}}\\
\mathbf{else}:\\
\;\;\;\;x - \left(x + \frac{\varepsilon \cdot -0.5}{x}\right)\\
\end{array}
\end{array}
if eps < -3.5000000000000002e-157Initial program 82.4%
flip--82.2%
clear-num82.0%
sub-neg82.0%
add-sqr-sqrt82.0%
hypot-define82.0%
add-sqr-sqrt82.0%
associate--r-97.3%
pow297.3%
pow297.3%
Applied egg-rr97.3%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
fma-define0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt25.0%
metadata-eval25.0%
Simplified25.0%
fma-undefine25.0%
clear-num25.1%
un-div-inv25.1%
clear-num25.0%
associate-/r/25.0%
associate-/l/24.9%
+-commutative24.9%
clear-num25.0%
associate-/r*25.1%
clear-num25.0%
/-rgt-identity25.0%
Applied egg-rr25.0%
if -3.5000000000000002e-157 < eps Initial program 93.8%
Taylor expanded in x around inf 53.5%
associate-*r/53.5%
Simplified53.5%
Final simplification43.8%
(FPCore (x eps) :precision binary64 (if (<= eps -1.7e-156) (* 0.5 (/ eps x)) (- x (+ x (/ (* eps -0.5) x)))))
double code(double x, double eps) {
double tmp;
if (eps <= -1.7e-156) {
tmp = 0.5 * (eps / x);
} else {
tmp = x - (x + ((eps * -0.5) / x));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-1.7d-156)) then
tmp = 0.5d0 * (eps / x)
else
tmp = x - (x + ((eps * (-0.5d0)) / x))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -1.7e-156) {
tmp = 0.5 * (eps / x);
} else {
tmp = x - (x + ((eps * -0.5) / x));
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -1.7e-156: tmp = 0.5 * (eps / x) else: tmp = x - (x + ((eps * -0.5) / x)) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -1.7e-156) tmp = Float64(0.5 * Float64(eps / x)); else tmp = Float64(x - Float64(x + Float64(Float64(eps * -0.5) / x))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -1.7e-156) tmp = 0.5 * (eps / x); else tmp = x - (x + ((eps * -0.5) / x)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -1.7e-156], N[(0.5 * N[(eps / x), $MachinePrecision]), $MachinePrecision], N[(x - N[(x + N[(N[(eps * -0.5), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -1.7 \cdot 10^{-156}:\\
\;\;\;\;0.5 \cdot \frac{\varepsilon}{x}\\
\mathbf{else}:\\
\;\;\;\;x - \left(x + \frac{\varepsilon \cdot -0.5}{x}\right)\\
\end{array}
\end{array}
if eps < -1.69999999999999995e-156Initial program 82.4%
Taylor expanded in x around inf 24.0%
if -1.69999999999999995e-156 < eps Initial program 93.8%
Taylor expanded in x around inf 53.5%
associate-*r/53.5%
Simplified53.5%
Final simplification43.5%
(FPCore (x eps) :precision binary64 (* 0.5 (/ eps x)))
double code(double x, double eps) {
return 0.5 * (eps / x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.5d0 * (eps / x)
end function
public static double code(double x, double eps) {
return 0.5 * (eps / x);
}
def code(x, eps): return 0.5 * (eps / x)
function code(x, eps) return Float64(0.5 * Float64(eps / x)) end
function tmp = code(x, eps) tmp = 0.5 * (eps / x); end
code[x_, eps_] := N[(0.5 * N[(eps / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \frac{\varepsilon}{x}
\end{array}
Initial program 89.9%
Taylor expanded in x around inf 16.4%
Final simplification16.4%
(FPCore (x eps) :precision binary64 (* x -2.0))
double code(double x, double eps) {
return x * -2.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x * (-2.0d0)
end function
public static double code(double x, double eps) {
return x * -2.0;
}
def code(x, eps): return x * -2.0
function code(x, eps) return Float64(x * -2.0) end
function tmp = code(x, eps) tmp = x * -2.0; end
code[x_, eps_] := N[(x * -2.0), $MachinePrecision]
\begin{array}{l}
\\
x \cdot -2
\end{array}
Initial program 89.9%
flip--89.8%
clear-num89.6%
sub-neg89.6%
add-sqr-sqrt68.8%
hypot-define68.8%
add-sqr-sqrt68.8%
associate--r-63.2%
pow263.2%
pow263.2%
Applied egg-rr63.2%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
fma-define0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt17.3%
metadata-eval17.3%
Simplified17.3%
Taylor expanded in x around 0 5.1%
Final simplification5.1%
(FPCore (x eps) :precision binary64 x)
double code(double x, double eps) {
return x;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x
end function
public static double code(double x, double eps) {
return x;
}
def code(x, eps): return x
function code(x, eps) return x end
function tmp = code(x, eps) tmp = x; end
code[x_, eps_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 89.9%
Taylor expanded in eps around inf 3.3%
Final simplification3.3%
(FPCore (x eps) :precision binary64 (/ eps (+ x (sqrt (- (* x x) eps)))))
double code(double x, double eps) {
return eps / (x + sqrt(((x * x) - eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (x + sqrt(((x * x) - eps)))
end function
public static double code(double x, double eps) {
return eps / (x + Math.sqrt(((x * x) - eps)));
}
def code(x, eps): return eps / (x + math.sqrt(((x * x) - eps)))
function code(x, eps) return Float64(eps / Float64(x + sqrt(Float64(Float64(x * x) - eps)))) end
function tmp = code(x, eps) tmp = eps / (x + sqrt(((x * x) - eps))); end
code[x_, eps_] := N[(eps / N[(x + N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{x + \sqrt{x \cdot x - \varepsilon}}
\end{array}
herbie shell --seed 2024046
(FPCore (x eps)
:name "ENA, Section 1.4, Exercise 4d"
:precision binary64
:pre (and (and (<= 0.0 x) (<= x 1000000000.0)) (and (<= -1.0 eps) (<= eps 1.0)))
:herbie-target
(/ eps (+ x (sqrt (- (* x x) eps))))
(- x (sqrt (- (* x x) eps))))