
(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))) -1e-152)
(/ eps (+ x (hypot x (sqrt (- eps)))))
(/
eps
(fma
-0.125
(* (/ eps (* x x)) (/ eps x))
(+ (* (/ eps x) -0.5) (* x 2.0))))))
double code(double x, double eps) {
double tmp;
if ((x - sqrt(((x * x) - eps))) <= -1e-152) {
tmp = eps / (x + hypot(x, sqrt(-eps)));
} else {
tmp = eps / fma(-0.125, ((eps / (x * x)) * (eps / x)), (((eps / x) * -0.5) + (x * 2.0)));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -1e-152) tmp = Float64(eps / Float64(x + hypot(x, sqrt(Float64(-eps))))); else tmp = Float64(eps / fma(-0.125, Float64(Float64(eps / Float64(x * x)) * Float64(eps / x)), Float64(Float64(Float64(eps / x) * -0.5) + Float64(x * 2.0)))); end return tmp end
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1e-152], N[(eps / N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(eps / N[(-0.125 * N[(N[(eps / N[(x * x), $MachinePrecision]), $MachinePrecision] * N[(eps / x), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x - \sqrt{x \cdot x - \varepsilon} \leq -1 \cdot 10^{-152}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\mathsf{fma}\left(-0.125, \frac{\varepsilon}{x \cdot x} \cdot \frac{\varepsilon}{x}, \frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.00000000000000007e-152Initial program 98.8%
flip--98.7%
div-inv98.5%
add-sqr-sqrt98.0%
sub-neg98.0%
add-sqr-sqrt98.0%
hypot-def98.0%
Applied egg-rr98.0%
associate-*r/98.0%
*-rgt-identity98.0%
associate--r-99.1%
+-inverses99.1%
+-lft-identity99.1%
Simplified99.1%
if -1.00000000000000007e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 6.9%
flip--6.9%
div-inv6.9%
add-sqr-sqrt6.9%
sub-neg6.9%
add-sqr-sqrt3.1%
hypot-def3.1%
Applied egg-rr3.1%
associate-*r/3.1%
*-rgt-identity3.1%
associate--r-60.6%
+-inverses60.6%
+-lft-identity60.6%
Simplified60.6%
Taylor expanded in x around inf 0.0%
fma-def0.0%
*-commutative0.0%
metadata-eval0.0%
pow-sqr0.0%
unpow20.0%
rem-square-sqrt0.0%
unpow20.0%
rem-square-sqrt0.0%
metadata-eval0.0%
*-lft-identity0.0%
unpow20.0%
Simplified92.3%
fma-udef92.3%
Applied egg-rr92.3%
unpow392.3%
times-frac100.0%
Applied egg-rr100.0%
Final simplification99.5%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- x (sqrt (- (* x x) eps)))))
(if (<= t_0 -1e-152)
t_0
(/
eps
(fma
-0.125
(* (/ eps (* x x)) (/ eps x))
(+ (* (/ eps x) -0.5) (* x 2.0)))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -1e-152) {
tmp = t_0;
} else {
tmp = eps / fma(-0.125, ((eps / (x * x)) * (eps / x)), (((eps / x) * -0.5) + (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 <= -1e-152) tmp = t_0; else tmp = Float64(eps / fma(-0.125, Float64(Float64(eps / Float64(x * x)) * Float64(eps / x)), Float64(Float64(Float64(eps / x) * -0.5) + Float64(x * 2.0)))); 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, -1e-152], t$95$0, N[(eps / N[(-0.125 * N[(N[(eps / N[(x * x), $MachinePrecision]), $MachinePrecision] * N[(eps / x), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x - \sqrt{x \cdot x - \varepsilon}\\
\mathbf{if}\;t_0 \leq -1 \cdot 10^{-152}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\mathsf{fma}\left(-0.125, \frac{\varepsilon}{x \cdot x} \cdot \frac{\varepsilon}{x}, \frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.00000000000000007e-152Initial program 98.8%
if -1.00000000000000007e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 6.9%
flip--6.9%
div-inv6.9%
add-sqr-sqrt6.9%
sub-neg6.9%
add-sqr-sqrt3.1%
hypot-def3.1%
Applied egg-rr3.1%
associate-*r/3.1%
*-rgt-identity3.1%
associate--r-60.6%
+-inverses60.6%
+-lft-identity60.6%
Simplified60.6%
Taylor expanded in x around inf 0.0%
fma-def0.0%
*-commutative0.0%
metadata-eval0.0%
pow-sqr0.0%
unpow20.0%
rem-square-sqrt0.0%
unpow20.0%
rem-square-sqrt0.0%
metadata-eval0.0%
*-lft-identity0.0%
unpow20.0%
Simplified92.3%
fma-udef92.3%
Applied egg-rr92.3%
unpow392.3%
times-frac100.0%
Applied egg-rr100.0%
Final simplification99.3%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -1e-152) t_0 (/ eps (+ (* (/ eps x) -0.5) (* x 2.0))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -1e-152) {
tmp = t_0;
} else {
tmp = eps / (((eps / x) * -0.5) + (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 <= (-1d-152)) then
tmp = t_0
else
tmp = eps / (((eps / x) * (-0.5d0)) + (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 <= -1e-152) {
tmp = t_0;
} else {
tmp = eps / (((eps / x) * -0.5) + (x * 2.0));
}
return tmp;
}
def code(x, eps): t_0 = x - math.sqrt(((x * x) - eps)) tmp = 0 if t_0 <= -1e-152: tmp = t_0 else: tmp = eps / (((eps / x) * -0.5) + (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 <= -1e-152) tmp = t_0; else tmp = Float64(eps / Float64(Float64(Float64(eps / x) * -0.5) + 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 <= -1e-152) tmp = t_0; else tmp = eps / (((eps / x) * -0.5) + (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, -1e-152], t$95$0, N[(eps / N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $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 -1 \cdot 10^{-152}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.00000000000000007e-152Initial program 98.8%
if -1.00000000000000007e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 6.9%
flip--6.9%
div-inv6.9%
add-sqr-sqrt6.9%
sub-neg6.9%
add-sqr-sqrt3.1%
hypot-def3.1%
Applied egg-rr3.1%
associate-*r/3.1%
*-rgt-identity3.1%
associate--r-60.6%
+-inverses60.6%
+-lft-identity60.6%
Simplified60.6%
Taylor expanded in x around inf 0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt99.7%
associate-*r*99.7%
metadata-eval99.7%
associate-*r/99.7%
*-commutative99.7%
fma-def99.7%
*-commutative99.7%
Simplified99.7%
fma-udef92.3%
Applied egg-rr99.7%
Final simplification99.2%
(FPCore (x eps) :precision binary64 (if (<= x 1.55e-93) (- x (sqrt (- eps))) (/ eps (+ (* (/ eps x) -0.5) (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= 1.55e-93) {
tmp = x - sqrt(-eps);
} else {
tmp = eps / (((eps / x) * -0.5) + (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 <= 1.55d-93) then
tmp = x - sqrt(-eps)
else
tmp = eps / (((eps / x) * (-0.5d0)) + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= 1.55e-93) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = eps / (((eps / x) * -0.5) + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 1.55e-93: tmp = x - math.sqrt(-eps) else: tmp = eps / (((eps / x) * -0.5) + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (x <= 1.55e-93) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(eps / Float64(Float64(Float64(eps / x) * -0.5) + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= 1.55e-93) tmp = x - sqrt(-eps); else tmp = eps / (((eps / x) * -0.5) + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 1.55e-93], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(eps / N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.55 \cdot 10^{-93}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}\\
\end{array}
\end{array}
if x < 1.55e-93Initial program 92.8%
Taylor expanded in x around 0 92.3%
neg-mul-192.3%
Simplified92.3%
if 1.55e-93 < x Initial program 20.5%
flip--20.6%
div-inv20.5%
add-sqr-sqrt20.6%
sub-neg20.6%
add-sqr-sqrt17.2%
hypot-def17.2%
Applied egg-rr17.2%
associate-*r/17.2%
*-rgt-identity17.2%
associate--r-66.6%
+-inverses66.6%
+-lft-identity66.6%
Simplified66.6%
Taylor expanded in x around inf 0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt88.1%
associate-*r*88.1%
metadata-eval88.1%
associate-*r/88.1%
*-commutative88.1%
fma-def88.1%
*-commutative88.1%
Simplified88.1%
fma-udef87.7%
Applied egg-rr88.1%
Final simplification90.5%
(FPCore (x eps) :precision binary64 (/ eps (+ (* (/ eps x) -0.5) (* x 2.0))))
double code(double x, double eps) {
return eps / (((eps / x) * -0.5) + (x * 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (((eps / x) * (-0.5d0)) + (x * 2.0d0))
end function
public static double code(double x, double eps) {
return eps / (((eps / x) * -0.5) + (x * 2.0));
}
def code(x, eps): return eps / (((eps / x) * -0.5) + (x * 2.0))
function code(x, eps) return Float64(eps / Float64(Float64(Float64(eps / x) * -0.5) + Float64(x * 2.0))) end
function tmp = code(x, eps) tmp = eps / (((eps / x) * -0.5) + (x * 2.0)); end
code[x_, eps_] := N[(eps / N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}
\end{array}
Initial program 61.5%
flip--61.4%
div-inv61.2%
add-sqr-sqrt61.0%
sub-neg61.0%
add-sqr-sqrt59.5%
hypot-def59.5%
Applied egg-rr59.5%
associate-*r/59.5%
*-rgt-identity59.5%
associate--r-83.5%
+-inverses83.5%
+-lft-identity83.5%
Simplified83.5%
Taylor expanded in x around inf 0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt45.5%
associate-*r*45.5%
metadata-eval45.5%
associate-*r/45.5%
*-commutative45.5%
fma-def45.5%
*-commutative45.5%
Simplified45.5%
fma-udef40.2%
Applied egg-rr45.5%
Final simplification45.5%
(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 61.5%
Taylor expanded in x around inf 44.8%
Final simplification44.8%
(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 61.5%
flip--61.4%
div-inv61.2%
add-sqr-sqrt61.0%
sub-neg61.0%
add-sqr-sqrt59.5%
hypot-def59.5%
Applied egg-rr59.5%
associate-*r/59.5%
*-rgt-identity59.5%
associate--r-83.5%
+-inverses83.5%
+-lft-identity83.5%
Simplified83.5%
Taylor expanded in x around inf 0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt45.5%
associate-*r*45.5%
metadata-eval45.5%
associate-*r/45.5%
*-commutative45.5%
fma-def45.5%
*-commutative45.5%
Simplified45.5%
Taylor expanded in eps around inf 5.6%
*-commutative5.6%
Simplified5.6%
Final simplification5.6%
(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 61.5%
add-sqr-sqrt61.0%
pow261.0%
pow1/261.0%
sqrt-pow160.9%
metadata-eval60.9%
Applied egg-rr60.9%
Taylor expanded in x around inf 4.3%
distribute-rgt1-in4.3%
metadata-eval4.3%
mul0-lft4.3%
Simplified4.3%
Final simplification4.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 2023293
(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))))