
(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 9 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}
(FPCore (x eps) :precision binary64 (if (<= (- x (sqrt (- (* x x) eps))) -2e-154) (- x (sqrt (fma x x (- eps)))) (+ (* 0.125 (* (/ (* eps (/ 1.0 x)) x) (/ eps x))) (* (/ eps x) 0.5))))
double code(double x, double eps) {
double tmp;
if ((x - sqrt(((x * x) - eps))) <= -2e-154) {
tmp = x - sqrt(fma(x, x, -eps));
} else {
tmp = (0.125 * (((eps * (1.0 / x)) / x) * (eps / x))) + ((eps / x) * 0.5);
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -2e-154) tmp = Float64(x - sqrt(fma(x, x, Float64(-eps)))); else tmp = Float64(Float64(0.125 * Float64(Float64(Float64(eps * Float64(1.0 / x)) / x) * Float64(eps / x))) + Float64(Float64(eps / x) * 0.5)); end return tmp end
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -2e-154], N[(x - N[Sqrt[N[(x * x + (-eps)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.125 * N[(N[(N[(eps * N[(1.0 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] * N[(eps / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(eps / x), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x - \sqrt{x \cdot x - \varepsilon} \leq -2 \cdot 10^{-154}:\\
\;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\
\mathbf{else}:\\
\;\;\;\;0.125 \cdot \left(\frac{\varepsilon \cdot \frac{1}{x}}{x} \cdot \frac{\varepsilon}{x}\right) + \frac{\varepsilon}{x} \cdot 0.5\\
\end{array}
\end{array}
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- x (sqrt (- (* x x) eps)))))
(if (<= t_0 -2e-154)
t_0
(+ (* 0.125 (* (/ (* eps (/ 1.0 x)) x) (/ 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 <= -2e-154) {
tmp = t_0;
} else {
tmp = (0.125 * (((eps * (1.0 / x)) / x) * (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 <= (-2d-154)) then
tmp = t_0
else
tmp = (0.125d0 * (((eps * (1.0d0 / x)) / x) * (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 <= -2e-154) {
tmp = t_0;
} else {
tmp = (0.125 * (((eps * (1.0 / x)) / x) * (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 <= -2e-154: tmp = t_0 else: tmp = (0.125 * (((eps * (1.0 / x)) / x) * (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 <= -2e-154) tmp = t_0; else tmp = Float64(Float64(0.125 * Float64(Float64(Float64(eps * Float64(1.0 / x)) / x) * 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 <= -2e-154) tmp = t_0; else tmp = (0.125 * (((eps * (1.0 / x)) / x) * (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, -2e-154], t$95$0, N[(N[(0.125 * N[(N[(N[(eps * N[(1.0 / x), $MachinePrecision]), $MachinePrecision] / x), $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 -2 \cdot 10^{-154}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;0.125 \cdot \left(\frac{\varepsilon \cdot \frac{1}{x}}{x} \cdot \frac{\varepsilon}{x}\right) + \frac{\varepsilon}{x} \cdot 0.5\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (if (<= x 1.85e-110) (- x (sqrt (- eps))) (/ 1.0 (+ (/ -0.5 x) (* 2.0 (/ x eps))))))
double code(double x, double eps) {
double tmp;
if (x <= 1.85e-110) {
tmp = x - sqrt(-eps);
} else {
tmp = 1.0 / ((-0.5 / x) + (2.0 * (x / eps)));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= 1.85d-110) then
tmp = x - sqrt(-eps)
else
tmp = 1.0d0 / (((-0.5d0) / x) + (2.0d0 * (x / eps)))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= 1.85e-110) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = 1.0 / ((-0.5 / x) + (2.0 * (x / eps)));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 1.85e-110: tmp = x - math.sqrt(-eps) else: tmp = 1.0 / ((-0.5 / x) + (2.0 * (x / eps))) return tmp
function code(x, eps) tmp = 0.0 if (x <= 1.85e-110) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(1.0 / Float64(Float64(-0.5 / x) + Float64(2.0 * Float64(x / eps)))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= 1.85e-110) tmp = x - sqrt(-eps); else tmp = 1.0 / ((-0.5 / x) + (2.0 * (x / eps))); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 1.85e-110], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(-0.5 / x), $MachinePrecision] + N[(2.0 * N[(x / eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.85 \cdot 10^{-110}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{-0.5}{x} + 2 \cdot \frac{x}{\varepsilon}}\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (/ 1.0 (+ (/ -0.5 x) (* 2.0 (/ x eps)))))
double code(double x, double eps) {
return 1.0 / ((-0.5 / x) + (2.0 * (x / eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 1.0d0 / (((-0.5d0) / x) + (2.0d0 * (x / eps)))
end function
public static double code(double x, double eps) {
return 1.0 / ((-0.5 / x) + (2.0 * (x / eps)));
}
def code(x, eps): return 1.0 / ((-0.5 / x) + (2.0 * (x / eps)))
function code(x, eps) return Float64(1.0 / Float64(Float64(-0.5 / x) + Float64(2.0 * Float64(x / eps)))) end
function tmp = code(x, eps) tmp = 1.0 / ((-0.5 / x) + (2.0 * (x / eps))); end
code[x_, eps_] := N[(1.0 / N[(N[(-0.5 / x), $MachinePrecision] + N[(2.0 * N[(x / eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{-0.5}{x} + 2 \cdot \frac{x}{\varepsilon}}
\end{array}
(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}
(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}
(FPCore (x eps) :precision binary64 (/ eps x))
double code(double x, double eps) {
return eps / x;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / x
end function
public static double code(double x, double eps) {
return eps / x;
}
def code(x, eps): return eps / x
function code(x, eps) return Float64(eps / x) end
function tmp = code(x, eps) tmp = eps / x; end
code[x_, eps_] := N[(eps / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{x}
\end{array}
(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}
(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 2024006
(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))))