
(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 5 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+82) (- (* 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+82) {
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+82) 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+82], 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^{+82}:\\
\;\;\;\;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) < 5.00000000000000015e82Initial program 100.0%
if 5.00000000000000015e82 < (*.f64 b b) Initial program 83.5%
Taylor expanded in b around inf 83.5%
unpow283.5%
associate-/l*90.4%
fmm-def90.4%
metadata-eval90.4%
Simplified90.4%
*-un-lft-identity90.4%
unpow290.4%
times-frac100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 1e+88) (- (* 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) <= 1e+88) {
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) <= 1d+88) 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) <= 1e+88) {
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) <= 1e+88: 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) <= 1e+88) 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) <= 1e+88) 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], 1e+88], 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 10^{+88}:\\
\;\;\;\;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) < 9.99999999999999959e87Initial program 100.0%
if 9.99999999999999959e87 < (*.f64 b b) Initial program 83.3%
Taylor expanded in b around inf 83.3%
unpow283.3%
associate-/l*90.4%
fmm-def90.4%
metadata-eval90.4%
Simplified90.4%
fma-undefine90.4%
div-inv90.4%
associate-*r*83.3%
pow283.3%
pow-flip83.3%
metadata-eval83.3%
Applied egg-rr83.3%
metadata-eval83.3%
pow-sqr83.3%
inv-pow83.3%
inv-pow83.3%
unpow283.3%
swap-sqr100.0%
div-inv100.0%
div-inv100.0%
clear-num100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= a 1e+199) (fma a a (* b (- b))) (* (+ b a) (+ b a))))
double code(double a, double b) {
double tmp;
if (a <= 1e+199) {
tmp = fma(a, a, (b * -b));
} else {
tmp = (b + a) * (b + a);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (a <= 1e+199) 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, 1e+199], 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 10^{+199}:\\
\;\;\;\;\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 < 1.0000000000000001e199Initial program 93.9%
sqr-neg93.9%
cancel-sign-sub93.9%
fma-define96.9%
Simplified96.9%
if 1.0000000000000001e199 < a Initial program 81.5%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt59.3%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-prod40.7%
add-sqr-sqrt96.3%
Applied egg-rr96.3%
Final simplification96.9%
(FPCore (a b) :precision binary64 (if (<= a 1e+149) (- (* a a) (* b b)) (* (+ b a) (+ b a))))
double code(double a, double b) {
double tmp;
if (a <= 1e+149) {
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 <= 1d+149) 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 <= 1e+149) {
tmp = (a * a) - (b * b);
} else {
tmp = (b + a) * (b + a);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 1e+149: tmp = (a * a) - (b * b) else: tmp = (b + a) * (b + a) return tmp
function code(a, b) tmp = 0.0 if (a <= 1e+149) 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 <= 1e+149) tmp = (a * a) - (b * b); else tmp = (b + a) * (b + a); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 1e+149], 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 10^{+149}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;\left(b + a\right) \cdot \left(b + a\right)\\
\end{array}
\end{array}
if a < 1.00000000000000005e149Initial program 95.4%
if 1.00000000000000005e149 < a Initial program 75.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt64.9%
sqrt-unprod97.3%
sqr-neg97.3%
sqrt-prod32.4%
add-sqr-sqrt89.2%
Applied egg-rr89.2%
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 92.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt49.9%
sqrt-unprod73.1%
sqr-neg73.1%
sqrt-prod25.5%
add-sqr-sqrt54.1%
Applied egg-rr54.1%
Final simplification54.1%
(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 2024115
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:alt
(* (+ a b) (- a b))
(- (* a a) (* b b)))