
(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) (* 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%
Taylor expanded in x around inf 98.8%
unpow298.8%
Simplified98.8%
Final simplification98.8%
(FPCore (x y)
:precision binary64
(if (<= x -4.5e-17)
(* x x)
(if (<= x -7.5e-58)
(* y y)
(if (<= x -1.4e-80) (* x x) (* y (+ x (+ x y)))))))
double code(double x, double y) {
double tmp;
if (x <= -4.5e-17) {
tmp = x * x;
} else if (x <= -7.5e-58) {
tmp = y * y;
} else if (x <= -1.4e-80) {
tmp = x * x;
} else {
tmp = y * (x + (x + y));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-4.5d-17)) then
tmp = x * x
else if (x <= (-7.5d-58)) then
tmp = y * y
else if (x <= (-1.4d-80)) then
tmp = x * x
else
tmp = y * (x + (x + y))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -4.5e-17) {
tmp = x * x;
} else if (x <= -7.5e-58) {
tmp = y * y;
} else if (x <= -1.4e-80) {
tmp = x * x;
} else {
tmp = y * (x + (x + y));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -4.5e-17: tmp = x * x elif x <= -7.5e-58: tmp = y * y elif x <= -1.4e-80: tmp = x * x else: tmp = y * (x + (x + y)) return tmp
function code(x, y) tmp = 0.0 if (x <= -4.5e-17) tmp = Float64(x * x); elseif (x <= -7.5e-58) tmp = Float64(y * y); elseif (x <= -1.4e-80) tmp = Float64(x * x); else tmp = Float64(y * Float64(x + Float64(x + y))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -4.5e-17) tmp = x * x; elseif (x <= -7.5e-58) tmp = y * y; elseif (x <= -1.4e-80) tmp = x * x; else tmp = y * (x + (x + y)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -4.5e-17], N[(x * x), $MachinePrecision], If[LessEqual[x, -7.5e-58], N[(y * y), $MachinePrecision], If[LessEqual[x, -1.4e-80], N[(x * x), $MachinePrecision], N[(y * N[(x + N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.5 \cdot 10^{-17}:\\
\;\;\;\;x \cdot x\\
\mathbf{elif}\;x \leq -7.5 \cdot 10^{-58}:\\
\;\;\;\;y \cdot y\\
\mathbf{elif}\;x \leq -1.4 \cdot 10^{-80}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x + \left(x + y\right)\right)\\
\end{array}
\end{array}
if x < -4.49999999999999978e-17 or -7.50000000000000002e-58 < x < -1.39999999999999995e-80Initial program 90.2%
associate-+l+90.2%
fma-def90.2%
distribute-rgt-out95.1%
Simplified95.1%
fma-udef95.1%
distribute-rgt-in90.2%
associate-+l+90.2%
+-commutative90.2%
associate-*l*90.2%
distribute-lft-out95.1%
Applied egg-rr95.1%
Taylor expanded in y around 0 85.0%
unpow285.0%
Simplified85.0%
if -4.49999999999999978e-17 < x < -7.50000000000000002e-58Initial program 100.0%
Taylor expanded in x around 0 74.3%
unpow274.3%
Simplified74.3%
if -1.39999999999999995e-80 < x Initial program 95.2%
Taylor expanded in x around 0 66.3%
unpow266.3%
*-commutative66.3%
associate-*l*66.3%
distribute-lft-in68.7%
fma-udef68.7%
Simplified68.7%
fma-udef68.7%
distribute-rgt-in66.3%
associate-*r*66.3%
*-commutative66.3%
*-commutative66.3%
associate-*r*66.3%
count-266.3%
+-commutative66.3%
distribute-lft-in66.3%
associate-+r+66.3%
Applied egg-rr66.3%
distribute-lft-out66.3%
distribute-lft-out68.7%
+-commutative68.7%
Applied egg-rr68.7%
Final simplification74.0%
(FPCore (x y) :precision binary64 (if (or (<= x -1.05e-20) (and (not (<= x -3.1e-58)) (<= x -3e-85))) (* x x) (* y y)))
double code(double x, double y) {
double tmp;
if ((x <= -1.05e-20) || (!(x <= -3.1e-58) && (x <= -3e-85))) {
tmp = x * x;
} 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 ((x <= (-1.05d-20)) .or. (.not. (x <= (-3.1d-58))) .and. (x <= (-3d-85))) then
tmp = x * x
else
tmp = y * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.05e-20) || (!(x <= -3.1e-58) && (x <= -3e-85))) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.05e-20) or (not (x <= -3.1e-58) and (x <= -3e-85)): tmp = x * x else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.05e-20) || (!(x <= -3.1e-58) && (x <= -3e-85))) tmp = Float64(x * x); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.05e-20) || (~((x <= -3.1e-58)) && (x <= -3e-85))) tmp = x * x; else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.05e-20], And[N[Not[LessEqual[x, -3.1e-58]], $MachinePrecision], LessEqual[x, -3e-85]]], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.05 \cdot 10^{-20} \lor \neg \left(x \leq -3.1 \cdot 10^{-58}\right) \land x \leq -3 \cdot 10^{-85}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if x < -1.0499999999999999e-20 or -3.0999999999999999e-58 < x < -3.00000000000000022e-85Initial program 90.5%
associate-+l+90.5%
fma-def90.5%
distribute-rgt-out95.2%
Simplified95.2%
fma-udef95.2%
distribute-rgt-in90.5%
associate-+l+90.5%
+-commutative90.5%
associate-*l*90.5%
distribute-lft-out95.2%
Applied egg-rr95.2%
Taylor expanded in y around 0 83.2%
unpow283.2%
Simplified83.2%
if -1.0499999999999999e-20 < x < -3.0999999999999999e-58 or -3.00000000000000022e-85 < x Initial program 95.3%
Taylor expanded in x around 0 68.0%
unpow268.0%
Simplified68.0%
Final simplification73.0%
(FPCore (x y) :precision binary64 (if (<= y 2.15e-35) (* x (+ x (+ y y))) (* y y)))
double code(double x, double y) {
double tmp;
if (y <= 2.15e-35) {
tmp = x * (x + (y + 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 <= 2.15d-35) then
tmp = x * (x + (y + y))
else
tmp = y * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 2.15e-35) {
tmp = x * (x + (y + y));
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2.15e-35: tmp = x * (x + (y + y)) else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (y <= 2.15e-35) tmp = Float64(x * Float64(x + Float64(y + y))); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 2.15e-35) tmp = x * (x + (y + y)); else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2.15e-35], N[(x * N[(x + N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.15 \cdot 10^{-35}:\\
\;\;\;\;x \cdot \left(x + \left(y + y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 2.1500000000000001e-35Initial program 95.5%
associate-+l+95.5%
fma-def95.5%
distribute-rgt-out97.7%
Simplified97.7%
fma-udef97.7%
distribute-rgt-in95.5%
associate-+l+95.5%
+-commutative95.5%
associate-*l*95.5%
distribute-lft-out97.7%
Applied egg-rr97.7%
Taylor expanded in y around 0 61.9%
unpow261.9%
associate-*r*61.9%
count-261.9%
distribute-rgt-in64.2%
Simplified64.2%
if 2.1500000000000001e-35 < y Initial program 89.9%
Taylor expanded in x around 0 73.8%
unpow273.8%
Simplified73.8%
Final simplification67.2%
(FPCore (x y) :precision binary64 (* x x))
double code(double x, double y) {
return x * x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * x
end function
public static double code(double x, double y) {
return x * x;
}
def code(x, y): return x * x
function code(x, y) return Float64(x * x) end
function tmp = code(x, y) tmp = x * x; end
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 93.7%
associate-+l+93.7%
fma-def93.7%
distribute-rgt-out96.9%
Simplified96.9%
fma-udef96.9%
distribute-rgt-in93.7%
associate-+l+93.7%
+-commutative93.7%
associate-*l*93.7%
distribute-lft-out96.9%
Applied egg-rr96.9%
Taylor expanded in y around 0 56.9%
unpow256.9%
Simplified56.9%
Final simplification56.9%
(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 2023195
(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)))