
(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 (fma (+ y 1.0) x y))
double code(double x, double y) {
return fma((y + 1.0), x, y);
}
function code(x, y) return fma(Float64(y + 1.0), x, y) end
code[x_, y_] := N[(N[(y + 1.0), $MachinePrecision] * x + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y + 1, x, y\right)
\end{array}
Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
fma-define100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= x -6.5e+235)
(* y x)
(if (<= x -6.5e+174)
x
(if (<= x -1.5e+137)
(* y x)
(if (<= x -7.4e-142) x (if (<= x 1.0) y (* y x)))))))
double code(double x, double y) {
double tmp;
if (x <= -6.5e+235) {
tmp = y * x;
} else if (x <= -6.5e+174) {
tmp = x;
} else if (x <= -1.5e+137) {
tmp = y * x;
} else if (x <= -7.4e-142) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = y * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-6.5d+235)) then
tmp = y * x
else if (x <= (-6.5d+174)) then
tmp = x
else if (x <= (-1.5d+137)) then
tmp = y * x
else if (x <= (-7.4d-142)) then
tmp = x
else if (x <= 1.0d0) then
tmp = y
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -6.5e+235) {
tmp = y * x;
} else if (x <= -6.5e+174) {
tmp = x;
} else if (x <= -1.5e+137) {
tmp = y * x;
} else if (x <= -7.4e-142) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6.5e+235: tmp = y * x elif x <= -6.5e+174: tmp = x elif x <= -1.5e+137: tmp = y * x elif x <= -7.4e-142: tmp = x elif x <= 1.0: tmp = y else: tmp = y * x return tmp
function code(x, y) tmp = 0.0 if (x <= -6.5e+235) tmp = Float64(y * x); elseif (x <= -6.5e+174) tmp = x; elseif (x <= -1.5e+137) tmp = Float64(y * x); elseif (x <= -7.4e-142) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -6.5e+235) tmp = y * x; elseif (x <= -6.5e+174) tmp = x; elseif (x <= -1.5e+137) tmp = y * x; elseif (x <= -7.4e-142) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6.5e+235], N[(y * x), $MachinePrecision], If[LessEqual[x, -6.5e+174], x, If[LessEqual[x, -1.5e+137], N[(y * x), $MachinePrecision], If[LessEqual[x, -7.4e-142], x, If[LessEqual[x, 1.0], y, N[(y * x), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.5 \cdot 10^{+235}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq -6.5 \cdot 10^{+174}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -1.5 \cdot 10^{+137}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq -7.4 \cdot 10^{-142}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if x < -6.5000000000000001e235 or -6.5000000000000001e174 < x < -1.5e137 or 1 < x Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 99.7%
Taylor expanded in y around inf 59.1%
if -6.5000000000000001e235 < x < -6.5000000000000001e174 or -1.5e137 < x < -7.39999999999999972e-142Initial program 99.9%
*-commutative99.9%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 50.2%
if -7.39999999999999972e-142 < x < 1Initial program 100.0%
Taylor expanded in x around 0 85.5%
Final simplification67.0%
(FPCore (x y)
:precision binary64
(if (<= x -9.8e+235)
(* y x)
(if (<= x -6.5e+174)
x
(if (or (<= x -1.6e+128) (not (<= x 6800000000.0))) (* y x) (+ y x)))))
double code(double x, double y) {
double tmp;
if (x <= -9.8e+235) {
tmp = y * x;
} else if (x <= -6.5e+174) {
tmp = x;
} else if ((x <= -1.6e+128) || !(x <= 6800000000.0)) {
tmp = y * x;
} else {
tmp = y + x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-9.8d+235)) then
tmp = y * x
else if (x <= (-6.5d+174)) then
tmp = x
else if ((x <= (-1.6d+128)) .or. (.not. (x <= 6800000000.0d0))) then
tmp = y * x
else
tmp = y + x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -9.8e+235) {
tmp = y * x;
} else if (x <= -6.5e+174) {
tmp = x;
} else if ((x <= -1.6e+128) || !(x <= 6800000000.0)) {
tmp = y * x;
} else {
tmp = y + x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -9.8e+235: tmp = y * x elif x <= -6.5e+174: tmp = x elif (x <= -1.6e+128) or not (x <= 6800000000.0): tmp = y * x else: tmp = y + x return tmp
function code(x, y) tmp = 0.0 if (x <= -9.8e+235) tmp = Float64(y * x); elseif (x <= -6.5e+174) tmp = x; elseif ((x <= -1.6e+128) || !(x <= 6800000000.0)) tmp = Float64(y * x); else tmp = Float64(y + x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -9.8e+235) tmp = y * x; elseif (x <= -6.5e+174) tmp = x; elseif ((x <= -1.6e+128) || ~((x <= 6800000000.0))) tmp = y * x; else tmp = y + x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -9.8e+235], N[(y * x), $MachinePrecision], If[LessEqual[x, -6.5e+174], x, If[Or[LessEqual[x, -1.6e+128], N[Not[LessEqual[x, 6800000000.0]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(y + x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.8 \cdot 10^{+235}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq -6.5 \cdot 10^{+174}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -1.6 \cdot 10^{+128} \lor \neg \left(x \leq 6800000000\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;y + x\\
\end{array}
\end{array}
if x < -9.7999999999999995e235 or -6.5000000000000001e174 < x < -1.59999999999999993e128 or 6.8e9 < x Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 99.7%
Taylor expanded in y around inf 59.4%
if -9.7999999999999995e235 < x < -6.5000000000000001e174Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 74.3%
if -1.59999999999999993e128 < x < 6.8e9Initial program 100.0%
Taylor expanded in y around 0 90.7%
Final simplification79.4%
(FPCore (x y) :precision binary64 (if (<= y -1.15e-11) (* (+ y 1.0) x) (if (<= y 1.6e-5) (+ y x) (* y (+ 1.0 x)))))
double code(double x, double y) {
double tmp;
if (y <= -1.15e-11) {
tmp = (y + 1.0) * x;
} else if (y <= 1.6e-5) {
tmp = y + x;
} else {
tmp = y * (1.0 + x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-1.15d-11)) then
tmp = (y + 1.0d0) * x
else if (y <= 1.6d-5) then
tmp = y + x
else
tmp = y * (1.0d0 + x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -1.15e-11) {
tmp = (y + 1.0) * x;
} else if (y <= 1.6e-5) {
tmp = y + x;
} else {
tmp = y * (1.0 + x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.15e-11: tmp = (y + 1.0) * x elif y <= 1.6e-5: tmp = y + x else: tmp = y * (1.0 + x) return tmp
function code(x, y) tmp = 0.0 if (y <= -1.15e-11) tmp = Float64(Float64(y + 1.0) * x); elseif (y <= 1.6e-5) tmp = Float64(y + x); else tmp = Float64(y * Float64(1.0 + x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.15e-11) tmp = (y + 1.0) * x; elseif (y <= 1.6e-5) tmp = y + x; else tmp = y * (1.0 + x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.15e-11], N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[y, 1.6e-5], N[(y + x), $MachinePrecision], N[(y * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.15 \cdot 10^{-11}:\\
\;\;\;\;\left(y + 1\right) \cdot x\\
\mathbf{elif}\;y \leq 1.6 \cdot 10^{-5}:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 + x\right)\\
\end{array}
\end{array}
if y < -1.15000000000000007e-11Initial program 99.9%
*-commutative99.9%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 51.3%
if -1.15000000000000007e-11 < y < 1.59999999999999993e-5Initial program 100.0%
Taylor expanded in y around 0 100.0%
if 1.59999999999999993e-5 < y Initial program 100.0%
Taylor expanded in y around inf 98.4%
+-commutative98.4%
Simplified98.4%
Final simplification86.4%
(FPCore (x y) :precision binary64 (if (<= x -8.5e+20) (* (+ y 1.0) x) (if (<= x 22000000000.0) (+ y x) (* y x))))
double code(double x, double y) {
double tmp;
if (x <= -8.5e+20) {
tmp = (y + 1.0) * x;
} else if (x <= 22000000000.0) {
tmp = y + x;
} else {
tmp = y * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-8.5d+20)) then
tmp = (y + 1.0d0) * x
else if (x <= 22000000000.0d0) then
tmp = y + x
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -8.5e+20) {
tmp = (y + 1.0) * x;
} else if (x <= 22000000000.0) {
tmp = y + x;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8.5e+20: tmp = (y + 1.0) * x elif x <= 22000000000.0: tmp = y + x else: tmp = y * x return tmp
function code(x, y) tmp = 0.0 if (x <= -8.5e+20) tmp = Float64(Float64(y + 1.0) * x); elseif (x <= 22000000000.0) tmp = Float64(y + x); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -8.5e+20) tmp = (y + 1.0) * x; elseif (x <= 22000000000.0) tmp = y + x; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8.5e+20], N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[x, 22000000000.0], N[(y + x), $MachinePrecision], N[(y * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.5 \cdot 10^{+20}:\\
\;\;\;\;\left(y + 1\right) \cdot x\\
\mathbf{elif}\;x \leq 22000000000:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if x < -8.5e20Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 100.0%
if -8.5e20 < x < 2.2e10Initial program 100.0%
Taylor expanded in y around 0 97.5%
if 2.2e10 < x Initial program 99.9%
*-commutative99.9%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 99.6%
Taylor expanded in y around inf 54.1%
Final simplification87.6%
(FPCore (x y) :precision binary64 (+ y (* (+ y 1.0) x)))
double code(double x, double y) {
return y + ((y + 1.0) * x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + ((y + 1.0d0) * x)
end function
public static double code(double x, double y) {
return y + ((y + 1.0) * x);
}
def code(x, y): return y + ((y + 1.0) * x)
function code(x, y) return Float64(y + Float64(Float64(y + 1.0) * x)) end
function tmp = code(x, y) tmp = y + ((y + 1.0) * x); end
code[x_, y_] := N[(y + N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y + \left(y + 1\right) \cdot x
\end{array}
Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -7.4e-142) x y))
double code(double x, double y) {
double tmp;
if (x <= -7.4e-142) {
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 <= (-7.4d-142)) then
tmp = x
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -7.4e-142) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -7.4e-142: tmp = x else: tmp = y return tmp
function code(x, y) tmp = 0.0 if (x <= -7.4e-142) tmp = x; else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -7.4e-142) tmp = x; else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -7.4e-142], x, y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.4 \cdot 10^{-142}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if x < -7.39999999999999972e-142Initial program 99.9%
*-commutative99.9%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 45.6%
if -7.39999999999999972e-142 < x Initial program 100.0%
Taylor expanded in x around 0 54.8%
Final simplification51.4%
(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%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 34.3%
Final simplification34.3%
herbie shell --seed 2024055
(FPCore (x y)
:name "Numeric.Log:$cexpm1 from log-domain-0.10.2.1, B"
:precision binary64
(+ (+ (* x y) x) y))