
(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 (+ (* x x) (pow y 2.0)))
double code(double x, double y) {
return (x * x) + pow(y, 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + (y ** 2.0d0)
end function
public static double code(double x, double y) {
return (x * x) + Math.pow(y, 2.0);
}
def code(x, y): return (x * x) + math.pow(y, 2.0)
function code(x, y) return Float64(Float64(x * x) + (y ^ 2.0)) end
function tmp = code(x, y) tmp = (x * x) + (y ^ 2.0); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + {y}^{2}
\end{array}
Initial program 93.0%
associate-+l+92.9%
+-commutative92.9%
fma-def93.0%
associate-*l*93.0%
Simplified93.0%
Taylor expanded in y around inf 99.2%
Final simplification99.2%
(FPCore (x y) :precision binary64 (if (<= y 4.8e+247) (+ (* x (+ x (* y 2.0))) (* y y)) (* y (+ y (* x 2.0)))))
double code(double x, double y) {
double tmp;
if (y <= 4.8e+247) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 4.8d+247) then
tmp = (x * (x + (y * 2.0d0))) + (y * y)
else
tmp = y * (y + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 4.8e+247) {
tmp = (x * (x + (y * 2.0))) + (y * y);
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 4.8e+247: tmp = (x * (x + (y * 2.0))) + (y * y) else: tmp = y * (y + (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if (y <= 4.8e+247) tmp = Float64(Float64(x * Float64(x + Float64(y * 2.0))) + Float64(y * y)); else tmp = Float64(y * Float64(y + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 4.8e+247) tmp = (x * (x + (y * 2.0))) + (y * y); else tmp = y * (y + (x * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 4.8e+247], N[(N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.8 \cdot 10^{+247}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right) + y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y + x \cdot 2\right)\\
\end{array}
\end{array}
if y < 4.8e247Initial program 93.8%
+-commutative93.8%
associate-*r*93.8%
distribute-lft-out97.9%
*-commutative97.9%
Applied egg-rr97.9%
if 4.8e247 < y Initial program 75.0%
+-commutative75.0%
associate-*r*75.0%
distribute-lft-out75.0%
*-commutative75.0%
Applied egg-rr75.0%
Taylor expanded in x around 0 75.0%
+-commutative75.0%
unpow275.0%
associate-*r*75.0%
*-commutative75.0%
distribute-rgt-out100.0%
*-commutative100.0%
Simplified100.0%
Final simplification98.0%
(FPCore (x y) :precision binary64 (if (<= x -6e-100) (* x (+ x (* y 2.0))) (* y (+ y (* x 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= -6e-100) {
tmp = x * (x + (y * 2.0));
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-6d-100)) then
tmp = x * (x + (y * 2.0d0))
else
tmp = y * (y + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -6e-100) {
tmp = x * (x + (y * 2.0));
} else {
tmp = y * (y + (x * 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6e-100: tmp = x * (x + (y * 2.0)) else: tmp = y * (y + (x * 2.0)) return tmp
function code(x, y) tmp = 0.0 if (x <= -6e-100) tmp = Float64(x * Float64(x + Float64(y * 2.0))); else tmp = Float64(y * Float64(y + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -6e-100) tmp = x * (x + (y * 2.0)); else tmp = y * (y + (x * 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6e-100], N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * N[(y + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{-100}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y + x \cdot 2\right)\\
\end{array}
\end{array}
if x < -6.0000000000000001e-100Initial program 91.9%
associate-+l+91.9%
+-commutative91.9%
fma-def91.9%
associate-*l*91.9%
Simplified91.9%
Taylor expanded in y around 0 70.6%
associate-*r*70.6%
*-commutative70.6%
*-commutative70.6%
*-commutative70.6%
Simplified70.6%
fma-def70.6%
associate-*r*70.6%
*-commutative70.6%
Applied egg-rr70.6%
fma-udef70.6%
distribute-lft-out74.6%
Applied egg-rr74.6%
if -6.0000000000000001e-100 < x Initial program 93.6%
+-commutative93.6%
associate-*r*93.6%
distribute-lft-out97.4%
*-commutative97.4%
Applied egg-rr97.4%
Taylor expanded in x around 0 67.0%
+-commutative67.0%
unpow267.0%
associate-*r*67.0%
*-commutative67.0%
distribute-rgt-out69.6%
*-commutative69.6%
Simplified69.6%
Final simplification71.5%
(FPCore (x y) :precision binary64 (* x (+ x (* y 2.0))))
double code(double x, double y) {
return x * (x + (y * 2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * (x + (y * 2.0d0))
end function
public static double code(double x, double y) {
return x * (x + (y * 2.0));
}
def code(x, y): return x * (x + (y * 2.0))
function code(x, y) return Float64(x * Float64(x + Float64(y * 2.0))) end
function tmp = code(x, y) tmp = x * (x + (y * 2.0)); end
code[x_, y_] := N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(x + y \cdot 2\right)
\end{array}
Initial program 93.0%
associate-+l+92.9%
+-commutative92.9%
fma-def93.0%
associate-*l*93.0%
Simplified93.0%
Taylor expanded in y around 0 54.1%
associate-*r*54.1%
*-commutative54.1%
*-commutative54.1%
*-commutative54.1%
Simplified54.1%
fma-def54.1%
associate-*r*54.1%
*-commutative54.1%
Applied egg-rr54.1%
fma-udef54.1%
distribute-lft-out58.0%
Applied egg-rr58.0%
Final simplification58.0%
(FPCore (x y) :precision binary64 (* y (* x 2.0)))
double code(double x, double y) {
return y * (x * 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * (x * 2.0d0)
end function
public static double code(double x, double y) {
return y * (x * 2.0);
}
def code(x, y): return y * (x * 2.0)
function code(x, y) return Float64(y * Float64(x * 2.0)) end
function tmp = code(x, y) tmp = y * (x * 2.0); end
code[x_, y_] := N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(x \cdot 2\right)
\end{array}
Initial program 93.0%
associate-+l+92.9%
+-commutative92.9%
fma-def93.0%
associate-*l*93.0%
Simplified93.0%
Taylor expanded in y around 0 54.1%
associate-*r*54.1%
*-commutative54.1%
*-commutative54.1%
*-commutative54.1%
Simplified54.1%
Taylor expanded in x around 0 18.2%
*-commutative18.2%
associate-*r*18.2%
*-commutative18.2%
associate-*r*18.2%
Simplified18.2%
Final simplification18.2%
(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 2023310
(FPCore (x y)
:name "Examples.Basics.ProofTests:f4 from sbv-4.4"
:precision binary64
:herbie-target
(+ (* x x) (+ (* y y) (* (* x y) 2.0)))
(+ (+ (* x x) (* (* x 2.0) y)) (* y y)))