
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
return (a * a) - (b * b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
return (a * a) - (b * b);
}
def code(a, b): return (a * a) - (b * b)
function code(a, b) return Float64(Float64(a * a) - Float64(b * b)) end
function tmp = code(a, b) tmp = (a * a) - (b * b); end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot a - b \cdot b
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
return (a * a) - (b * b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
return (a * a) - (b * b);
}
def code(a, b): return (a * a) - (b * b)
function code(a, b) return Float64(Float64(a * a) - Float64(b * b)) end
function tmp = code(a, b) tmp = (a * a) - (b * b); end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot a - b \cdot b
\end{array}
(FPCore (a b) :precision binary64 (if (<= (* b b) 5e+307) (- (* a a) (* b b)) (* (pow b 2.0) (fma a (* (/ 1.0 b) (/ a b)) -1.0))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+307) {
tmp = (a * a) - (b * b);
} else {
tmp = pow(b, 2.0) * fma(a, ((1.0 / b) * (a / b)), -1.0);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 5e+307) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64((b ^ 2.0) * fma(a, Float64(Float64(1.0 / b) * Float64(a / b)), -1.0)); end return tmp end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 5e+307], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(N[Power[b, 2.0], $MachinePrecision] * N[(a * N[(N[(1.0 / b), $MachinePrecision] * N[(a / b), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 5 \cdot 10^{+307}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;{b}^{2} \cdot \mathsf{fma}\left(a, \frac{1}{b} \cdot \frac{a}{b}, -1\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 5e307Initial program 100.0%
if 5e307 < (*.f64 b b) Initial program 71.7%
Taylor expanded in b around inf 71.7%
unpow271.7%
associate-/l*81.7%
fma-neg81.7%
metadata-eval81.7%
Simplified81.7%
*-un-lft-identity81.7%
unpow281.7%
times-frac100.0%
Applied egg-rr100.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 5e+307) (- (* a a) (* b b)) (* (pow b 2.0) (+ -1.0 (pow (/ a b) 2.0)))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+307) {
tmp = (a * a) - (b * b);
} else {
tmp = pow(b, 2.0) * (-1.0 + pow((a / b), 2.0));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b * b) <= 5d+307) then
tmp = (a * a) - (b * b)
else
tmp = (b ** 2.0d0) * ((-1.0d0) + ((a / b) ** 2.0d0))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+307) {
tmp = (a * a) - (b * b);
} else {
tmp = Math.pow(b, 2.0) * (-1.0 + Math.pow((a / b), 2.0));
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 5e+307: tmp = (a * a) - (b * b) else: tmp = math.pow(b, 2.0) * (-1.0 + math.pow((a / b), 2.0)) return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 5e+307) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64((b ^ 2.0) * Float64(-1.0 + (Float64(a / b) ^ 2.0))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 5e+307) tmp = (a * a) - (b * b); else tmp = (b ^ 2.0) * (-1.0 + ((a / b) ^ 2.0)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 5e+307], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(N[Power[b, 2.0], $MachinePrecision] * N[(-1.0 + N[Power[N[(a / b), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 5 \cdot 10^{+307}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;{b}^{2} \cdot \left(-1 + {\left(\frac{a}{b}\right)}^{2}\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 5e307Initial program 100.0%
if 5e307 < (*.f64 b b) Initial program 71.7%
Taylor expanded in b around inf 71.7%
unpow271.7%
associate-/l*81.7%
fma-neg81.7%
metadata-eval81.7%
Simplified81.7%
*-un-lft-identity81.7%
unpow281.7%
times-frac100.0%
Applied egg-rr100.0%
fma-undefine100.0%
associate-*r*100.0%
div-inv100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 5e+307) (- (* a a) (* b b)) (* (pow b 2.0) (+ -1.0 (/ (/ a b) (/ b a))))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+307) {
tmp = (a * a) - (b * b);
} else {
tmp = pow(b, 2.0) * (-1.0 + ((a / b) / (b / a)));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b * b) <= 5d+307) then
tmp = (a * a) - (b * b)
else
tmp = (b ** 2.0d0) * ((-1.0d0) + ((a / b) / (b / a)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+307) {
tmp = (a * a) - (b * b);
} else {
tmp = Math.pow(b, 2.0) * (-1.0 + ((a / b) / (b / a)));
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 5e+307: tmp = (a * a) - (b * b) else: tmp = math.pow(b, 2.0) * (-1.0 + ((a / b) / (b / a))) return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 5e+307) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64((b ^ 2.0) * Float64(-1.0 + Float64(Float64(a / b) / Float64(b / a)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 5e+307) tmp = (a * a) - (b * b); else tmp = (b ^ 2.0) * (-1.0 + ((a / b) / (b / a))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 5e+307], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(N[Power[b, 2.0], $MachinePrecision] * N[(-1.0 + N[(N[(a / b), $MachinePrecision] / N[(b / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 5 \cdot 10^{+307}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;{b}^{2} \cdot \left(-1 + \frac{\frac{a}{b}}{\frac{b}{a}}\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 5e307Initial program 100.0%
if 5e307 < (*.f64 b b) Initial program 71.7%
Taylor expanded in b around inf 71.7%
unpow271.7%
associate-/l*81.7%
fma-neg81.7%
metadata-eval81.7%
Simplified81.7%
*-un-lft-identity81.7%
unpow281.7%
times-frac100.0%
Applied egg-rr100.0%
fma-undefine100.0%
associate-*r*100.0%
div-inv100.0%
pow2100.0%
Applied egg-rr100.0%
unpow2100.0%
clear-num100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= a 3.2e+208) (fma a a (* b (- b))) (* (+ b a) (+ b a))))
double code(double a, double b) {
double tmp;
if (a <= 3.2e+208) {
tmp = fma(a, a, (b * -b));
} else {
tmp = (b + a) * (b + a);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (a <= 3.2e+208) tmp = fma(a, a, Float64(b * Float64(-b))); else tmp = Float64(Float64(b + a) * Float64(b + a)); end return tmp end
code[a_, b_] := If[LessEqual[a, 3.2e+208], N[(a * a + N[(b * (-b)), $MachinePrecision]), $MachinePrecision], N[(N[(b + a), $MachinePrecision] * N[(b + a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 3.2 \cdot 10^{+208}:\\
\;\;\;\;\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(b + a\right) \cdot \left(b + a\right)\\
\end{array}
\end{array}
if a < 3.2000000000000001e208Initial program 94.5%
sqr-neg94.5%
cancel-sign-sub94.5%
fma-define96.6%
Simplified96.6%
if 3.2000000000000001e208 < a Initial program 77.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt44.4%
sqrt-unprod94.4%
sqr-neg94.4%
sqrt-prod50.0%
add-sqr-sqrt94.4%
Applied egg-rr94.4%
Final simplification96.5%
(FPCore (a b) :precision binary64 (if (<= a 7.5e+161) (- (* a a) (* b b)) (* (+ b a) (+ b a))))
double code(double a, double b) {
double tmp;
if (a <= 7.5e+161) {
tmp = (a * a) - (b * b);
} else {
tmp = (b + a) * (b + a);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= 7.5d+161) then
tmp = (a * a) - (b * b)
else
tmp = (b + a) * (b + a)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= 7.5e+161) {
tmp = (a * a) - (b * b);
} else {
tmp = (b + a) * (b + a);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 7.5e+161: tmp = (a * a) - (b * b) else: tmp = (b + a) * (b + a) return tmp
function code(a, b) tmp = 0.0 if (a <= 7.5e+161) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64(Float64(b + a) * Float64(b + a)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 7.5e+161) tmp = (a * a) - (b * b); else tmp = (b + a) * (b + a); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 7.5e+161], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(N[(b + a), $MachinePrecision] * N[(b + a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 7.5 \cdot 10^{+161}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;\left(b + a\right) \cdot \left(b + a\right)\\
\end{array}
\end{array}
if a < 7.4999999999999995e161Initial program 94.4%
if 7.4999999999999995e161 < a Initial program 82.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt43.5%
sqrt-unprod95.7%
sqr-neg95.7%
sqrt-prod52.2%
add-sqr-sqrt95.7%
Applied egg-rr95.7%
Final simplification94.5%
(FPCore (a b) :precision binary64 (* (+ b a) (+ b a)))
double code(double a, double b) {
return (b + a) * (b + a);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (b + a) * (b + a)
end function
public static double code(double a, double b) {
return (b + a) * (b + a);
}
def code(a, b): return (b + a) * (b + a)
function code(a, b) return Float64(Float64(b + a) * Float64(b + a)) end
function tmp = code(a, b) tmp = (b + a) * (b + a); end
code[a_, b_] := N[(N[(b + a), $MachinePrecision] * N[(b + a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(b + a\right) \cdot \left(b + a\right)
\end{array}
Initial program 93.4%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt49.5%
sqrt-unprod71.9%
sqr-neg71.9%
sqrt-prod25.4%
add-sqr-sqrt54.3%
Applied egg-rr54.3%
Final simplification54.3%
(FPCore (a b) :precision binary64 (* (+ a b) (- a b)))
double code(double a, double b) {
return (a + b) * (a - b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (a + b) * (a - b)
end function
public static double code(double a, double b) {
return (a + b) * (a - b);
}
def code(a, b): return (a + b) * (a - b)
function code(a, b) return Float64(Float64(a + b) * Float64(a - b)) end
function tmp = code(a, b) tmp = (a + b) * (a - b); end
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(a + b\right) \cdot \left(a - b\right)
\end{array}
herbie shell --seed 2024103
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:alt
(* (+ a b) (- a b))
(- (* a a) (* b b)))