
(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 59.0%
flip--58.9%
div-inv58.7%
add-sqr-sqrt58.7%
associate--r-99.4%
+-commutative99.4%
*-un-lft-identity99.4%
*-un-lft-identity99.4%
distribute-rgt-out--99.4%
pow299.4%
metadata-eval99.4%
pow299.4%
Applied egg-rr99.4%
mul0-rgt99.4%
+-rgt-identity99.4%
associate-*r/99.6%
*-rgt-identity99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- x (sqrt (- (* x x) eps)))))
(if (<= t_0 -1e-152)
t_0
(+ (* 0.125 (* (/ eps (pow x 2.0)) (/ eps 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-152) {
tmp = t_0;
} else {
tmp = (0.125 * ((eps / pow(x, 2.0)) * (eps / 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-152)) then
tmp = t_0
else
tmp = (0.125d0 * ((eps / (x ** 2.0d0)) * (eps / 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-152) {
tmp = t_0;
} else {
tmp = (0.125 * ((eps / Math.pow(x, 2.0)) * (eps / 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-152: tmp = t_0 else: tmp = (0.125 * ((eps / math.pow(x, 2.0)) * (eps / 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-152) tmp = t_0; else tmp = Float64(Float64(0.125 * Float64(Float64(eps / (x ^ 2.0)) * Float64(eps / 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-152) tmp = t_0; else tmp = (0.125 * ((eps / (x ^ 2.0)) * (eps / 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-152], t$95$0, N[(N[(0.125 * N[(N[(eps / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] * N[(eps / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(eps / x), $MachinePrecision] * 0.5), $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}:\\
\;\;\;\;0.125 \cdot \left(\frac{\varepsilon}{{x}^{2}} \cdot \frac{\varepsilon}{x}\right) + \frac{\varepsilon}{x} \cdot 0.5\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.00000000000000007e-152Initial program 98.5%
if -1.00000000000000007e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 9.6%
Taylor expanded in x around inf 90.2%
unpow290.2%
unpow390.2%
unpow290.2%
times-frac98.7%
Applied egg-rr98.7%
Final simplification98.6%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -1e-152) t_0 (/ (* eps 0.5) x))))
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 * 0.5) / x;
}
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 * 0.5d0) / x
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 * 0.5) / x;
}
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 * 0.5) / x 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(Float64(eps * 0.5) / x); 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 * 0.5) / x; 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[(N[(eps * 0.5), $MachinePrecision] / x), $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 \cdot 0.5}{x}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -1.00000000000000007e-152Initial program 98.5%
if -1.00000000000000007e-152 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 9.6%
Taylor expanded in x around inf 97.0%
associate-*r/97.0%
*-commutative97.0%
Simplified97.0%
Final simplification97.8%
(FPCore (x eps) :precision binary64 (if (<= x 5.1e-116) (- x (sqrt (- eps))) (/ (* eps 0.5) x)))
double code(double x, double eps) {
double tmp;
if (x <= 5.1e-116) {
tmp = x - sqrt(-eps);
} else {
tmp = (eps * 0.5) / x;
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= 5.1d-116) then
tmp = x - sqrt(-eps)
else
tmp = (eps * 0.5d0) / x
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= 5.1e-116) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = (eps * 0.5) / x;
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 5.1e-116: tmp = x - math.sqrt(-eps) else: tmp = (eps * 0.5) / x return tmp
function code(x, eps) tmp = 0.0 if (x <= 5.1e-116) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(Float64(eps * 0.5) / x); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= 5.1e-116) tmp = x - sqrt(-eps); else tmp = (eps * 0.5) / x; end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 5.1e-116], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(N[(eps * 0.5), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.1 \cdot 10^{-116}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon \cdot 0.5}{x}\\
\end{array}
\end{array}
if x < 5.1000000000000002e-116Initial program 97.3%
Taylor expanded in x around 0 95.9%
mul-1-neg95.9%
Simplified95.9%
if 5.1000000000000002e-116 < x Initial program 24.1%
Taylor expanded in x around inf 83.2%
associate-*r/83.2%
*-commutative83.2%
Simplified83.2%
Final simplification89.2%
(FPCore (x eps) :precision binary64 (/ 0.5 (/ x eps)))
double code(double x, double eps) {
return 0.5 / (x / eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.5d0 / (x / eps)
end function
public static double code(double x, double eps) {
return 0.5 / (x / eps);
}
def code(x, eps): return 0.5 / (x / eps)
function code(x, eps) return Float64(0.5 / Float64(x / eps)) end
function tmp = code(x, eps) tmp = 0.5 / (x / eps); end
code[x_, eps_] := N[(0.5 / N[(x / eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{\frac{x}{\varepsilon}}
\end{array}
Initial program 59.0%
flip--58.9%
div-inv58.7%
add-sqr-sqrt58.7%
associate--r-99.4%
+-commutative99.4%
*-un-lft-identity99.4%
*-un-lft-identity99.4%
distribute-rgt-out--99.4%
pow299.4%
metadata-eval99.4%
pow299.4%
Applied egg-rr99.4%
mul0-rgt99.4%
+-rgt-identity99.4%
associate-*r/99.6%
*-rgt-identity99.6%
Simplified99.6%
Taylor expanded in eps around 0 47.4%
associate-*r/47.4%
associate-/l*47.3%
Simplified47.3%
Final simplification47.3%
(FPCore (x eps) :precision binary64 (/ (* eps 0.5) x))
double code(double x, double eps) {
return (eps * 0.5) / x;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (eps * 0.5d0) / x
end function
public static double code(double x, double eps) {
return (eps * 0.5) / x;
}
def code(x, eps): return (eps * 0.5) / x
function code(x, eps) return Float64(Float64(eps * 0.5) / x) end
function tmp = code(x, eps) tmp = (eps * 0.5) / x; end
code[x_, eps_] := N[(N[(eps * 0.5), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon \cdot 0.5}{x}
\end{array}
Initial program 59.0%
Taylor expanded in x around inf 47.4%
associate-*r/47.4%
*-commutative47.4%
Simplified47.4%
Final simplification47.4%
(FPCore (x eps) :precision binary64 (- x x))
double code(double x, double eps) {
return x - x;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x - x
end function
public static double code(double x, double eps) {
return x - x;
}
def code(x, eps): return x - x
function code(x, eps) return Float64(x - x) end
function tmp = code(x, eps) tmp = x - x; end
code[x_, eps_] := N[(x - x), $MachinePrecision]
\begin{array}{l}
\\
x - x
\end{array}
Initial program 59.0%
Taylor expanded in x around inf 4.3%
Final simplification4.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 59.0%
Taylor expanded in x around 0 53.9%
mul-1-neg53.9%
Simplified53.9%
Taylor expanded in x around inf 3.8%
Final simplification3.8%
(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 2023301
(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))))