
(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) (FPCore (a_m b) :precision binary64 (* (sqrt (+ b a_m)) (sqrt (- a_m b))))
a_m = fabs(a);
double code(double a_m, double b) {
return sqrt((b + a_m)) * sqrt((a_m - b));
}
a_m = abs(a)
real(8) function code(a_m, b)
real(8), intent (in) :: a_m
real(8), intent (in) :: b
code = sqrt((b + a_m)) * sqrt((a_m - b))
end function
a_m = Math.abs(a);
public static double code(double a_m, double b) {
return Math.sqrt((b + a_m)) * Math.sqrt((a_m - b));
}
a_m = math.fabs(a) def code(a_m, b): return math.sqrt((b + a_m)) * math.sqrt((a_m - b))
a_m = abs(a) function code(a_m, b) return Float64(sqrt(Float64(b + a_m)) * sqrt(Float64(a_m - b))) end
a_m = abs(a); function tmp = code(a_m, b) tmp = sqrt((b + a_m)) * sqrt((a_m - b)); end
a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b_] := N[(N[Sqrt[N[(b + a$95$m), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(a$95$m - b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
\sqrt{b + a\_m} \cdot \sqrt{a\_m - b}
\end{array}
Initial program 54.4%
pow1/254.4%
difference-of-squares55.0%
unpow-prod-down50.0%
Applied egg-rr50.0%
unpow1/250.0%
unpow1/250.0%
+-commutative50.0%
Simplified50.0%
a_m = (fabs.f64 a) (FPCore (a_m b) :precision binary64 (+ a_m (* b (* (/ b a_m) -0.5))))
a_m = fabs(a);
double code(double a_m, double b) {
return a_m + (b * ((b / a_m) * -0.5));
}
a_m = abs(a)
real(8) function code(a_m, b)
real(8), intent (in) :: a_m
real(8), intent (in) :: b
code = a_m + (b * ((b / a_m) * (-0.5d0)))
end function
a_m = Math.abs(a);
public static double code(double a_m, double b) {
return a_m + (b * ((b / a_m) * -0.5));
}
a_m = math.fabs(a) def code(a_m, b): return a_m + (b * ((b / a_m) * -0.5))
a_m = abs(a) function code(a_m, b) return Float64(a_m + Float64(b * Float64(Float64(b / a_m) * -0.5))) end
a_m = abs(a); function tmp = code(a_m, b) tmp = a_m + (b * ((b / a_m) * -0.5)); end
a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b_] := N[(a$95$m + N[(b * N[(N[(b / a$95$m), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
a\_m + b \cdot \left(\frac{b}{a\_m} \cdot -0.5\right)
\end{array}
Initial program 54.4%
pow1/254.4%
difference-of-squares55.0%
unpow-prod-down50.0%
Applied egg-rr50.0%
unpow1/250.0%
unpow1/250.0%
+-commutative50.0%
Simplified50.0%
Taylor expanded in b around 0 49.2%
Applied egg-rr50.8%
a_m = (fabs.f64 a) (FPCore (a_m b) :precision binary64 a_m)
a_m = fabs(a);
double code(double a_m, double b) {
return a_m;
}
a_m = abs(a)
real(8) function code(a_m, b)
real(8), intent (in) :: a_m
real(8), intent (in) :: b
code = a_m
end function
a_m = Math.abs(a);
public static double code(double a_m, double b) {
return a_m;
}
a_m = math.fabs(a) def code(a_m, b): return a_m
a_m = abs(a) function code(a_m, b) return a_m end
a_m = abs(a); function tmp = code(a_m, b) tmp = a_m; end
a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b_] := a$95$m
\begin{array}{l}
a_m = \left|a\right|
\\
a\_m
\end{array}
Initial program 54.4%
Taylor expanded in a around inf 50.5%
(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 2024123
(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))))