
(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 8 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 (<= (- x (sqrt (- (* x x) eps))) -2e-154) (/ eps (+ x (hypot x (sqrt (- eps))))) (/ eps (fma x 2.0 (* (/ eps x) -0.5)))))
double code(double x, double eps) {
double tmp;
if ((x - sqrt(((x * x) - eps))) <= -2e-154) {
tmp = eps / (x + hypot(x, sqrt(-eps)));
} else {
tmp = eps / fma(x, 2.0, ((eps / x) * -0.5));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -2e-154) tmp = Float64(eps / Float64(x + hypot(x, sqrt(Float64(-eps))))); else tmp = Float64(eps / fma(x, 2.0, Float64(Float64(eps / x) * -0.5))); end return tmp end
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -2e-154], N[(eps / N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(eps / N[(x * 2.0 + N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x - \sqrt{x \cdot x - \varepsilon} \leq -2 \cdot 10^{-154}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\mathsf{fma}\left(x, 2, \frac{\varepsilon}{x} \cdot -0.5\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.9999999999999999e-154Initial program 97.9%
flip--97.9%
div-inv97.5%
add-sqr-sqrt97.3%
sub-neg97.3%
add-sqr-sqrt97.3%
hypot-def97.3%
Applied egg-rr97.3%
associate-*r/97.4%
*-rgt-identity97.4%
associate--r-99.3%
+-inverses99.3%
+-lft-identity99.3%
Simplified99.3%
if -1.9999999999999999e-154 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 8.9%
flip--8.9%
div-inv8.9%
add-sqr-sqrt9.1%
sub-neg9.1%
add-sqr-sqrt2.6%
hypot-def2.6%
Applied egg-rr2.6%
associate-*r/2.6%
*-rgt-identity2.6%
associate--r-45.9%
+-inverses45.9%
+-lft-identity45.9%
Simplified45.9%
Taylor expanded in x around inf 0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt98.9%
*-commutative98.9%
associate-*r*98.9%
metadata-eval98.9%
associate-*r/98.9%
*-commutative98.9%
Simplified98.9%
Final simplification99.1%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -2e-154) t_0 (/ eps (fma x 2.0 (* (/ eps x) -0.5))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -2e-154) {
tmp = t_0;
} else {
tmp = eps / fma(x, 2.0, ((eps / x) * -0.5));
}
return tmp;
}
function code(x, eps) t_0 = Float64(x - sqrt(Float64(Float64(x * x) - eps))) tmp = 0.0 if (t_0 <= -2e-154) tmp = t_0; else tmp = Float64(eps / fma(x, 2.0, Float64(Float64(eps / x) * -0.5))); end return tmp end
code[x_, eps_] := Block[{t$95$0 = N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2e-154], t$95$0, N[(eps / N[(x * 2.0 + N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x - \sqrt{x \cdot x - \varepsilon}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-154}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\mathsf{fma}\left(x, 2, \frac{\varepsilon}{x} \cdot -0.5\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.9999999999999999e-154Initial program 97.9%
if -1.9999999999999999e-154 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 8.9%
flip--8.9%
div-inv8.9%
add-sqr-sqrt9.1%
sub-neg9.1%
add-sqr-sqrt2.6%
hypot-def2.6%
Applied egg-rr2.6%
associate-*r/2.6%
*-rgt-identity2.6%
associate--r-45.9%
+-inverses45.9%
+-lft-identity45.9%
Simplified45.9%
Taylor expanded in x around inf 0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt98.9%
*-commutative98.9%
associate-*r*98.9%
metadata-eval98.9%
associate-*r/98.9%
*-commutative98.9%
Simplified98.9%
Final simplification98.3%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -2e-154) t_0 (/ eps (+ x (+ x (* (/ eps x) -0.5)))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -2e-154) {
tmp = t_0;
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: t_0
real(8) :: tmp
t_0 = x - sqrt(((x * x) - eps))
if (t_0 <= (-2d-154)) then
tmp = t_0
else
tmp = eps / (x + (x + ((eps / x) * (-0.5d0))))
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = x - Math.sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -2e-154) {
tmp = t_0;
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
def code(x, eps): t_0 = x - math.sqrt(((x * x) - eps)) tmp = 0 if t_0 <= -2e-154: tmp = t_0 else: tmp = eps / (x + (x + ((eps / x) * -0.5))) return tmp
function code(x, eps) t_0 = Float64(x - sqrt(Float64(Float64(x * x) - eps))) tmp = 0.0 if (t_0 <= -2e-154) tmp = t_0; else tmp = Float64(eps / Float64(x + Float64(x + Float64(Float64(eps / x) * -0.5)))); end return tmp end
function tmp_2 = code(x, eps) t_0 = x - sqrt(((x * x) - eps)); tmp = 0.0; if (t_0 <= -2e-154) tmp = t_0; else tmp = eps / (x + (x + ((eps / x) * -0.5))); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2e-154], t$95$0, N[(eps / N[(x + N[(x + N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x - \sqrt{x \cdot x - \varepsilon}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-154}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{x + \left(x + \frac{\varepsilon}{x} \cdot -0.5\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.9999999999999999e-154Initial program 97.9%
if -1.9999999999999999e-154 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 8.9%
flip--8.9%
div-inv8.9%
add-sqr-sqrt9.1%
sub-neg9.1%
add-sqr-sqrt2.6%
hypot-def2.6%
Applied egg-rr2.6%
associate-*r/2.6%
*-rgt-identity2.6%
associate--r-45.9%
+-inverses45.9%
+-lft-identity45.9%
Simplified45.9%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt98.8%
*-commutative98.8%
associate-*r*98.8%
metadata-eval98.8%
associate-*r/98.8%
*-commutative98.8%
Simplified98.8%
Final simplification98.3%
(FPCore (x eps)
:precision binary64
(if (or (<= x 5.6e-121)
(and (not (<= x 7.6e-115))
(or (<= x 2.5e-107) (and (not (<= x 4.5e-97)) (<= x 6.8e-82)))))
(- x (sqrt (- eps)))
(/ eps (+ x (+ x (* (/ eps x) -0.5))))))
double code(double x, double eps) {
double tmp;
if ((x <= 5.6e-121) || (!(x <= 7.6e-115) && ((x <= 2.5e-107) || (!(x <= 4.5e-97) && (x <= 6.8e-82))))) {
tmp = x - sqrt(-eps);
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((x <= 5.6d-121) .or. (.not. (x <= 7.6d-115)) .and. (x <= 2.5d-107) .or. (.not. (x <= 4.5d-97)) .and. (x <= 6.8d-82)) then
tmp = x - sqrt(-eps)
else
tmp = eps / (x + (x + ((eps / x) * (-0.5d0))))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= 5.6e-121) || (!(x <= 7.6e-115) && ((x <= 2.5e-107) || (!(x <= 4.5e-97) && (x <= 6.8e-82))))) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= 5.6e-121) or (not (x <= 7.6e-115) and ((x <= 2.5e-107) or (not (x <= 4.5e-97) and (x <= 6.8e-82)))): tmp = x - math.sqrt(-eps) else: tmp = eps / (x + (x + ((eps / x) * -0.5))) return tmp
function code(x, eps) tmp = 0.0 if ((x <= 5.6e-121) || (!(x <= 7.6e-115) && ((x <= 2.5e-107) || (!(x <= 4.5e-97) && (x <= 6.8e-82))))) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(eps / Float64(x + Float64(x + Float64(Float64(eps / x) * -0.5)))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= 5.6e-121) || (~((x <= 7.6e-115)) && ((x <= 2.5e-107) || (~((x <= 4.5e-97)) && (x <= 6.8e-82))))) tmp = x - sqrt(-eps); else tmp = eps / (x + (x + ((eps / x) * -0.5))); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, 5.6e-121], And[N[Not[LessEqual[x, 7.6e-115]], $MachinePrecision], Or[LessEqual[x, 2.5e-107], And[N[Not[LessEqual[x, 4.5e-97]], $MachinePrecision], LessEqual[x, 6.8e-82]]]]], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(eps / N[(x + N[(x + N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.6 \cdot 10^{-121} \lor \neg \left(x \leq 7.6 \cdot 10^{-115}\right) \land \left(x \leq 2.5 \cdot 10^{-107} \lor \neg \left(x \leq 4.5 \cdot 10^{-97}\right) \land x \leq 6.8 \cdot 10^{-82}\right):\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{x + \left(x + \frac{\varepsilon}{x} \cdot -0.5\right)}\\
\end{array}
\end{array}
if x < 5.6000000000000002e-121 or 7.59999999999999984e-115 < x < 2.49999999999999985e-107 or 4.5000000000000001e-97 < x < 6.7999999999999995e-82Initial program 98.5%
Taylor expanded in x around 0 95.5%
neg-mul-195.5%
Simplified95.5%
if 5.6000000000000002e-121 < x < 7.59999999999999984e-115 or 2.49999999999999985e-107 < x < 4.5000000000000001e-97 or 6.7999999999999995e-82 < x Initial program 18.9%
flip--19.0%
div-inv18.9%
add-sqr-sqrt19.0%
sub-neg19.0%
add-sqr-sqrt14.4%
hypot-def14.4%
Applied egg-rr14.4%
associate-*r/14.4%
*-rgt-identity14.4%
associate--r-54.7%
+-inverses54.7%
+-lft-identity54.7%
Simplified54.7%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt90.1%
*-commutative90.1%
associate-*r*90.1%
metadata-eval90.1%
associate-*r/90.1%
*-commutative90.1%
Simplified90.1%
Final simplification92.8%
(FPCore (x eps) :precision binary64 (/ eps (+ x (+ x (* (/ eps x) -0.5)))))
double code(double x, double eps) {
return eps / (x + (x + ((eps / x) * -0.5)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (x + (x + ((eps / x) * (-0.5d0))))
end function
public static double code(double x, double eps) {
return eps / (x + (x + ((eps / x) * -0.5)));
}
def code(x, eps): return eps / (x + (x + ((eps / x) * -0.5)))
function code(x, eps) return Float64(eps / Float64(x + Float64(x + Float64(Float64(eps / x) * -0.5)))) end
function tmp = code(x, eps) tmp = eps / (x + (x + ((eps / x) * -0.5))); end
code[x_, eps_] := N[(eps / N[(x + N[(x + N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{x + \left(x + \frac{\varepsilon}{x} \cdot -0.5\right)}
\end{array}
Initial program 59.3%
flip--59.3%
div-inv59.1%
add-sqr-sqrt59.1%
sub-neg59.1%
add-sqr-sqrt56.2%
hypot-def56.2%
Applied egg-rr56.2%
associate-*r/56.3%
*-rgt-identity56.3%
associate--r-76.2%
+-inverses76.2%
+-lft-identity76.2%
Simplified76.2%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt48.6%
*-commutative48.6%
associate-*r*48.6%
metadata-eval48.6%
associate-*r/48.6%
*-commutative48.6%
Simplified48.6%
Final simplification48.6%
(FPCore (x eps) :precision binary64 (* (/ eps x) 0.5))
double code(double x, double eps) {
return (eps / x) * 0.5;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (eps / x) * 0.5d0
end function
public static double code(double x, double eps) {
return (eps / x) * 0.5;
}
def code(x, eps): return (eps / x) * 0.5
function code(x, eps) return Float64(Float64(eps / x) * 0.5) end
function tmp = code(x, eps) tmp = (eps / x) * 0.5; end
code[x_, eps_] := N[(N[(eps / x), $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{x} \cdot 0.5
\end{array}
Initial program 59.3%
Taylor expanded in x around inf 47.6%
Final simplification47.6%
(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 59.3%
flip--59.3%
div-inv59.1%
add-sqr-sqrt59.1%
sub-neg59.1%
add-sqr-sqrt56.2%
hypot-def56.2%
Applied egg-rr56.2%
associate-*r/56.3%
*-rgt-identity56.3%
associate--r-76.2%
+-inverses76.2%
+-lft-identity76.2%
Simplified76.2%
Taylor expanded in x around inf 0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt48.6%
*-commutative48.6%
associate-*r*48.6%
metadata-eval48.6%
associate-*r/48.6%
*-commutative48.6%
Simplified48.6%
Taylor expanded in eps around inf 5.3%
*-commutative5.3%
Simplified5.3%
Final simplification5.3%
(FPCore (x eps) :precision binary64 0.0)
double code(double x, double eps) {
return 0.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.0d0
end function
public static double code(double x, double eps) {
return 0.0;
}
def code(x, eps): return 0.0
function code(x, eps) return 0.0 end
function tmp = code(x, eps) tmp = 0.0; end
code[x_, eps_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 59.3%
sub-neg59.3%
+-commutative59.3%
add-sqr-sqrt58.5%
distribute-rgt-neg-in58.5%
fma-def58.4%
pow1/258.4%
sqrt-pow158.6%
metadata-eval58.6%
pow1/258.6%
sqrt-pow158.4%
metadata-eval58.4%
Applied egg-rr58.4%
Taylor expanded in x around inf 4.4%
distribute-lft1-in4.4%
metadata-eval4.4%
mul0-lft4.4%
Simplified4.4%
Final simplification4.4%
(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 2023200
(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))))