
(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 (/ eps (+ x (sqrt (- (pow x 2.0) eps)))))
double code(double x, double eps) {
return eps / (x + sqrt((pow(x, 2.0) - eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (x + sqrt(((x ** 2.0d0) - eps)))
end function
public static double code(double x, double eps) {
return eps / (x + Math.sqrt((Math.pow(x, 2.0) - eps)));
}
def code(x, eps): return eps / (x + math.sqrt((math.pow(x, 2.0) - eps)))
function code(x, eps) return Float64(eps / Float64(x + sqrt(Float64((x ^ 2.0) - eps)))) end
function tmp = code(x, eps) tmp = eps / (x + sqrt(((x ^ 2.0) - eps))); end
code[x_, eps_] := N[(eps / N[(x + N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{x + \sqrt{{x}^{2} - \varepsilon}}
\end{array}
Initial program 61.8%
pow1/261.8%
add-cube-cbrt60.6%
pow360.6%
pow-pow60.6%
pow260.6%
metadata-eval60.6%
Applied egg-rr60.6%
flip--60.5%
div-inv60.5%
unpow260.5%
pow-prod-up60.6%
metadata-eval60.6%
pow360.6%
add-cube-cbrt61.0%
pow1/357.2%
pow-pow61.4%
metadata-eval61.4%
pow1/261.4%
Applied egg-rr61.4%
associate-*r/61.4%
*-rgt-identity61.4%
sub-neg61.4%
associate--r+99.5%
+-inverses99.5%
neg-sub099.5%
remove-double-neg99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (if (<= (- x (sqrt (- (* x x) eps))) -1e-155) (/ eps (+ x (hypot x (sqrt (- eps))))) (/ eps (+ (* -0.5 (/ eps x)) (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if ((x - sqrt(((x * x) - eps))) <= -1e-155) {
tmp = eps / (x + hypot(x, sqrt(-eps)));
} else {
tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
return tmp;
}
public static double code(double x, double eps) {
double tmp;
if ((x - Math.sqrt(((x * x) - eps))) <= -1e-155) {
tmp = eps / (x + Math.hypot(x, Math.sqrt(-eps)));
} else {
tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if (x - math.sqrt(((x * x) - eps))) <= -1e-155: tmp = eps / (x + math.hypot(x, math.sqrt(-eps))) else: tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -1e-155) tmp = Float64(eps / Float64(x + hypot(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 - sqrt(((x * x) - eps))) <= -1e-155) tmp = eps / (x + hypot(x, sqrt(-eps))); else tmp = eps / ((-0.5 * (eps / x)) + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1e-155], N[(eps / N[(x + N[Sqrt[x ^ 2 + N[Sqrt[(-eps)], $MachinePrecision] ^ 2], $MachinePrecision]), $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 - \sqrt{x \cdot x - \varepsilon} \leq -1 \cdot 10^{-155}:\\
\;\;\;\;\frac{\varepsilon}{x + \mathsf{hypot}\left(x, \sqrt{-\varepsilon}\right)}\\
\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))) < -1.00000000000000001e-155Initial program 97.2%
flip--97.1%
div-inv96.8%
add-sqr-sqrt96.5%
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.00000000000000001e-155 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 8.3%
flip--8.3%
div-inv8.3%
add-sqr-sqrt8.4%
associate--r-99.6%
pow299.6%
pow299.6%
sub-neg99.6%
add-sqr-sqrt39.1%
hypot-define39.1%
Applied egg-rr39.1%
*-commutative39.1%
+-inverses39.1%
+-lft-identity39.1%
associate-*l/39.2%
*-lft-identity39.2%
Simplified39.2%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-/l*0.0%
associate-*r*0.0%
*-commutative0.0%
associate-*r*0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt98.8%
metadata-eval98.8%
Simplified98.8%
Taylor expanded in eps around 0 98.8%
Final simplification99.1%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -5e-155) 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-155) {
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-155)) 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-155) {
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-155: 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-155) 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-155) 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-155], 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^{-155}:\\
\;\;\;\;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))) < -4.9999999999999999e-155Initial program 97.4%
if -4.9999999999999999e-155 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 8.9%
flip--9.0%
div-inv9.0%
add-sqr-sqrt9.1%
associate--r-99.6%
pow299.6%
pow299.6%
sub-neg99.6%
add-sqr-sqrt39.7%
hypot-define39.7%
Applied egg-rr39.7%
*-commutative39.7%
+-inverses39.7%
+-lft-identity39.7%
associate-*l/39.8%
*-lft-identity39.8%
Simplified39.8%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-/l*0.0%
associate-*r*0.0%
*-commutative0.0%
associate-*r*0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt98.7%
metadata-eval98.7%
Simplified98.7%
Taylor expanded in eps around 0 98.7%
Final simplification97.9%
(FPCore (x eps) :precision binary64 (if (<= x 6e-107) (- x (sqrt (- eps))) (/ eps (+ (* -0.5 (/ eps x)) (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= 6e-107) {
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 <= 6d-107) 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 <= 6e-107) {
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 <= 6e-107: 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 <= 6e-107) 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 <= 6e-107) 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, 6e-107], 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 6 \cdot 10^{-107}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{-0.5 \cdot \frac{\varepsilon}{x} + x \cdot 2}\\
\end{array}
\end{array}
if x < 5.9999999999999994e-107Initial program 98.2%
Taylor expanded in x around 0 97.3%
neg-mul-197.3%
Simplified97.3%
if 5.9999999999999994e-107 < 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-sqrt52.8%
hypot-define52.7%
Applied egg-rr52.7%
*-commutative52.7%
+-inverses52.7%
+-lft-identity52.7%
associate-*l/52.9%
*-lft-identity52.9%
Simplified52.9%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-/l*0.0%
associate-*r*0.0%
*-commutative0.0%
associate-*r*0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt82.3%
metadata-eval82.3%
Simplified82.3%
Taylor expanded in eps around 0 82.3%
Final simplification89.7%
(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 61.8%
flip--61.7%
div-inv61.5%
add-sqr-sqrt61.4%
associate--r-99.3%
pow299.3%
pow299.3%
sub-neg99.3%
add-sqr-sqrt75.2%
hypot-define75.2%
Applied egg-rr75.2%
*-commutative75.2%
+-inverses75.2%
+-lft-identity75.2%
associate-*l/75.3%
*-lft-identity75.3%
Simplified75.3%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-/l*0.0%
associate-*r*0.0%
*-commutative0.0%
associate-*r*0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt45.5%
metadata-eval45.5%
Simplified45.5%
Taylor expanded in eps around 0 45.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.8%
Taylor expanded in x around inf 44.6%
Final simplification44.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 61.8%
flip--61.7%
div-inv61.5%
add-sqr-sqrt61.4%
associate--r-99.3%
pow299.3%
pow299.3%
sub-neg99.3%
add-sqr-sqrt75.2%
hypot-define75.2%
Applied egg-rr75.2%
*-commutative75.2%
+-inverses75.2%
+-lft-identity75.2%
associate-*l/75.3%
*-lft-identity75.3%
Simplified75.3%
Taylor expanded in eps around 0 0.0%
+-commutative0.0%
*-commutative0.0%
fma-define0.0%
associate-/l*0.0%
associate-*r*0.0%
*-commutative0.0%
associate-*r*0.0%
associate-*r/0.0%
unpow20.0%
rem-square-sqrt45.5%
metadata-eval45.5%
Simplified45.5%
Taylor expanded in eps around inf 5.2%
*-commutative5.2%
Simplified5.2%
Final simplification5.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 61.8%
sub-neg61.8%
+-commutative61.8%
add-sqr-sqrt61.3%
distribute-rgt-neg-in61.3%
fma-define61.1%
pow1/261.1%
sqrt-pow161.2%
pow261.2%
metadata-eval61.2%
pow1/261.2%
sqrt-pow161.1%
pow261.1%
metadata-eval61.1%
Applied egg-rr61.1%
Taylor expanded in eps around 0 4.2%
distribute-rgt1-in4.2%
metadata-eval4.2%
mul0-lft4.2%
Simplified4.2%
Final simplification4.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 2024073
(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
(/ eps (+ x (sqrt (- (* x x) eps))))
(- x (sqrt (- (* x x) eps))))