| Alternative 1 | |
|---|---|
| Error | 0.8 |
| Cost | 260 |
\[\begin{array}{l}
\mathbf{if}\;a \leq -7.920695166384969 \cdot 10^{-250}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a\\
\end{array}
\]
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
(FPCore (a b) :precision binary64 (if (<= a -7.920695166384969e-250) (- a) (+ a (/ (* b -0.5) (/ a b)))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
double code(double a, double b) {
double tmp;
if (a <= -7.920695166384969e-250) {
tmp = -a;
} else {
tmp = a + ((b * -0.5) / (a / b));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-7.920695166384969d-250)) then
tmp = -a
else
tmp = a + ((b * (-0.5d0)) / (a / b))
end if
code = tmp
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
public static double code(double a, double b) {
double tmp;
if (a <= -7.920695166384969e-250) {
tmp = -a;
} else {
tmp = a + ((b * -0.5) / (a / b));
}
return tmp;
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
def code(a, b): tmp = 0 if a <= -7.920695166384969e-250: tmp = -a else: tmp = a + ((b * -0.5) / (a / b)) return tmp
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function code(a, b) tmp = 0.0 if (a <= -7.920695166384969e-250) tmp = Float64(-a); else tmp = Float64(a + Float64(Float64(b * -0.5) / Float64(a / b))); end return tmp end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -7.920695166384969e-250) tmp = -a; else tmp = a + ((b * -0.5) / (a / b)); end tmp_2 = tmp; end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := If[LessEqual[a, -7.920695166384969e-250], (-a), N[(a + N[(N[(b * -0.5), $MachinePrecision] / N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\sqrt{a \cdot a - b \cdot b}
\begin{array}{l}
\mathbf{if}\;a \leq -7.920695166384969 \cdot 10^{-250}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a + \frac{b \cdot -0.5}{\frac{a}{b}}\\
\end{array}
Results
| Original | 31.1 |
|---|---|
| Target | 0.5 |
| Herbie | 0.7 |
if a < -7.92069516638496914e-250Initial program 31.1
Taylor expanded in a around -inf 0.5
Simplified0.5
[Start]0.5 | \[ -1 \cdot a
\] |
|---|---|
mul-1-neg [=>]0.5 | \[ \color{blue}{-a}
\] |
if -7.92069516638496914e-250 < a Initial program 31.2
Taylor expanded in a around inf 5.0
Simplified5.0
[Start]5.0 | \[ a + -0.5 \cdot \frac{{b}^{2}}{a}
\] |
|---|---|
unpow2 [=>]5.0 | \[ a + -0.5 \cdot \frac{\color{blue}{b \cdot b}}{a}
\] |
Applied egg-rr0.8
Final simplification0.7
| Alternative 1 | |
|---|---|
| Error | 0.8 |
| Cost | 260 |
| Alternative 2 | |
|---|---|
| Error | 31.6 |
| Cost | 64 |
herbie shell --seed 2022356
(FPCore (a b)
:name "bug366, discussion (missed optimization)"
:precision binary64
:herbie-target
(* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b))))
(sqrt (- (* a a) (* b b))))