
(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))) -1e-150) (/ eps (+ x (hypot x (sqrt (- eps))))) (/ eps (+ x (+ x (* (/ eps x) -0.5))))))
double code(double x, double eps) {
double tmp;
if ((x - sqrt(((x * x) - eps))) <= -1e-150) {
tmp = eps / (x + hypot(x, sqrt(-eps)));
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
public static double code(double x, double eps) {
double tmp;
if ((x - Math.sqrt(((x * x) - eps))) <= -1e-150) {
tmp = eps / (x + Math.hypot(x, Math.sqrt(-eps)));
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
def code(x, eps): tmp = 0 if (x - math.sqrt(((x * x) - eps))) <= -1e-150: tmp = eps / (x + math.hypot(x, math.sqrt(-eps))) else: tmp = eps / (x + (x + ((eps / x) * -0.5))) return tmp
function code(x, eps) tmp = 0.0 if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -1e-150) tmp = Float64(eps / Float64(x + hypot(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 - sqrt(((x * x) - eps))) <= -1e-150) tmp = eps / (x + hypot(x, sqrt(-eps))); else tmp = eps / (x + (x + ((eps / x) * -0.5))); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1e-150], N[(eps / N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $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 - \sqrt{x \cdot x - \varepsilon} \leq -1 \cdot 10^{-150}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}\\
\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.00000000000000001e-150Initial program 98.9%
flip--98.8%
div-inv98.5%
add-sqr-sqrt98.2%
associate--r-99.3%
pow299.3%
pow299.3%
sub-neg99.3%
add-sqr-sqrt99.3%
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.00000000000000001e-150 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 6.4%
flip--6.4%
div-inv6.4%
add-sqr-sqrt6.5%
associate--r-99.6%
pow299.6%
pow299.6%
sub-neg99.6%
add-sqr-sqrt49.3%
hypot-define49.3%
Applied egg-rr49.3%
*-commutative49.3%
+-inverses49.3%
+-lft-identity49.3%
associate-*l/49.5%
*-lft-identity49.5%
Simplified49.5%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt99.3%
mul-1-neg99.3%
distribute-neg-frac99.3%
distribute-rgt-neg-in99.3%
distribute-lft-neg-in99.3%
metadata-eval99.3%
*-commutative99.3%
fma-define99.3%
Simplified99.3%
fma-undefine99.3%
Applied egg-rr99.3%
Final simplification99.3%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -1e-150) 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 <= -1e-150) {
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 <= (-1d-150)) 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 <= -1e-150) {
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 <= -1e-150: 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 <= -1e-150) 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 <= -1e-150) 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, -1e-150], 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 -1 \cdot 10^{-150}:\\
\;\;\;\;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.00000000000000001e-150Initial program 98.9%
if -1.00000000000000001e-150 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 6.4%
flip--6.4%
div-inv6.4%
add-sqr-sqrt6.5%
associate--r-99.6%
pow299.6%
pow299.6%
sub-neg99.6%
add-sqr-sqrt49.3%
hypot-define49.3%
Applied egg-rr49.3%
*-commutative49.3%
+-inverses49.3%
+-lft-identity49.3%
associate-*l/49.5%
*-lft-identity49.5%
Simplified49.5%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt99.3%
mul-1-neg99.3%
distribute-neg-frac99.3%
distribute-rgt-neg-in99.3%
distribute-lft-neg-in99.3%
metadata-eval99.3%
*-commutative99.3%
fma-define99.3%
Simplified99.3%
fma-undefine99.3%
Applied egg-rr99.3%
Final simplification99.1%
(FPCore (x eps) :precision binary64 (if (<= x 1.16e-129) (- x (sqrt (- eps))) (/ eps (+ x (+ x (* (/ eps x) -0.5))))))
double code(double x, double eps) {
double tmp;
if (x <= 1.16e-129) {
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 <= 1.16d-129) 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 <= 1.16e-129) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = eps / (x + (x + ((eps / x) * -0.5)));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 1.16e-129: 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 <= 1.16e-129) 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 <= 1.16e-129) tmp = x - sqrt(-eps); else tmp = eps / (x + (x + ((eps / x) * -0.5))); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 1.16e-129], 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 1.16 \cdot 10^{-129}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{x + \left(x + \frac{\varepsilon}{x} \cdot -0.5\right)}\\
\end{array}
\end{array}
if x < 1.16e-129Initial program 97.6%
Taylor expanded in x around 0 96.6%
neg-mul-196.6%
Simplified96.6%
if 1.16e-129 < x Initial program 27.8%
flip--27.7%
div-inv27.7%
add-sqr-sqrt27.7%
associate--r-99.5%
pow299.5%
pow299.5%
sub-neg99.5%
add-sqr-sqrt63.1%
hypot-define63.1%
Applied egg-rr63.1%
*-commutative63.1%
+-inverses63.1%
+-lft-identity63.1%
associate-*l/63.3%
*-lft-identity63.3%
Simplified63.3%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt79.4%
mul-1-neg79.4%
distribute-neg-frac79.4%
distribute-rgt-neg-in79.4%
distribute-lft-neg-in79.4%
metadata-eval79.4%
*-commutative79.4%
fma-define79.4%
Simplified79.4%
fma-undefine79.4%
Applied egg-rr79.4%
Final simplification87.4%
(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 60.2%
flip--60.2%
div-inv60.0%
add-sqr-sqrt59.8%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt78.4%
hypot-define78.4%
Applied egg-rr78.4%
*-commutative78.4%
+-inverses78.4%
+-lft-identity78.4%
associate-*l/78.5%
*-lft-identity78.5%
Simplified78.5%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt46.1%
mul-1-neg46.1%
distribute-neg-frac46.1%
distribute-rgt-neg-in46.1%
distribute-lft-neg-in46.1%
metadata-eval46.1%
*-commutative46.1%
fma-define46.1%
Simplified46.1%
fma-undefine46.1%
Applied egg-rr46.1%
Final simplification46.1%
(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 60.2%
Taylor expanded in x around inf 45.6%
Final simplification45.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 60.2%
flip--60.2%
div-inv60.0%
add-sqr-sqrt59.8%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt78.4%
hypot-define78.4%
Applied egg-rr78.4%
*-commutative78.4%
+-inverses78.4%
+-lft-identity78.4%
associate-*l/78.5%
*-lft-identity78.5%
Simplified78.5%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt46.1%
mul-1-neg46.1%
distribute-neg-frac46.1%
distribute-rgt-neg-in46.1%
distribute-lft-neg-in46.1%
metadata-eval46.1%
*-commutative46.1%
fma-define46.1%
Simplified46.1%
Taylor expanded in eps around inf 5.2%
Simplified5.2%
(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 60.2%
Taylor expanded in x around inf 4.3%
Taylor expanded in x around 0 4.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 2024186
(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))))