| Alternative 1 | |
|---|---|
| Error | 0.7 |
| Cost | 260 |
\[\begin{array}{l}
\mathbf{if}\;a \leq -1.8646666152228868 \cdot 10^{-290}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a\\
\end{array}
\]
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
(FPCore (a b) :precision binary64 (if (<= a -1.8646666152228868e-290) (- a) (+ (* 0.5 (+ 0.0 (* (/ b a) (- b)))) a)))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
double code(double a, double b) {
double tmp;
if (a <= -1.8646666152228868e-290) {
tmp = -a;
} else {
tmp = (0.5 * (0.0 + ((b / a) * -b))) + a;
}
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 <= (-1.8646666152228868d-290)) then
tmp = -a
else
tmp = (0.5d0 * (0.0d0 + ((b / a) * -b))) + a
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 <= -1.8646666152228868e-290) {
tmp = -a;
} else {
tmp = (0.5 * (0.0 + ((b / a) * -b))) + a;
}
return tmp;
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
def code(a, b): tmp = 0 if a <= -1.8646666152228868e-290: tmp = -a else: tmp = (0.5 * (0.0 + ((b / a) * -b))) + a 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 <= -1.8646666152228868e-290) tmp = Float64(-a); else tmp = Float64(Float64(0.5 * Float64(0.0 + Float64(Float64(b / a) * Float64(-b)))) + a); 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 <= -1.8646666152228868e-290) tmp = -a; else tmp = (0.5 * (0.0 + ((b / a) * -b))) + a; 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, -1.8646666152228868e-290], (-a), N[(N[(0.5 * N[(0.0 + N[(N[(b / a), $MachinePrecision] * (-b)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + a), $MachinePrecision]]
\sqrt{a \cdot a - b \cdot b}
\begin{array}{l}
\mathbf{if}\;a \leq -1.8646666152228868 \cdot 10^{-290}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(0 + \frac{b}{a} \cdot \left(-b\right)\right) + a\\
\end{array}
Results
| Original | 31.3 |
|---|---|
| Target | 0.5 |
| Herbie | 0.5 |
if a < -1.86466661522288677e-290Initial program 31.1
Simplified31.1
Taylor expanded in a around -inf 0.7
Simplified0.7
if -1.86466661522288677e-290 < a Initial program 31.5
Simplified31.5
Taylor expanded in a around inf 4.3
Simplified4.3
Applied egg-rr0.3
| Alternative 1 | |
|---|---|
| Error | 0.7 |
| Cost | 260 |
| Alternative 2 | |
|---|---|
| Error | 32.4 |
| Cost | 64 |
herbie shell --seed 2023010
(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))))