
(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
(let* ((t_0 (- x (sqrt (- (* x x) eps)))))
(if (<= t_0 -1e-151)
t_0
(/
eps
(+
x
(+ x (fma (/ eps x) -0.5 (* (/ eps x) (/ (* eps -0.125) (* x x))))))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -1e-151) {
tmp = t_0;
} else {
tmp = eps / (x + (x + fma((eps / x), -0.5, ((eps / x) * ((eps * -0.125) / (x * x))))));
}
return tmp;
}
function code(x, eps) t_0 = Float64(x - sqrt(Float64(Float64(x * x) - eps))) tmp = 0.0 if (t_0 <= -1e-151) tmp = t_0; else tmp = Float64(eps / Float64(x + Float64(x + fma(Float64(eps / x), -0.5, Float64(Float64(eps / x) * Float64(Float64(eps * -0.125) / Float64(x * x))))))); 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-151], t$95$0, N[(eps / N[(x + N[(x + N[(N[(eps / x), $MachinePrecision] * -0.5 + N[(N[(eps / x), $MachinePrecision] * N[(N[(eps * -0.125), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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^{-151}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{x + \left(x + \mathsf{fma}\left(\frac{\varepsilon}{x}, -0.5, \frac{\varepsilon}{x} \cdot \frac{\varepsilon \cdot -0.125}{x \cdot x}\right)\right)}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -9.9999999999999994e-152Initial program 99.9%
if -9.9999999999999994e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 9.2%
flip--9.3%
div-inv9.3%
add-sqr-sqrt9.3%
sub-neg9.3%
add-sqr-sqrt3.7%
hypot-def3.7%
Applied egg-rr3.7%
associate-*r/3.7%
*-rgt-identity3.7%
associate--r-53.4%
+-inverses53.4%
+-lft-identity53.4%
Simplified53.4%
Taylor expanded in x around inf 0.0%
Simplified92.2%
associate-*r*92.2%
unpow392.2%
times-frac99.6%
Applied egg-rr99.6%
Final simplification99.8%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -1e-151) 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-151) {
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-151)) 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-151) {
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-151: 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-151) 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-151) 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-151], 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^{-151}:\\
\;\;\;\;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))) < -9.9999999999999994e-152Initial program 99.9%
if -9.9999999999999994e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 9.2%
flip--9.3%
div-inv9.3%
add-sqr-sqrt9.3%
sub-neg9.3%
add-sqr-sqrt3.7%
hypot-def3.7%
Applied egg-rr3.7%
associate-*r/3.7%
*-rgt-identity3.7%
associate--r-53.4%
+-inverses53.4%
+-lft-identity53.4%
Simplified53.4%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt99.2%
associate-*r*99.2%
metadata-eval99.2%
*-commutative99.2%
associate-*l/99.2%
fma-udef99.2%
Simplified99.2%
Taylor expanded in x around 0 99.3%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (if (<= x 2.2e-103) (- x (sqrt (- eps))) (/ eps (+ (* (/ eps x) -0.5) (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= 2.2e-103) {
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 <= 2.2d-103) 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 <= 2.2e-103) {
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 <= 2.2e-103: 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 <= 2.2e-103) 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 <= 2.2e-103) 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, 2.2e-103], 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 2.2 \cdot 10^{-103}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}\\
\end{array}
\end{array}
if x < 2.1999999999999999e-103Initial program 95.7%
Taylor expanded in x around 0 92.6%
neg-mul-192.6%
Simplified92.6%
if 2.1999999999999999e-103 < x Initial program 19.9%
flip--19.9%
div-inv19.8%
add-sqr-sqrt19.9%
sub-neg19.9%
add-sqr-sqrt15.9%
hypot-def15.9%
Applied egg-rr15.9%
associate-*r/15.9%
*-rgt-identity15.9%
associate--r-59.5%
+-inverses59.5%
+-lft-identity59.5%
Simplified59.5%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt87.3%
associate-*r*87.3%
metadata-eval87.3%
*-commutative87.3%
associate-*l/87.3%
fma-udef87.3%
Simplified87.3%
Taylor expanded in x around 0 87.4%
Final simplification90.4%
(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 63.4%
flip--63.4%
div-inv63.2%
add-sqr-sqrt63.0%
sub-neg63.0%
add-sqr-sqrt60.8%
hypot-def60.8%
Applied egg-rr60.8%
associate-*r/60.8%
*-rgt-identity60.8%
associate--r-80.9%
+-inverses80.9%
+-lft-identity80.9%
Simplified80.9%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt43.6%
associate-*r*43.6%
metadata-eval43.6%
*-commutative43.6%
associate-*l/43.6%
fma-udef43.6%
Simplified43.6%
Taylor expanded in x around 0 43.6%
Final simplification43.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 63.4%
Taylor expanded in x around inf 42.8%
Final simplification42.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 63.4%
flip--63.4%
div-inv63.2%
add-sqr-sqrt63.0%
sub-neg63.0%
add-sqr-sqrt60.8%
hypot-def60.8%
Applied egg-rr60.8%
associate-*r/60.8%
*-rgt-identity60.8%
associate--r-80.9%
+-inverses80.9%
+-lft-identity80.9%
Simplified80.9%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt43.6%
associate-*r*43.6%
metadata-eval43.6%
*-commutative43.6%
associate-*l/43.6%
fma-udef43.6%
Simplified43.6%
Taylor expanded in eps around inf 5.3%
*-commutative5.3%
Simplified5.3%
Final simplification5.3%
(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 63.4%
Taylor expanded in x around 0 59.5%
neg-mul-159.5%
Simplified59.5%
Taylor expanded in x around inf 3.5%
Final simplification3.5%
(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 2023297
(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))))