
(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 4 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 (* (+ b a) (- a b)))
double code(double a, double b) {
return (b + a) * (a - b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (b + a) * (a - b)
end function
public static double code(double a, double b) {
return (b + a) * (a - b);
}
def code(a, b): return (b + a) * (a - b)
function code(a, b) return Float64(Float64(b + a) * Float64(a - b)) end
function tmp = code(a, b) tmp = (b + a) * (a - b); end
code[a_, b_] := N[(N[(b + a), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(b + a\right) \cdot \left(a - b\right)
\end{array}
Initial program 93.7%
pow293.7%
add-cbrt-cube82.6%
pow1/363.9%
pow-pow63.9%
pow363.9%
metadata-eval63.9%
Applied egg-rr63.9%
pow-pow93.7%
metadata-eval93.7%
pow293.7%
difference-of-squares100.0%
+-commutative100.0%
Applied egg-rr100.0%
(FPCore (a b) :precision binary64 (if (<= (* a a) 2e-69) (* b (- b)) (* a (- a b))))
double code(double a, double b) {
double tmp;
if ((a * a) <= 2e-69) {
tmp = b * -b;
} else {
tmp = a * (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 * a) <= 2d-69) then
tmp = b * -b
else
tmp = a * (a - b)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((a * a) <= 2e-69) {
tmp = b * -b;
} else {
tmp = a * (a - b);
}
return tmp;
}
def code(a, b): tmp = 0 if (a * a) <= 2e-69: tmp = b * -b else: tmp = a * (a - b) return tmp
function code(a, b) tmp = 0.0 if (Float64(a * a) <= 2e-69) tmp = Float64(b * Float64(-b)); else tmp = Float64(a * Float64(a - b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((a * a) <= 2e-69) tmp = b * -b; else tmp = a * (a - b); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(a * a), $MachinePrecision], 2e-69], N[(b * (-b)), $MachinePrecision], N[(a * N[(a - b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot a \leq 2 \cdot 10^{-69}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\mathbf{else}:\\
\;\;\;\;a \cdot \left(a - b\right)\\
\end{array}
\end{array}
if (*.f64 a a) < 1.9999999999999999e-69Initial program 100.0%
Taylor expanded in a around 0 86.0%
neg-mul-186.0%
Simplified86.0%
pow286.0%
distribute-lft-neg-in86.0%
Applied egg-rr86.0%
if 1.9999999999999999e-69 < (*.f64 a a) Initial program 88.6%
pow288.6%
add-cbrt-cube72.2%
pow1/349.1%
pow-pow49.1%
pow349.1%
metadata-eval49.1%
Applied egg-rr49.1%
pow-pow88.6%
metadata-eval88.6%
pow288.6%
difference-of-squares100.0%
+-commutative100.0%
Applied egg-rr100.0%
Taylor expanded in b around 0 77.3%
Final simplification81.3%
(FPCore (a b) :precision binary64 (if (<= (* b b) 2e-114) (* a a) (* b (- b))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 2e-114) {
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-114) 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-114) {
tmp = a * a;
} else {
tmp = b * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 2e-114: tmp = a * a else: tmp = b * -b return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 2e-114) 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-114) tmp = a * a; else tmp = b * -b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 2e-114], N[(a * a), $MachinePrecision], N[(b * (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 2 \cdot 10^{-114}:\\
\;\;\;\;a \cdot a\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 2.0000000000000001e-114Initial program 99.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.8%
sqrt-unprod96.9%
sqr-neg96.9%
sqrt-prod45.0%
add-sqr-sqrt88.0%
Applied egg-rr88.0%
Taylor expanded in a around inf 88.3%
Taylor expanded in a around inf 88.5%
if 2.0000000000000001e-114 < (*.f64 b b) Initial program 89.3%
Taylor expanded in a around 0 75.6%
neg-mul-175.6%
Simplified75.6%
pow275.6%
distribute-lft-neg-in75.6%
Applied egg-rr75.6%
Final simplification81.0%
(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 93.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.6%
sqrt-unprod71.1%
sqr-neg71.1%
sqrt-prod26.6%
add-sqr-sqrt50.4%
Applied egg-rr50.4%
Taylor expanded in a around inf 56.1%
Taylor expanded in a around inf 51.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 2024172
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:alt
(! :herbie-platform default (* (+ a b) (- a b)))
(- (* a a) (* b b)))