
(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 4 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}
(FPCore (a b) :precision binary64 (if (<= a -5e-271) (- a) (* (sqrt (- a b)) (sqrt (+ a b)))))
double code(double a, double b) {
double tmp;
if (a <= -5e-271) {
tmp = -a;
} else {
tmp = sqrt((a - b)) * sqrt((a + b));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-5d-271)) then
tmp = -a
else
tmp = sqrt((a - b)) * sqrt((a + b))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -5e-271) {
tmp = -a;
} else {
tmp = Math.sqrt((a - b)) * Math.sqrt((a + b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -5e-271: tmp = -a else: tmp = math.sqrt((a - b)) * math.sqrt((a + b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -5e-271) tmp = Float64(-a); else tmp = Float64(sqrt(Float64(a - b)) * sqrt(Float64(a + b))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -5e-271) tmp = -a; else tmp = sqrt((a - b)) * sqrt((a + b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -5e-271], (-a), N[(N[Sqrt[N[(a - b), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -5 \cdot 10^{-271}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;\sqrt{a - b} \cdot \sqrt{a + b}\\
\end{array}
\end{array}
if a < -5.0000000000000002e-271Initial program 57.9%
difference-of-squares58.4%
Simplified58.4%
Taylor expanded in a around inf 1.6%
fma-def1.6%
distribute-rgt1-in1.6%
metadata-eval1.6%
mul0-lft1.6%
fma-udef1.6%
metadata-eval1.6%
+-lft-identity1.6%
Simplified1.1%
frac-2neg1.1%
div-inv1.1%
distribute-neg-frac1.1%
Applied egg-rr1.1%
un-div-inv1.1%
associate-/r/1.1%
frac-2neg1.1%
*-commutative1.1%
add-sqr-sqrt0.0%
sqrt-unprod57.5%
sqr-neg57.5%
sqrt-unprod98.5%
add-sqr-sqrt99.1%
fma-neg99.1%
*-commutative99.1%
*-commutative99.1%
associate-*l*99.1%
Applied egg-rr99.1%
Taylor expanded in b around 0 99.2%
neg-mul-199.2%
Simplified99.2%
if -5.0000000000000002e-271 < a Initial program 55.6%
difference-of-squares56.2%
Simplified56.2%
*-commutative56.2%
sqrt-prod99.2%
Applied egg-rr99.2%
Final simplification99.2%
(FPCore (a b) :precision binary64 (if (<= a -5e-271) (- a) (+ a (* (* b (/ b a)) -0.5))))
double code(double a, double b) {
double tmp;
if (a <= -5e-271) {
tmp = -a;
} else {
tmp = a + ((b * (b / a)) * -0.5);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-5d-271)) then
tmp = -a
else
tmp = a + ((b * (b / a)) * (-0.5d0))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -5e-271) {
tmp = -a;
} else {
tmp = a + ((b * (b / a)) * -0.5);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -5e-271: tmp = -a else: tmp = a + ((b * (b / a)) * -0.5) return tmp
function code(a, b) tmp = 0.0 if (a <= -5e-271) tmp = Float64(-a); else tmp = Float64(a + Float64(Float64(b * Float64(b / a)) * -0.5)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -5e-271) tmp = -a; else tmp = a + ((b * (b / a)) * -0.5); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -5e-271], (-a), N[(a + N[(N[(b * N[(b / a), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -5 \cdot 10^{-271}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a + \left(b \cdot \frac{b}{a}\right) \cdot -0.5\\
\end{array}
\end{array}
if a < -5.0000000000000002e-271Initial program 57.9%
difference-of-squares58.4%
Simplified58.4%
Taylor expanded in a around inf 1.6%
fma-def1.6%
distribute-rgt1-in1.6%
metadata-eval1.6%
mul0-lft1.6%
fma-udef1.6%
metadata-eval1.6%
+-lft-identity1.6%
Simplified1.1%
frac-2neg1.1%
div-inv1.1%
distribute-neg-frac1.1%
Applied egg-rr1.1%
un-div-inv1.1%
associate-/r/1.1%
frac-2neg1.1%
*-commutative1.1%
add-sqr-sqrt0.0%
sqrt-unprod57.5%
sqr-neg57.5%
sqrt-unprod98.5%
add-sqr-sqrt99.1%
fma-neg99.1%
*-commutative99.1%
*-commutative99.1%
associate-*l*99.1%
Applied egg-rr99.1%
Taylor expanded in b around 0 99.2%
neg-mul-199.2%
Simplified99.2%
if -5.0000000000000002e-271 < a Initial program 55.6%
difference-of-squares56.2%
Simplified56.2%
Taylor expanded in a around inf 91.7%
fma-def91.7%
distribute-rgt1-in91.7%
metadata-eval91.7%
mul0-lft91.7%
fma-udef91.7%
metadata-eval91.7%
+-lft-identity91.7%
Simplified99.1%
fma-udef99.1%
*-commutative99.1%
associate-/r/99.1%
*-commutative99.1%
Applied egg-rr99.1%
Final simplification99.1%
(FPCore (a b) :precision binary64 (if (<= a -5e-271) (- a) a))
double code(double a, double b) {
double tmp;
if (a <= -5e-271) {
tmp = -a;
} else {
tmp = a;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-5d-271)) then
tmp = -a
else
tmp = a
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -5e-271) {
tmp = -a;
} else {
tmp = a;
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -5e-271: tmp = -a else: tmp = a return tmp
function code(a, b) tmp = 0.0 if (a <= -5e-271) tmp = Float64(-a); else tmp = a; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -5e-271) tmp = -a; else tmp = a; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -5e-271], (-a), a]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -5 \cdot 10^{-271}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a\\
\end{array}
\end{array}
if a < -5.0000000000000002e-271Initial program 57.9%
difference-of-squares58.4%
Simplified58.4%
Taylor expanded in a around inf 1.6%
fma-def1.6%
distribute-rgt1-in1.6%
metadata-eval1.6%
mul0-lft1.6%
fma-udef1.6%
metadata-eval1.6%
+-lft-identity1.6%
Simplified1.1%
frac-2neg1.1%
div-inv1.1%
distribute-neg-frac1.1%
Applied egg-rr1.1%
un-div-inv1.1%
associate-/r/1.1%
frac-2neg1.1%
*-commutative1.1%
add-sqr-sqrt0.0%
sqrt-unprod57.5%
sqr-neg57.5%
sqrt-unprod98.5%
add-sqr-sqrt99.1%
fma-neg99.1%
*-commutative99.1%
*-commutative99.1%
associate-*l*99.1%
Applied egg-rr99.1%
Taylor expanded in b around 0 99.2%
neg-mul-199.2%
Simplified99.2%
if -5.0000000000000002e-271 < a Initial program 55.6%
difference-of-squares56.2%
Simplified56.2%
Taylor expanded in a around inf 98.4%
Final simplification98.8%
(FPCore (a b) :precision binary64 a)
double code(double a, double b) {
return a;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = a
end function
public static double code(double a, double b) {
return a;
}
def code(a, b): return a
function code(a, b) return a end
function tmp = code(a, b) tmp = a; end
code[a_, b_] := a
\begin{array}{l}
\\
a
\end{array}
Initial program 56.7%
difference-of-squares57.3%
Simplified57.3%
Taylor expanded in a around inf 49.0%
Final simplification49.0%
(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 2023187
(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))))