
(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) (* y (* x 2.0))) (* y y)) 5e+249) (+ (* x x) (* y (+ y (* x 2.0)))) (+ (* x x) (* y y))))
double code(double x, double y) {
double tmp;
if ((((x * x) + (y * (x * 2.0))) + (y * y)) <= 5e+249) {
tmp = (x * x) + (y * (y + (x * 2.0)));
} 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) + (y * (x * 2.0d0))) + (y * y)) <= 5d+249) then
tmp = (x * x) + (y * (y + (x * 2.0d0)))
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) + (y * (x * 2.0))) + (y * y)) <= 5e+249) {
tmp = (x * x) + (y * (y + (x * 2.0)));
} else {
tmp = (x * x) + (y * y);
}
return tmp;
}
def code(x, y): tmp = 0 if (((x * x) + (y * (x * 2.0))) + (y * y)) <= 5e+249: tmp = (x * x) + (y * (y + (x * 2.0))) else: tmp = (x * x) + (y * y) return tmp
function code(x, y) tmp = 0.0 if (Float64(Float64(Float64(x * x) + Float64(y * Float64(x * 2.0))) + Float64(y * y)) <= 5e+249) tmp = Float64(Float64(x * x) + Float64(y * Float64(y + Float64(x * 2.0)))); 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) + (y * (x * 2.0))) + (y * y)) <= 5e+249) tmp = (x * x) + (y * (y + (x * 2.0))); else tmp = (x * x) + (y * y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[(N[(x * x), $MachinePrecision] + N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], 5e+249], N[(N[(x * x), $MachinePrecision] + N[(y * N[(y + N[(x * 2.0), $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 + y \cdot \left(x \cdot 2\right)\right) + y \cdot y \leq 5 \cdot 10^{+249}:\\
\;\;\;\;x \cdot x + y \cdot \left(y + x \cdot 2\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)) < 4.9999999999999996e249Initial program 100.0%
associate-+l+100.0%
associate-*l*100.0%
*-commutative100.0%
*-commutative100.0%
+-commutative100.0%
fma-define100.0%
*-commutative100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in y around 0 100.0%
if 4.9999999999999996e249 < (+.f64 (+.f64 (*.f64 x x) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) (*.f64 y y)) Initial program 91.9%
associate-+l+91.9%
associate-*l*91.9%
*-commutative91.9%
*-commutative91.9%
+-commutative91.9%
fma-define91.9%
*-commutative91.9%
*-commutative91.9%
Simplified91.9%
Taylor expanded in y around 0 95.2%
Taylor expanded in y around inf 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (fma y y (* x (+ x (* y 2.0)))))
double code(double x, double y) {
return fma(y, y, (x * (x + (y * 2.0))));
}
function code(x, y) return fma(y, y, Float64(x * Float64(x + Float64(y * 2.0)))) end
code[x_, y_] := N[(y * y + N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y, x \cdot \left(x + y \cdot 2\right)\right)
\end{array}
Initial program 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-define96.1%
*-commutative96.1%
*-commutative96.1%
Simplified96.1%
+-commutative96.1%
fma-undefine96.1%
associate-*r*96.1%
associate-+r+96.1%
+-commutative96.1%
fma-define96.1%
+-commutative96.1%
associate-*r*96.1%
distribute-lft-out98.4%
*-commutative98.4%
Applied egg-rr98.4%
Final simplification98.4%
(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 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-define96.1%
*-commutative96.1%
*-commutative96.1%
Simplified96.1%
Taylor expanded in y around 0 97.6%
Taylor expanded in y around inf 98.1%
(FPCore (x y) :precision binary64 (* x (* y 2.0)))
double code(double x, double y) {
return x * (y * 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * (y * 2.0d0)
end function
public static double code(double x, double y) {
return x * (y * 2.0);
}
def code(x, y): return x * (y * 2.0)
function code(x, y) return Float64(x * Float64(y * 2.0)) end
function tmp = code(x, y) tmp = x * (y * 2.0); end
code[x_, y_] := N[(x * N[(y * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y \cdot 2\right)
\end{array}
Initial program 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-define96.1%
*-commutative96.1%
*-commutative96.1%
Simplified96.1%
Taylor expanded in y around 0 51.0%
*-commutative51.0%
associate-*r*51.0%
*-commutative51.0%
Simplified51.0%
Taylor expanded in x around 0 11.3%
associate-*r*11.7%
Simplified11.7%
Taylor expanded in x around 0 11.3%
associate-*r*11.7%
*-commutative11.7%
associate-*r*11.3%
Simplified11.3%
Final simplification11.3%
(FPCore (x y) :precision binary64 (* 2.0 (* y x)))
double code(double x, double y) {
return 2.0 * (y * x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 2.0d0 * (y * x)
end function
public static double code(double x, double y) {
return 2.0 * (y * x);
}
def code(x, y): return 2.0 * (y * x)
function code(x, y) return Float64(2.0 * Float64(y * x)) end
function tmp = code(x, y) tmp = 2.0 * (y * x); end
code[x_, y_] := N[(2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(y \cdot x\right)
\end{array}
Initial program 96.1%
associate-+l+96.1%
associate-*l*96.1%
*-commutative96.1%
*-commutative96.1%
+-commutative96.1%
fma-define96.1%
*-commutative96.1%
*-commutative96.1%
Simplified96.1%
Taylor expanded in y around 0 51.0%
*-commutative51.0%
associate-*r*51.0%
*-commutative51.0%
Simplified51.0%
Taylor expanded in x around 0 11.3%
Final simplification11.3%
(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 2024139
(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)))