
(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 (* (- 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}
Initial program 92.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt44.8%
sqrt-unprod71.2%
sqr-neg71.2%
sqrt-prod27.5%
add-sqr-sqrt50.6%
Applied egg-rr50.6%
add-sqr-sqrt27.5%
add-sqr-sqrt27.5%
sqr-neg27.5%
sqrt-unprod0.0%
add-sqr-sqrt55.0%
cancel-sign-sub-inv55.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 2e-7) (* a a) (* b (- a b))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 2e-7) {
tmp = a * a;
} else {
tmp = b * (a - b);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b * b) <= 2d-7) then
tmp = a * a
else
tmp = b * (a - b)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 2e-7) {
tmp = a * a;
} else {
tmp = b * (a - b);
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 2e-7: tmp = a * a else: tmp = b * (a - b) return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 2e-7) tmp = Float64(a * a); else tmp = Float64(b * Float64(a - b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 2e-7) tmp = a * a; else tmp = b * (a - b); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 2e-7], N[(a * a), $MachinePrecision], N[(b * N[(a - b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 2 \cdot 10^{-7}:\\
\;\;\;\;a \cdot a\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(a - b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 1.9999999999999999e-7Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt44.6%
sqrt-unprod87.4%
sqr-neg87.4%
sqrt-prod42.8%
add-sqr-sqrt80.7%
Applied egg-rr80.7%
Taylor expanded in a around inf 81.2%
Taylor expanded in a around inf 81.0%
if 1.9999999999999999e-7 < (*.f64 b b) Initial program 84.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.1%
sqrt-unprod54.5%
sqr-neg54.5%
sqrt-prod11.6%
add-sqr-sqrt19.6%
Applied egg-rr19.6%
add-sqr-sqrt11.6%
add-sqr-sqrt11.6%
sqr-neg11.6%
sqrt-unprod0.0%
add-sqr-sqrt54.6%
cancel-sign-sub-inv54.6%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 86.1%
Final simplification83.5%
(FPCore (a b) :precision binary64 (if (<= (* b b) 2e-7) (* a a) (* b (- b))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 2e-7) {
tmp = a * a;
} else {
tmp = b * -b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b * b) <= 2d-7) then
tmp = a * a
else
tmp = b * -b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 2e-7) {
tmp = a * a;
} else {
tmp = b * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 2e-7: tmp = a * a else: tmp = b * -b return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 2e-7) tmp = Float64(a * a); else tmp = Float64(b * Float64(-b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 2e-7) tmp = a * a; else tmp = b * -b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 2e-7], N[(a * a), $MachinePrecision], N[(b * (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 2 \cdot 10^{-7}:\\
\;\;\;\;a \cdot a\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 1.9999999999999999e-7Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt44.6%
sqrt-unprod87.4%
sqr-neg87.4%
sqrt-prod42.8%
add-sqr-sqrt80.7%
Applied egg-rr80.7%
Taylor expanded in a around inf 81.2%
Taylor expanded in a around inf 81.0%
if 1.9999999999999999e-7 < (*.f64 b b) Initial program 84.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.1%
sqrt-unprod54.5%
sqr-neg54.5%
sqrt-prod11.6%
add-sqr-sqrt19.6%
Applied egg-rr19.6%
add-sqr-sqrt11.6%
add-sqr-sqrt11.6%
sqr-neg11.6%
sqrt-unprod0.0%
add-sqr-sqrt54.6%
cancel-sign-sub-inv54.6%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 83.7%
neg-mul-183.7%
Simplified83.7%
Taylor expanded in a around 0 79.7%
Final simplification80.4%
(FPCore (a b) :precision binary64 (if (<= b 4.8e+241) (* a a) (* a (- b))))
double code(double a, double b) {
double tmp;
if (b <= 4.8e+241) {
tmp = a * a;
} else {
tmp = a * -b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 4.8d+241) then
tmp = a * a
else
tmp = a * -b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 4.8e+241) {
tmp = a * a;
} else {
tmp = a * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 4.8e+241: tmp = a * a else: tmp = a * -b return tmp
function code(a, b) tmp = 0.0 if (b <= 4.8e+241) tmp = Float64(a * a); else tmp = Float64(a * Float64(-b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 4.8e+241) tmp = a * a; else tmp = a * -b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 4.8e+241], N[(a * a), $MachinePrecision], N[(a * (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 4.8 \cdot 10^{+241}:\\
\;\;\;\;a \cdot a\\
\mathbf{else}:\\
\;\;\;\;a \cdot \left(-b\right)\\
\end{array}
\end{array}
if b < 4.7999999999999998e241Initial program 94.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt48.9%
sqrt-unprod77.2%
sqr-neg77.2%
sqrt-prod29.5%
add-sqr-sqrt54.7%
Applied egg-rr54.7%
Taylor expanded in a around inf 57.6%
Taylor expanded in a around inf 55.4%
if 4.7999999999999998e241 < b Initial program 66.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt0.0%
sqrt-unprod4.8%
sqr-neg4.8%
sqrt-prod4.8%
add-sqr-sqrt4.8%
Applied egg-rr4.8%
add-sqr-sqrt4.8%
add-sqr-sqrt4.8%
sqr-neg4.8%
sqrt-unprod0.0%
add-sqr-sqrt100.0%
cancel-sign-sub-inv100.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 95.2%
neg-mul-195.2%
Simplified95.2%
Taylor expanded in a around inf 34.1%
Final simplification53.6%
(FPCore (a b) :precision binary64 (* a a))
double code(double a, double b) {
return a * a;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = a * a
end function
public static double code(double a, double b) {
return a * a;
}
def code(a, b): return a * a
function code(a, b) return Float64(a * a) end
function tmp = code(a, b) tmp = a * a; end
code[a_, b_] := N[(a * a), $MachinePrecision]
\begin{array}{l}
\\
a \cdot a
\end{array}
Initial program 92.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt44.8%
sqrt-unprod71.2%
sqr-neg71.2%
sqrt-prod27.5%
add-sqr-sqrt50.6%
Applied egg-rr50.6%
Taylor expanded in a around inf 54.6%
Taylor expanded in a around inf 51.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 2024121
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:alt
(! :herbie-platform default (* (+ a b) (- a b)))
(- (* a a) (* b b)))