
(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 (/ 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}
Initial program 67.3%
add-sqr-sqrt67.1%
add-sqr-sqrt66.7%
difference-of-squares66.7%
pow1/266.7%
sqrt-pow166.8%
metadata-eval66.8%
pow1/266.8%
sqrt-pow166.8%
metadata-eval66.8%
Applied egg-rr66.8%
difference-of-squares66.7%
add-sqr-sqrt66.6%
flip--66.6%
pow-prod-up67.2%
metadata-eval67.2%
pow1/267.2%
pow-prod-up66.7%
metadata-eval66.7%
pow1/266.7%
add-sqr-sqrt66.7%
pow-prod-up66.8%
Applied egg-rr66.8%
unpow266.8%
unpow266.8%
associate--r-99.4%
+-inverses99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- x (sqrt (- (* x x) eps))))) (if (<= t_0 -5e-154) t_0 (/ eps (+ (* (/ eps x) -0.5) (* x 2.0))))))
double code(double x, double eps) {
double t_0 = x - sqrt(((x * x) - eps));
double tmp;
if (t_0 <= -5e-154) {
tmp = t_0;
} else {
tmp = eps / (((eps / x) * -0.5) + (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-154)) then
tmp = t_0
else
tmp = eps / (((eps / x) * (-0.5d0)) + (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-154) {
tmp = t_0;
} else {
tmp = eps / (((eps / x) * -0.5) + (x * 2.0));
}
return tmp;
}
def code(x, eps): t_0 = x - math.sqrt(((x * x) - eps)) tmp = 0 if t_0 <= -5e-154: tmp = t_0 else: tmp = eps / (((eps / x) * -0.5) + (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-154) tmp = t_0; else tmp = Float64(eps / Float64(Float64(Float64(eps / x) * -0.5) + 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-154) tmp = t_0; else tmp = eps / (((eps / x) * -0.5) + (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-154], t$95$0, N[(eps / N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $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^{-154}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}\\
\end{array}
\end{array}
if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -5.0000000000000002e-154Initial program 99.5%
if -5.0000000000000002e-154 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) Initial program 7.8%
flip--7.8%
div-inv7.8%
add-sqr-sqrt8.0%
sub-neg8.0%
add-sqr-sqrt1.9%
hypot-def1.9%
Applied egg-rr1.9%
associate-*r/1.9%
*-rgt-identity1.9%
associate--r-37.8%
+-inverses37.8%
+-lft-identity37.8%
Simplified37.8%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt98.9%
associate-*r*98.9%
metadata-eval98.9%
*-commutative98.9%
associate-*l/98.9%
Simplified98.9%
fma-udef98.9%
metadata-eval98.9%
div-inv98.9%
+-commutative98.9%
div-inv98.9%
metadata-eval98.9%
Applied egg-rr98.9%
Final simplification99.3%
(FPCore (x eps) :precision binary64 (if (<= x 1.8e-88) (- x (sqrt (- eps))) (/ eps (+ (* (/ eps x) -0.5) (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= 1.8e-88) {
tmp = x - sqrt(-eps);
} else {
tmp = eps / (((eps / x) * -0.5) + (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 <= 1.8d-88) then
tmp = x - sqrt(-eps)
else
tmp = eps / (((eps / x) * (-0.5d0)) + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= 1.8e-88) {
tmp = x - Math.sqrt(-eps);
} else {
tmp = eps / (((eps / x) * -0.5) + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= 1.8e-88: tmp = x - math.sqrt(-eps) else: tmp = eps / (((eps / x) * -0.5) + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (x <= 1.8e-88) tmp = Float64(x - sqrt(Float64(-eps))); else tmp = Float64(eps / Float64(Float64(Float64(eps / x) * -0.5) + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= 1.8e-88) tmp = x - sqrt(-eps); else tmp = eps / (((eps / x) * -0.5) + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, 1.8e-88], N[(x - N[Sqrt[(-eps)], $MachinePrecision]), $MachinePrecision], N[(eps / N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.8 \cdot 10^{-88}:\\
\;\;\;\;x - \sqrt{-\varepsilon}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}\\
\end{array}
\end{array}
if x < 1.8e-88Initial program 95.7%
Taylor expanded in x around 0 94.5%
neg-mul-194.5%
Simplified94.5%
if 1.8e-88 < x Initial program 19.8%
flip--19.8%
div-inv19.8%
add-sqr-sqrt19.9%
sub-neg19.9%
add-sqr-sqrt15.4%
hypot-def15.4%
Applied egg-rr15.4%
associate-*r/15.4%
*-rgt-identity15.4%
associate--r-46.8%
+-inverses46.8%
+-lft-identity46.8%
Simplified46.8%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt88.0%
associate-*r*88.0%
metadata-eval88.0%
*-commutative88.0%
associate-*l/88.0%
Simplified88.0%
fma-udef88.0%
metadata-eval88.0%
div-inv88.0%
+-commutative88.0%
div-inv88.0%
metadata-eval88.0%
Applied egg-rr88.0%
Final simplification92.1%
(FPCore (x eps) :precision binary64 (/ eps (+ (* (/ eps x) -0.5) (* x 2.0))))
double code(double x, double eps) {
return eps / (((eps / x) * -0.5) + (x * 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (((eps / x) * (-0.5d0)) + (x * 2.0d0))
end function
public static double code(double x, double eps) {
return eps / (((eps / x) * -0.5) + (x * 2.0));
}
def code(x, eps): return eps / (((eps / x) * -0.5) + (x * 2.0))
function code(x, eps) return Float64(eps / Float64(Float64(Float64(eps / x) * -0.5) + Float64(x * 2.0))) end
function tmp = code(x, eps) tmp = eps / (((eps / x) * -0.5) + (x * 2.0)); end
code[x_, eps_] := N[(eps / N[(N[(N[(eps / x), $MachinePrecision] * -0.5), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{\frac{\varepsilon}{x} \cdot -0.5 + x \cdot 2}
\end{array}
Initial program 67.3%
flip--67.2%
div-inv67.0%
add-sqr-sqrt66.8%
sub-neg66.8%
add-sqr-sqrt64.7%
hypot-def64.7%
Applied egg-rr64.7%
associate-*r/64.7%
*-rgt-identity64.7%
associate--r-77.6%
+-inverses77.6%
+-lft-identity77.6%
Simplified77.6%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt39.3%
associate-*r*39.3%
metadata-eval39.3%
*-commutative39.3%
associate-*l/39.3%
Simplified39.3%
fma-udef39.3%
metadata-eval39.3%
div-inv39.3%
+-commutative39.3%
div-inv39.3%
metadata-eval39.3%
Applied egg-rr39.3%
Final simplification39.3%
(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 67.3%
Taylor expanded in x around inf 38.7%
Final simplification38.7%
(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 67.3%
flip--67.2%
div-inv67.0%
add-sqr-sqrt66.8%
sub-neg66.8%
add-sqr-sqrt64.7%
hypot-def64.7%
Applied egg-rr64.7%
associate-*r/64.7%
*-rgt-identity64.7%
associate--r-77.6%
+-inverses77.6%
+-lft-identity77.6%
Simplified77.6%
Taylor expanded in x around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
associate-*r/0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt39.3%
associate-*r*39.3%
metadata-eval39.3%
*-commutative39.3%
associate-*l/39.3%
Simplified39.3%
Taylor expanded in eps around inf 5.1%
*-commutative5.1%
Simplified5.1%
Final simplification5.1%
(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 67.3%
Taylor expanded in x around inf 6.5%
Taylor expanded in x around 0 5.4%
*-commutative5.4%
Simplified5.4%
sub-neg5.4%
+-commutative5.4%
*-commutative5.4%
distribute-lft-neg-in5.4%
metadata-eval5.4%
associate-*r/5.4%
associate-*l/5.4%
*-commutative5.4%
Applied egg-rr5.4%
Taylor expanded in eps around 0 3.6%
Final simplification3.6%
(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 2023285
(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))))