
(FPCore (x y) :precision binary64 (+ (+ (* x y) x) y))
double code(double x, double y) {
return ((x * y) + x) + y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * y) + x) + y
end function
public static double code(double x, double y) {
return ((x * y) + x) + y;
}
def code(x, y): return ((x * y) + x) + y
function code(x, y) return Float64(Float64(Float64(x * y) + x) + y) end
function tmp = code(x, y) tmp = ((x * y) + x) + y; end
code[x_, y_] := N[(N[(N[(x * y), $MachinePrecision] + x), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + x\right) + y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (* x y) x) y))
double code(double x, double y) {
return ((x * y) + x) + y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * y) + x) + y
end function
public static double code(double x, double y) {
return ((x * y) + x) + y;
}
def code(x, y): return ((x * y) + x) + y
function code(x, y) return Float64(Float64(Float64(x * y) + x) + y) end
function tmp = code(x, y) tmp = ((x * y) + x) + y; end
code[x_, y_] := N[(N[(N[(x * y), $MachinePrecision] + x), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + x\right) + y
\end{array}
(FPCore (x y) :precision binary64 (+ x (fma x y y)))
double code(double x, double y) {
return x + fma(x, y, y);
}
function code(x, y) return Float64(x + fma(x, y, y)) end
code[x_, y_] := N[(x + N[(x * y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \mathsf{fma}\left(x, y, y\right)
\end{array}
Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= x -3.15e+282)
x
(if (<= x -2.2e+231)
(* x y)
(if (<= x -1.2e+57)
x
(if (<= x -5.8e+44)
(* x y)
(if (<= x -1.2e-83) x (if (<= x 1.0) y (* x y))))))))
double code(double x, double y) {
double tmp;
if (x <= -3.15e+282) {
tmp = x;
} else if (x <= -2.2e+231) {
tmp = x * y;
} else if (x <= -1.2e+57) {
tmp = x;
} else if (x <= -5.8e+44) {
tmp = x * y;
} else if (x <= -1.2e-83) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = 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 <= (-3.15d+282)) then
tmp = x
else if (x <= (-2.2d+231)) then
tmp = x * y
else if (x <= (-1.2d+57)) then
tmp = x
else if (x <= (-5.8d+44)) then
tmp = x * y
else if (x <= (-1.2d-83)) then
tmp = x
else if (x <= 1.0d0) then
tmp = y
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -3.15e+282) {
tmp = x;
} else if (x <= -2.2e+231) {
tmp = x * y;
} else if (x <= -1.2e+57) {
tmp = x;
} else if (x <= -5.8e+44) {
tmp = x * y;
} else if (x <= -1.2e-83) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -3.15e+282: tmp = x elif x <= -2.2e+231: tmp = x * y elif x <= -1.2e+57: tmp = x elif x <= -5.8e+44: tmp = x * y elif x <= -1.2e-83: tmp = x elif x <= 1.0: tmp = y else: tmp = x * y return tmp
function code(x, y) tmp = 0.0 if (x <= -3.15e+282) tmp = x; elseif (x <= -2.2e+231) tmp = Float64(x * y); elseif (x <= -1.2e+57) tmp = x; elseif (x <= -5.8e+44) tmp = Float64(x * y); elseif (x <= -1.2e-83) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -3.15e+282) tmp = x; elseif (x <= -2.2e+231) tmp = x * y; elseif (x <= -1.2e+57) tmp = x; elseif (x <= -5.8e+44) tmp = x * y; elseif (x <= -1.2e-83) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -3.15e+282], x, If[LessEqual[x, -2.2e+231], N[(x * y), $MachinePrecision], If[LessEqual[x, -1.2e+57], x, If[LessEqual[x, -5.8e+44], N[(x * y), $MachinePrecision], If[LessEqual[x, -1.2e-83], x, If[LessEqual[x, 1.0], y, N[(x * y), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.15 \cdot 10^{+282}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -2.2 \cdot 10^{+231}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq -1.2 \cdot 10^{+57}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -5.8 \cdot 10^{+44}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq -1.2 \cdot 10^{-83}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < -3.1500000000000002e282 or -2.19999999999999992e231 < x < -1.20000000000000002e57 or -5.8000000000000004e44 < x < -1.2e-83Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in y around 0 56.8%
if -3.1500000000000002e282 < x < -2.19999999999999992e231 or -1.20000000000000002e57 < x < -5.8000000000000004e44 or 1 < x Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around inf 98.3%
+-commutative98.3%
Simplified98.3%
Taylor expanded in y around inf 55.7%
if -1.2e-83 < x < 1Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around 0 79.7%
Final simplification67.5%
(FPCore (x y) :precision binary64 (if (or (<= x -1.25e-83) (not (<= x 1.35e-12))) (* x (+ y 1.0)) y))
double code(double x, double y) {
double tmp;
if ((x <= -1.25e-83) || !(x <= 1.35e-12)) {
tmp = x * (y + 1.0);
} else {
tmp = 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.25d-83)) .or. (.not. (x <= 1.35d-12))) then
tmp = x * (y + 1.0d0)
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.25e-83) || !(x <= 1.35e-12)) {
tmp = x * (y + 1.0);
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.25e-83) or not (x <= 1.35e-12): tmp = x * (y + 1.0) else: tmp = y return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.25e-83) || !(x <= 1.35e-12)) tmp = Float64(x * Float64(y + 1.0)); else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.25e-83) || ~((x <= 1.35e-12))) tmp = x * (y + 1.0); else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.25e-83], N[Not[LessEqual[x, 1.35e-12]], $MachinePrecision]], N[(x * N[(y + 1.0), $MachinePrecision]), $MachinePrecision], y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.25 \cdot 10^{-83} \lor \neg \left(x \leq 1.35 \cdot 10^{-12}\right):\\
\;\;\;\;x \cdot \left(y + 1\right)\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if x < -1.25e-83 or 1.3499999999999999e-12 < x Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around inf 89.7%
+-commutative89.7%
Simplified89.7%
if -1.25e-83 < x < 1.3499999999999999e-12Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around 0 80.4%
Final simplification85.3%
(FPCore (x y) :precision binary64 (if (<= x -2.6e-85) (* x (+ y 1.0)) (* y (+ x 1.0))))
double code(double x, double y) {
double tmp;
if (x <= -2.6e-85) {
tmp = x * (y + 1.0);
} else {
tmp = y * (x + 1.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2.6d-85)) then
tmp = x * (y + 1.0d0)
else
tmp = y * (x + 1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2.6e-85) {
tmp = x * (y + 1.0);
} else {
tmp = y * (x + 1.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.6e-85: tmp = x * (y + 1.0) else: tmp = y * (x + 1.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -2.6e-85) tmp = Float64(x * Float64(y + 1.0)); else tmp = Float64(y * Float64(x + 1.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2.6e-85) tmp = x * (y + 1.0); else tmp = y * (x + 1.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.6e-85], N[(x * N[(y + 1.0), $MachinePrecision]), $MachinePrecision], N[(y * N[(x + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.6 \cdot 10^{-85}:\\
\;\;\;\;x \cdot \left(y + 1\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x + 1\right)\\
\end{array}
\end{array}
if x < -2.60000000000000011e-85Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around inf 85.2%
+-commutative85.2%
Simplified85.2%
if -2.60000000000000011e-85 < x Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in y around inf 72.8%
+-commutative72.8%
Simplified72.8%
Final simplification76.3%
(FPCore (x y) :precision binary64 (if (<= x -1.25e-83) (+ x (* x y)) (* y (+ x 1.0))))
double code(double x, double y) {
double tmp;
if (x <= -1.25e-83) {
tmp = x + (x * y);
} else {
tmp = y * (x + 1.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.25d-83)) then
tmp = x + (x * y)
else
tmp = y * (x + 1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.25e-83) {
tmp = x + (x * y);
} else {
tmp = y * (x + 1.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.25e-83: tmp = x + (x * y) else: tmp = y * (x + 1.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -1.25e-83) tmp = Float64(x + Float64(x * y)); else tmp = Float64(y * Float64(x + 1.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.25e-83) tmp = x + (x * y); else tmp = y * (x + 1.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.25e-83], N[(x + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(y * N[(x + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.25 \cdot 10^{-83}:\\
\;\;\;\;x + x \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x + 1\right)\\
\end{array}
\end{array}
if x < -1.25e-83Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around inf 85.2%
+-commutative85.2%
Simplified85.2%
distribute-lft-in85.2%
*-rgt-identity85.2%
Applied egg-rr85.2%
if -1.25e-83 < x Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in y around inf 72.8%
+-commutative72.8%
Simplified72.8%
Final simplification76.3%
(FPCore (x y) :precision binary64 (+ y (+ x (* x y))))
double code(double x, double y) {
return y + (x + (x * y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + (x + (x * y))
end function
public static double code(double x, double y) {
return y + (x + (x * y));
}
def code(x, y): return y + (x + (x * y))
function code(x, y) return Float64(y + Float64(x + Float64(x * y))) end
function tmp = code(x, y) tmp = y + (x + (x * y)); end
code[x_, y_] := N[(y + N[(x + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y + \left(x + x \cdot y\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -2.8e-84) x y))
double code(double x, double y) {
double tmp;
if (x <= -2.8e-84) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2.8d-84)) then
tmp = x
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2.8e-84) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.8e-84: tmp = x else: tmp = y return tmp
function code(x, y) tmp = 0.0 if (x <= -2.8e-84) tmp = x; else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2.8e-84) tmp = x; else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.8e-84], x, y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.8 \cdot 10^{-84}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if x < -2.79999999999999982e-84Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in y around 0 53.3%
if -2.79999999999999982e-84 < x Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in x around 0 55.6%
Final simplification55.0%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
+-commutative100.0%
associate-+r+100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in y around 0 35.6%
Final simplification35.6%
herbie shell --seed 2023293
(FPCore (x y)
:name "Numeric.Log:$cexpm1 from log-domain-0.10.2.1, B"
:precision binary64
(+ (+ (* x y) x) y))