
(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 7 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-157) (/ eps (+ x (hypot x (sqrt (- eps))))) (/ eps (+ x (fma eps (/ -0.5 x) x)))))
double code(double x, double eps) {
double tmp;
if ((x - sqrt(((x * x) - eps))) <= -2e-157) {
tmp = eps / (x + hypot(x, sqrt(-eps)));
} else {
tmp = eps / (x + fma(eps, (-0.5 / x), x));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -2e-157) tmp = Float64(eps / Float64(x + hypot(x, sqrt(Float64(-eps))))); else tmp = Float64(eps / Float64(x + fma(eps, Float64(-0.5 / x), x))); end return tmp end
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -2e-157], N[(eps / N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(eps / N[(x + N[(eps * N[(-0.5 / x), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x - \sqrt{x \cdot x - \varepsilon} \leq -2 \cdot 10^{-157}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{fma}\left(\varepsilon, \frac{-0.5}{x}, x\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.99999999999999989e-157Initial program 98.9%
flip--98.7%
div-inv98.6%
add-sqr-sqrt98.2%
associate--r-99.2%
pow299.2%
pow299.2%
sub-neg99.2%
add-sqr-sqrt99.2%
hypot-define99.2%
Applied egg-rr99.2%
*-commutative99.2%
+-inverses99.2%
+-lft-identity99.2%
associate-*l/99.2%
*-lft-identity99.2%
Simplified99.2%
if -1.99999999999999989e-157 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 8.6%
flip--8.6%
div-inv8.6%
add-sqr-sqrt8.8%
associate--r-99.6%
pow299.6%
pow299.6%
sub-neg99.6%
add-sqr-sqrt44.7%
hypot-define44.7%
Applied egg-rr44.7%
*-commutative44.7%
+-inverses44.7%
+-lft-identity44.7%
associate-*l/44.8%
*-lft-identity44.8%
Simplified44.8%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt97.9%
mul-1-neg97.9%
distribute-neg-frac97.9%
distribute-rgt-neg-in97.9%
distribute-lft-neg-in97.9%
metadata-eval97.9%
associate-*r/97.9%
*-commutative97.9%
associate-/l*97.9%
fma-define97.9%
Simplified97.9%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -5e-154) t_0 (/ eps (+ (* -0.5 (/ eps x)) (* x 2.0))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -5e-154) {
tmp = t_0;
} else {
tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
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 <= (-5d-154)) then
tmp = t_0
else
tmp = eps / (((-0.5d0) * (eps / x)) + (x * 2.0d0))
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 <= -5e-154) {
tmp = t_0;
} else {
tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
return tmp;
}
def code(x, eps): t_0 = x - math.sqrt(((x * x) - eps)) tmp = 0 if t_0 <= -5e-154: tmp = t_0 else: tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)) return tmp
function code(x, eps) t_0 = Float64(x - sqrt(Float64(Float64(x * x) - eps))) tmp = 0.0 if (t_0 <= -5e-154) tmp = t_0; else tmp = Float64(eps / Float64(Float64(-0.5 * Float64(eps / x)) + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, eps) t_0 = x - sqrt(((x * x) - eps)); tmp = 0.0; if (t_0 <= -5e-154) tmp = t_0; else tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)); 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, -5e-154], t$95$0, N[(eps / N[(N[(-0.5 * N[(eps / x), $MachinePrecision]), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x - \sqrt{x \cdot x - \varepsilon}\\
\mathbf{if}\;t\_0 \leq -5 \cdot 10^{-154}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{-0.5 \cdot \frac{\varepsilon}{x} + x \cdot 2}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -5.0000000000000002e-154Initial program 99.0%
if -5.0000000000000002e-154 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 9.3%
flip--9.3%
div-inv9.3%
add-sqr-sqrt9.5%
associate--r-99.7%
pow299.7%
pow299.7%
sub-neg99.7%
add-sqr-sqrt45.2%
hypot-define45.2%
Applied egg-rr45.2%
*-commutative45.2%
+-inverses45.2%
+-lft-identity45.2%
associate-*l/45.4%
*-lft-identity45.4%
Simplified45.4%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-*r/0.0%
*-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt97.7%
mul-1-neg97.7%
distribute-lft-neg-in97.7%
distribute-rgt-neg-in97.7%
metadata-eval97.7%
associate-/l*97.7%
Simplified97.7%
Taylor expanded in eps around 0 97.7%
Final simplification98.5%
(FPCore (x eps) :precision binary64 (if (<= x 2.95e-92) (- x (sqrt (- eps))) (/ eps (+ (* -0.5 (/ eps x)) (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= 2.95e-92) {
tmp = x - sqrt(-eps);
} else {
tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= 2.95d-92) then
tmp = x - sqrt(-eps)
else
tmp = eps / (((-0.5d0) * (eps / x)) + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= 2.95e-92) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 2.95e-92: tmp = x - math.sqrt(-eps) else: tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (x <= 2.95e-92) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(eps / Float64(Float64(-0.5 * Float64(eps / x)) + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= 2.95e-92) tmp = x - sqrt(-eps); else tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 2.95e-92], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(eps / N[(N[(-0.5 * N[(eps / x), $MachinePrecision]), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.95 \cdot 10^{-92}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{-0.5 \cdot \frac{\varepsilon}{x} + x \cdot 2}\\
\end{array}
\end{array}
if x < 2.95e-92Initial program 96.0%
Taylor expanded in x around 0 93.6%
neg-mul-193.6%
Simplified93.6%
if 2.95e-92 < x Initial program 26.4%
flip--26.4%
div-inv26.4%
add-sqr-sqrt26.4%
associate--r-99.5%
pow299.5%
pow299.5%
sub-neg99.5%
add-sqr-sqrt55.9%
hypot-define55.9%
Applied egg-rr55.9%
*-commutative55.9%
+-inverses55.9%
+-lft-identity55.9%
associate-*l/55.9%
*-lft-identity55.9%
Simplified55.9%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-*r/0.0%
*-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt81.1%
mul-1-neg81.1%
distribute-lft-neg-in81.1%
distribute-rgt-neg-in81.1%
metadata-eval81.1%
associate-/l*81.1%
Simplified81.1%
Taylor expanded in eps around 0 81.1%
Final simplification88.1%
(FPCore (x eps) :precision binary64 (/ eps (+ (* -0.5 (/ eps x)) (* x 2.0))))
double code(double x, double eps) {
return eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (((-0.5d0) * (eps / x)) + (x * 2.0d0))
end function
public static double code(double x, double eps) {
return eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
def code(x, eps): return eps / ((-0.5 * (eps / x)) + (x * 2.0))
function code(x, eps) return Float64(eps / Float64(Float64(-0.5 * Float64(eps / x)) + Float64(x * 2.0))) end
function tmp = code(x, eps) tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)); end
code[x_, eps_] := N[(eps / N[(N[(-0.5 * N[(eps / x), $MachinePrecision]), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{-0.5 \cdot \frac{\varepsilon}{x} + x \cdot 2}
\end{array}
Initial program 65.0%
flip--64.9%
div-inv64.8%
add-sqr-sqrt64.6%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt78.8%
hypot-define78.8%
Applied egg-rr78.8%
*-commutative78.8%
+-inverses78.8%
+-lft-identity78.8%
associate-*l/78.8%
*-lft-identity78.8%
Simplified78.8%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-*r/0.0%
*-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt41.9%
mul-1-neg41.9%
distribute-lft-neg-in41.9%
distribute-rgt-neg-in41.9%
metadata-eval41.9%
associate-/l*41.9%
Simplified41.9%
Taylor expanded in eps around 0 41.9%
Final simplification41.9%
(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 65.0%
Taylor expanded in x around inf 41.3%
Final simplification41.3%
(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 65.0%
flip--64.9%
div-inv64.8%
add-sqr-sqrt64.6%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt78.8%
hypot-define78.8%
Applied egg-rr78.8%
*-commutative78.8%
+-inverses78.8%
+-lft-identity78.8%
associate-*l/78.8%
*-lft-identity78.8%
Simplified78.8%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-*r/0.0%
*-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt41.9%
mul-1-neg41.9%
distribute-lft-neg-in41.9%
distribute-rgt-neg-in41.9%
metadata-eval41.9%
associate-/l*41.9%
Simplified41.9%
Taylor expanded in eps around inf 5.4%
*-commutative5.4%
Simplified5.4%
(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 65.0%
Taylor expanded in x around inf 4.2%
Taylor expanded in x around 0 4.2%
(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 2024116
(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)))
:alt
(! :herbie-platform default (/ eps (+ x (sqrt (- (* x x) eps)))))
(- x (sqrt (- (* x x) eps))))