
(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 5 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 (* (/ (+ b a_m) (sqrt (+ b a_m))) (sqrt (- a_m b))))
a_m = fabs(a);
double code(double a_m, double b) {
return ((b + a_m) / 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 = ((b + a_m) / 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 ((b + a_m) / Math.sqrt((b + a_m))) * Math.sqrt((a_m - b));
}
a_m = math.fabs(a) def code(a_m, b): return ((b + a_m) / math.sqrt((b + a_m))) * math.sqrt((a_m - b))
a_m = abs(a) function code(a_m, b) return Float64(Float64(Float64(b + a_m) / sqrt(Float64(b + a_m))) * sqrt(Float64(a_m - b))) end
a_m = abs(a); function tmp = code(a_m, b) tmp = ((b + a_m) / sqrt((b + a_m))) * sqrt((a_m - b)); end
a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b_] := N[(N[(N[(b + a$95$m), $MachinePrecision] / N[Sqrt[N[(b + a$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(a$95$m - b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
\frac{b + a\_m}{\sqrt{b + a\_m}} \cdot \sqrt{a\_m - b}
\end{array}
Initial program 56.9%
pow1/256.9%
difference-of-squares57.3%
unpow-prod-down53.9%
Applied egg-rr53.9%
unpow1/253.9%
unpow1/253.9%
+-commutative53.9%
Simplified53.9%
add-sqr-sqrt53.8%
pow253.8%
pow1/253.8%
sqrt-pow153.8%
metadata-eval53.8%
Applied egg-rr53.8%
pow-pow53.9%
metadata-eval53.9%
pow1/253.9%
flip-+31.5%
sqrt-div0.0%
difference-of-squares0.0%
sub-neg0.0%
mul-1-neg0.0%
add-sqr-sqrt0.0%
sqrt-unprod0.0%
mul-1-neg0.0%
mul-1-neg0.0%
sqr-neg0.0%
sqrt-prod0.0%
add-sqr-sqrt0.0%
sqrt-unprod0.0%
add-sqr-sqrt0.0%
+-commutative0.0%
sub-neg0.0%
mul-1-neg0.0%
add-sqr-sqrt0.0%
sqrt-unprod30.8%
mul-1-neg30.8%
mul-1-neg30.8%
sqr-neg30.8%
Applied egg-rr54.3%
+-commutative54.3%
+-commutative54.3%
Simplified54.3%
Final simplification54.3%
a_m = (fabs.f64 a) (FPCore (a_m b) :precision binary64 (/ (sqrt (- a_m b)) (pow (+ b a_m) -0.5)))
a_m = fabs(a);
double code(double a_m, double b) {
return sqrt((a_m - b)) / pow((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 = sqrt((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 Math.sqrt((a_m - b)) / Math.pow((b + a_m), -0.5);
}
a_m = math.fabs(a) def code(a_m, b): return math.sqrt((a_m - b)) / math.pow((b + a_m), -0.5)
a_m = abs(a) function code(a_m, b) return Float64(sqrt(Float64(a_m - b)) / (Float64(b + a_m) ^ -0.5)) end
a_m = abs(a); function tmp = code(a_m, b) tmp = sqrt((a_m - b)) / ((b + a_m) ^ -0.5); end
a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b_] := N[(N[Sqrt[N[(a$95$m - b), $MachinePrecision]], $MachinePrecision] / N[Power[N[(b + a$95$m), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
\frac{\sqrt{a\_m - b}}{{\left(b + a\_m\right)}^{-0.5}}
\end{array}
Initial program 56.9%
pow1/256.9%
difference-of-squares57.3%
unpow-prod-down53.9%
Applied egg-rr53.9%
unpow1/253.9%
unpow1/253.9%
+-commutative53.9%
Simplified53.9%
add-sqr-sqrt53.8%
pow253.8%
pow1/253.8%
sqrt-pow153.8%
metadata-eval53.8%
Applied egg-rr53.8%
pow-pow53.9%
metadata-eval53.9%
pow1/253.9%
flip-+31.5%
sqrt-div0.0%
difference-of-squares0.0%
sub-neg0.0%
mul-1-neg0.0%
add-sqr-sqrt0.0%
sqrt-unprod0.0%
mul-1-neg0.0%
mul-1-neg0.0%
sqr-neg0.0%
sqrt-prod0.0%
add-sqr-sqrt0.0%
sqrt-unprod0.0%
add-sqr-sqrt0.0%
+-commutative0.0%
sub-neg0.0%
mul-1-neg0.0%
add-sqr-sqrt0.0%
sqrt-unprod30.8%
mul-1-neg30.8%
mul-1-neg30.8%
sqr-neg30.8%
Applied egg-rr54.3%
+-commutative54.3%
+-commutative54.3%
Simplified54.3%
*-commutative54.3%
clear-num54.1%
un-div-inv54.2%
pow1/254.2%
pow154.2%
pow-div54.1%
metadata-eval54.1%
Applied egg-rr54.1%
Final simplification54.1%
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 56.9%
pow1/256.9%
difference-of-squares57.3%
unpow-prod-down53.9%
Applied egg-rr53.9%
unpow1/253.9%
unpow1/253.9%
+-commutative53.9%
Simplified53.9%
Final simplification53.9%
a_m = (fabs.f64 a) (FPCore (a_m b) :precision binary64 (+ a_m (* (* b -0.5) (/ b a_m))))
a_m = fabs(a);
double code(double a_m, double b) {
return a_m + ((b * -0.5) * (b / 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 + ((b * (-0.5d0)) * (b / a_m))
end function
a_m = Math.abs(a);
public static double code(double a_m, double b) {
return a_m + ((b * -0.5) * (b / a_m));
}
a_m = math.fabs(a) def code(a_m, b): return a_m + ((b * -0.5) * (b / a_m))
a_m = abs(a) function code(a_m, b) return Float64(a_m + Float64(Float64(b * -0.5) * Float64(b / a_m))) end
a_m = abs(a); function tmp = code(a_m, b) tmp = a_m + ((b * -0.5) * (b / a_m)); end
a_m = N[Abs[a], $MachinePrecision] code[a$95$m_, b_] := N[(a$95$m + N[(N[(b * -0.5), $MachinePrecision] * N[(b / a$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
a\_m + \left(b \cdot -0.5\right) \cdot \frac{b}{a\_m}
\end{array}
Initial program 56.9%
pow1/256.9%
difference-of-squares57.3%
unpow-prod-down53.9%
Applied egg-rr53.9%
unpow1/253.9%
unpow1/253.9%
+-commutative53.9%
Simplified53.9%
add-cbrt-cube38.8%
add-sqr-sqrt38.9%
cbrt-prod53.6%
Applied egg-rr53.6%
Taylor expanded in b around 0 49.8%
Simplified54.3%
Final simplification54.3%
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 56.9%
Taylor expanded in a around inf 53.8%
Final simplification53.8%
(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 2024078
(FPCore (a b)
:name "bug366, discussion (missed optimization)"
:precision binary64
:alt
(* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b))))
(sqrt (- (* a a) (* b b))))