
(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 6 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 (+ x (/ (* eps -0.5) x)))))
double code(double x, double eps) {
return eps / (x + (x + ((eps * -0.5) / x)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (x + (x + ((eps * (-0.5d0)) / x)))
end function
public static double code(double x, double eps) {
return eps / (x + (x + ((eps * -0.5) / x)));
}
def code(x, eps): return eps / (x + (x + ((eps * -0.5) / x)))
function code(x, eps) return Float64(eps / Float64(x + Float64(x + Float64(Float64(eps * -0.5) / x)))) end
function tmp = code(x, eps) tmp = eps / (x + (x + ((eps * -0.5) / x))); end
code[x_, eps_] := N[(eps / N[(x + N[(x + N[(N[(eps * -0.5), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{x + \left(x + \frac{\varepsilon \cdot -0.5}{x}\right)}
\end{array}
Initial program 58.6%
flip--58.5%
div-inv58.4%
add-sqr-sqrt58.4%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt77.2%
hypot-def77.2%
Applied egg-rr77.2%
+-inverses77.2%
+-lft-identity77.2%
associate-*r/77.4%
associate-/l*77.4%
/-rgt-identity77.4%
Simplified77.4%
Taylor expanded in x around inf 0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt50.0%
associate-*r*50.0%
metadata-eval50.0%
*-commutative50.0%
Simplified50.0%
Final simplification50.0%
(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(eps * Float64(0.5 / x)) end
function tmp = code(x, eps) tmp = eps * (0.5 / x); end
code[x_, eps_] := N[(eps * N[(0.5 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \frac{0.5}{x}
\end{array}
Initial program 58.6%
sub-neg58.6%
+-commutative58.6%
add-sqr-sqrt58.0%
distribute-rgt-neg-in58.0%
fma-def58.0%
pow1/258.0%
sqrt-pow158.1%
pow258.1%
metadata-eval58.1%
pow1/258.1%
sqrt-pow158.0%
pow258.0%
metadata-eval58.0%
Applied egg-rr58.0%
Taylor expanded in x around inf 8.5%
associate-+r+48.3%
distribute-rgt1-in48.3%
metadata-eval48.3%
mul0-lft48.3%
+-lft-identity48.3%
metadata-eval48.3%
times-frac48.3%
*-commutative48.3%
times-frac48.1%
/-rgt-identity48.1%
Simplified48.1%
Final simplification48.1%
(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 58.6%
flip--58.5%
div-inv58.4%
add-sqr-sqrt58.4%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt77.2%
hypot-def77.2%
Applied egg-rr77.2%
+-inverses77.2%
+-lft-identity77.2%
associate-*r/77.4%
associate-/l*77.4%
/-rgt-identity77.4%
Simplified77.4%
Taylor expanded in eps around 0 48.3%
associate-*r/48.3%
associate-/l*48.2%
Simplified48.2%
Final simplification48.2%
(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 58.6%
Taylor expanded in x around inf 48.3%
*-commutative48.3%
associate-*l/48.3%
Simplified48.3%
Final simplification48.3%
(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 58.6%
flip--58.5%
div-inv58.4%
add-sqr-sqrt58.4%
associate--r-99.4%
pow299.4%
pow299.4%
sub-neg99.4%
add-sqr-sqrt77.2%
hypot-def77.2%
Applied egg-rr77.2%
*-commutative77.2%
associate-/r/77.3%
+-inverses77.3%
+-commutative77.3%
Simplified77.3%
Taylor expanded in x around inf 0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt50.0%
associate-*r*50.0%
metadata-eval50.0%
*-commutative50.0%
Simplified49.9%
Taylor expanded in x around 0 5.5%
*-commutative5.5%
Simplified5.5%
Final simplification5.5%
(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 58.6%
sub-neg58.6%
+-commutative58.6%
add-sqr-sqrt58.0%
distribute-rgt-neg-in58.0%
fma-def58.0%
pow1/258.0%
sqrt-pow158.1%
pow258.1%
metadata-eval58.1%
pow1/258.1%
sqrt-pow158.0%
pow258.0%
metadata-eval58.0%
Applied egg-rr58.0%
Taylor expanded in x around inf 4.3%
distribute-rgt1-in4.3%
metadata-eval4.3%
mul0-lft4.3%
Simplified4.3%
Final simplification4.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 2024033
(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))))