
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{a \cdot a - b \cdot b}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{a \cdot a - b \cdot b}
\end{array}
a_m = (fabs.f64 a) b_m = (fabs.f64 b) (FPCore (a_m b_m) :precision binary64 (* (hypot (sqrt b_m) (sqrt a_m)) (sqrt (- a_m b_m))))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
return hypot(sqrt(b_m), sqrt(a_m)) * sqrt((a_m - b_m));
}
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
return Math.hypot(Math.sqrt(b_m), Math.sqrt(a_m)) * Math.sqrt((a_m - b_m));
}
a_m = math.fabs(a) b_m = math.fabs(b) def code(a_m, b_m): return math.hypot(math.sqrt(b_m), math.sqrt(a_m)) * math.sqrt((a_m - b_m))
a_m = abs(a) b_m = abs(b) function code(a_m, b_m) return Float64(hypot(sqrt(b_m), sqrt(a_m)) * sqrt(Float64(a_m - b_m))) end
a_m = abs(a); b_m = abs(b); function tmp = code(a_m, b_m) tmp = hypot(sqrt(b_m), sqrt(a_m)) * sqrt((a_m - b_m)); end
a_m = N[Abs[a], $MachinePrecision] b_m = N[Abs[b], $MachinePrecision] code[a$95$m_, b$95$m_] := N[(N[Sqrt[N[Sqrt[b$95$m], $MachinePrecision] ^ 2 + N[Sqrt[a$95$m], $MachinePrecision] ^ 2], $MachinePrecision] * N[Sqrt[N[(a$95$m - b$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
\mathsf{hypot}\left(\sqrt{b\_m}, \sqrt{a\_m}\right) \cdot \sqrt{a\_m - b\_m}
\end{array}
Initial program 52.9%
pow1/252.9%
difference-of-squares53.4%
unpow-prod-down48.9%
Applied egg-rr48.9%
unpow1/248.9%
unpow1/248.9%
Simplified48.9%
+-commutative48.9%
add-sqr-sqrt22.9%
add-sqr-sqrt22.9%
hypot-define22.9%
Applied egg-rr22.9%
a_m = (fabs.f64 a) b_m = (fabs.f64 b) (FPCore (a_m b_m) :precision binary64 (* (sqrt (- a_m b_m)) (sqrt (+ b_m a_m))))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
return sqrt((a_m - b_m)) * sqrt((b_m + a_m));
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
real(8), intent (in) :: a_m
real(8), intent (in) :: b_m
code = sqrt((a_m - b_m)) * sqrt((b_m + a_m))
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
return Math.sqrt((a_m - b_m)) * Math.sqrt((b_m + a_m));
}
a_m = math.fabs(a) b_m = math.fabs(b) def code(a_m, b_m): return math.sqrt((a_m - b_m)) * math.sqrt((b_m + a_m))
a_m = abs(a) b_m = abs(b) function code(a_m, b_m) return Float64(sqrt(Float64(a_m - b_m)) * sqrt(Float64(b_m + a_m))) end
a_m = abs(a); b_m = abs(b); function tmp = code(a_m, b_m) tmp = sqrt((a_m - b_m)) * sqrt((b_m + a_m)); end
a_m = N[Abs[a], $MachinePrecision] b_m = N[Abs[b], $MachinePrecision] code[a$95$m_, b$95$m_] := N[(N[Sqrt[N[(a$95$m - b$95$m), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(b$95$m + a$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
\sqrt{a\_m - b\_m} \cdot \sqrt{b\_m + a\_m}
\end{array}
Initial program 52.9%
pow1/252.9%
difference-of-squares53.4%
unpow-prod-down48.9%
Applied egg-rr48.9%
unpow1/248.9%
unpow1/248.9%
Simplified48.9%
Final simplification48.9%
a_m = (fabs.f64 a) b_m = (fabs.f64 b) (FPCore (a_m b_m) :precision binary64 a_m)
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
return a_m;
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
real(8), intent (in) :: a_m
real(8), intent (in) :: b_m
code = a_m
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
return a_m;
}
a_m = math.fabs(a) b_m = math.fabs(b) def code(a_m, b_m): return a_m
a_m = abs(a) b_m = abs(b) function code(a_m, b_m) return a_m end
a_m = abs(a); b_m = abs(b); function tmp = code(a_m, b_m) tmp = a_m; end
a_m = N[Abs[a], $MachinePrecision] b_m = N[Abs[b], $MachinePrecision] code[a$95$m_, b$95$m_] := a$95$m
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|
\\
a\_m
\end{array}
Initial program 52.9%
Taylor expanded in a around inf 49.4%
(FPCore (a b) :precision binary64 (* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b)))))
double code(double a, double b) {
return sqrt((fabs(a) + fabs(b))) * sqrt((fabs(a) - fabs(b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt((abs(a) + abs(b))) * sqrt((abs(a) - abs(b)))
end function
public static double code(double a, double b) {
return Math.sqrt((Math.abs(a) + Math.abs(b))) * Math.sqrt((Math.abs(a) - Math.abs(b)));
}
def code(a, b): return math.sqrt((math.fabs(a) + math.fabs(b))) * math.sqrt((math.fabs(a) - math.fabs(b)))
function code(a, b) return Float64(sqrt(Float64(abs(a) + abs(b))) * sqrt(Float64(abs(a) - abs(b)))) end
function tmp = code(a, b) tmp = sqrt((abs(a) + abs(b))) * sqrt((abs(a) - abs(b))); end
code[a_, b_] := N[(N[Sqrt[N[(N[Abs[a], $MachinePrecision] + N[Abs[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(N[Abs[a], $MachinePrecision] - N[Abs[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|a\right| + \left|b\right|} \cdot \sqrt{\left|a\right| - \left|b\right|}
\end{array}
herbie shell --seed 2024144
(FPCore (a b)
:name "bug366, discussion (missed optimization)"
:precision binary64
:alt
(! :herbie-platform default (let* ((fa (fabs a)) (fb (fabs b))) (* (sqrt (+ fa fb)) (sqrt (- fa fb)))))
(sqrt (- (* a a) (* b b))))