
(FPCore (x y) :precision binary64 (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))
double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) + ((x * 2.0d0) * y)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
def code(x, y): return ((x * x) + ((x * 2.0) * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * x) + ((x * 2.0) * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))
double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) + ((x * 2.0d0) * y)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
def code(x, y): return ((x * x) + ((x * 2.0) * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * x) + ((x * 2.0) * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (if (<= (+ (+ (* x x) (* (* x 2.0) y)) (* y y)) 5e+242) (fma x x (* y (+ (* x 2.0) y))) (+ (* x x) (* y y))))
double code(double x, double y) {
double tmp;
if ((((x * x) + ((x * 2.0) * y)) + (y * y)) <= 5e+242) {
tmp = fma(x, x, (y * ((x * 2.0) + y)));
} else {
tmp = (x * x) + (y * y);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) <= 5e+242) tmp = fma(x, x, Float64(y * Float64(Float64(x * 2.0) + y))); else tmp = Float64(Float64(x * x) + Float64(y * y)); end return tmp end
code[x_, y_] := If[LessEqual[N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], 5e+242], N[(x * x + N[(y * N[(N[(x * 2.0), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y \leq 5 \cdot 10^{+242}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(x \cdot 2 + y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x + y \cdot y\\
\end{array}
\end{array}
if (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) < 5.0000000000000004e242Initial program 99.9%
associate-+l+99.9%
associate-*l*99.9%
*-commutative99.9%
*-commutative99.9%
+-commutative99.9%
fma-define100.0%
*-commutative100.0%
*-commutative100.0%
associate-*l*100.0%
distribute-rgt-out100.0%
+-commutative100.0%
Simplified100.0%
if 5.0000000000000004e242 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) Initial program 88.1%
+-commutative88.1%
associate-*l*88.1%
distribute-lft-out93.3%
Applied egg-rr93.3%
Taylor expanded in y around 0 100.0%
(FPCore (x y) :precision binary64 (if (<= (+ (+ (* x x) (* (* x 2.0) y)) (* y y)) 5e+242) (+ (* y y) (* x (+ x (* 2.0 y)))) (+ (* x x) (* y y))))
double code(double x, double y) {
double tmp;
if ((((x * x) + ((x * 2.0) * y)) + (y * y)) <= 5e+242) {
tmp = (y * y) + (x * (x + (2.0 * y)));
} else {
tmp = (x * x) + (y * y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((((x * x) + ((x * 2.0d0) * y)) + (y * y)) <= 5d+242) then
tmp = (y * y) + (x * (x + (2.0d0 * y)))
else
tmp = (x * x) + (y * y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((((x * x) + ((x * 2.0) * y)) + (y * y)) <= 5e+242) {
tmp = (y * y) + (x * (x + (2.0 * y)));
} else {
tmp = (x * x) + (y * y);
}
return tmp;
}
def code(x, y): tmp = 0 if (((x * x) + ((x * 2.0) * y)) + (y * y)) <= 5e+242: tmp = (y * y) + (x * (x + (2.0 * y))) else: tmp = (x * x) + (y * y) return tmp
function code(x, y) tmp = 0.0 if (Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) <= 5e+242) tmp = Float64(Float64(y * y) + Float64(x * Float64(x + Float64(2.0 * y)))); else tmp = Float64(Float64(x * x) + Float64(y * y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((((x * x) + ((x * 2.0) * y)) + (y * y)) <= 5e+242) tmp = (y * y) + (x * (x + (2.0 * y))); else tmp = (x * x) + (y * y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], 5e+242], N[(N[(y * y), $MachinePrecision] + N[(x * N[(x + N[(2.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y \leq 5 \cdot 10^{+242}:\\
\;\;\;\;y \cdot y + x \cdot \left(x + 2 \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x + y \cdot y\\
\end{array}
\end{array}
if (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) < 5.0000000000000004e242Initial program 99.9%
+-commutative99.9%
associate-*l*99.9%
distribute-lft-out100.0%
Applied egg-rr100.0%
if 5.0000000000000004e242 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) Initial program 88.1%
+-commutative88.1%
associate-*l*88.1%
distribute-lft-out93.3%
Applied egg-rr93.3%
Taylor expanded in y around 0 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= y 3.45e-84) (* y (+ (* x 2.0) y)) (* y y)))
double code(double x, double y) {
double tmp;
if (y <= 3.45e-84) {
tmp = y * ((x * 2.0) + y);
} else {
tmp = y * y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 3.45d-84) then
tmp = y * ((x * 2.0d0) + y)
else
tmp = y * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 3.45e-84) {
tmp = y * ((x * 2.0) + y);
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 3.45e-84: tmp = y * ((x * 2.0) + y) else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (y <= 3.45e-84) tmp = Float64(y * Float64(Float64(x * 2.0) + y)); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 3.45e-84) tmp = y * ((x * 2.0) + y); else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 3.45e-84], N[(y * N[(N[(x * 2.0), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.45 \cdot 10^{-84}:\\
\;\;\;\;y \cdot \left(x \cdot 2 + y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 3.44999999999999994e-84Initial program 97.1%
associate-+l+97.1%
associate-*l*97.1%
*-commutative97.1%
*-commutative97.1%
+-commutative97.1%
fma-define97.1%
*-commutative97.1%
*-commutative97.1%
associate-*l*97.1%
distribute-rgt-out98.9%
+-commutative98.9%
Simplified98.9%
Taylor expanded in x around 0 52.3%
associate-*r*52.3%
*-commutative52.3%
unpow252.3%
distribute-rgt-in54.0%
fma-undefine54.0%
Simplified54.0%
fma-undefine54.0%
Applied egg-rr54.0%
if 3.44999999999999994e-84 < y Initial program 86.4%
associate-+l+86.4%
associate-*l*86.4%
*-commutative86.4%
*-commutative86.4%
+-commutative86.4%
fma-define86.4%
*-commutative86.4%
*-commutative86.4%
associate-*l*86.4%
distribute-rgt-out93.8%
+-commutative93.8%
Simplified93.8%
Taylor expanded in x around 0 65.0%
associate-*r*65.0%
*-commutative65.0%
unpow265.0%
distribute-rgt-in72.5%
fma-undefine72.5%
Simplified72.5%
Taylor expanded in x around 0 71.8%
(FPCore (x y) :precision binary64 (+ (* x x) (* y y)))
double code(double x, double y) {
return (x * x) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + (y * y)
end function
public static double code(double x, double y) {
return (x * x) + (y * y);
}
def code(x, y): return (x * x) + (y * y)
function code(x, y) return Float64(Float64(x * x) + Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) + (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + y \cdot y
\end{array}
Initial program 93.7%
+-commutative93.7%
associate-*l*93.7%
distribute-lft-out96.5%
Applied egg-rr96.5%
Taylor expanded in y around 0 98.0%
(FPCore (x y) :precision binary64 (* y y))
double code(double x, double y) {
return y * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * y
end function
public static double code(double x, double y) {
return y * y;
}
def code(x, y): return y * y
function code(x, y) return Float64(y * y) end
function tmp = code(x, y) tmp = y * y; end
code[x_, y_] := N[(y * y), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y
\end{array}
Initial program 93.7%
associate-+l+93.7%
associate-*l*93.7%
*-commutative93.7%
*-commutative93.7%
+-commutative93.7%
fma-define93.7%
*-commutative93.7%
*-commutative93.7%
associate-*l*93.7%
distribute-rgt-out97.3%
+-commutative97.3%
Simplified97.3%
Taylor expanded in x around 0 56.3%
associate-*r*56.3%
*-commutative56.3%
unpow256.3%
distribute-rgt-in59.8%
fma-undefine59.8%
Simplified59.8%
Taylor expanded in x around 0 59.1%
(FPCore (x y) :precision binary64 (+ (* x x) (+ (* y y) (* (* x y) 2.0))))
double code(double x, double y) {
return (x * x) + ((y * y) + ((x * y) * 2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + ((y * y) + ((x * y) * 2.0d0))
end function
public static double code(double x, double y) {
return (x * x) + ((y * y) + ((x * y) * 2.0));
}
def code(x, y): return (x * x) + ((y * y) + ((x * y) * 2.0))
function code(x, y) return Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(Float64(x * y) * 2.0))) end
function tmp = code(x, y) tmp = (x * x) + ((y * y) + ((x * y) * 2.0)); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(N[(x * y), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \left(y \cdot y + \left(x \cdot y\right) \cdot 2\right)
\end{array}
herbie shell --seed 2024146
(FPCore (x y)
:name "Examples.Basics.ProofTests:f4 from sbv-4.4"
:precision binary64
:alt
(! :herbie-platform default (+ (* x x) (+ (* y y) (* (* x y) 2))))
(+ (+ (* x x) (* (* x 2.0) y)) (* y y)))