Numeric.Log:$cexpm1 from log-domain-0.10.2.1, B

Percentage Accurate: 100.0% → 100.0%
Time: 2.0s
Alternatives: 4
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ \left(x \cdot y + x\right) + y \end{array} \]
(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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(x \cdot y + x\right) + y \end{array} \]
(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}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ y + \left(y + 1\right) \cdot x \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y) :precision binary64 (+ y (* (+ y 1.0) x)))
assert(x < y);
double code(double x, double y) {
	return y + ((y + 1.0) * x);
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = y + ((y + 1.0d0) * x)
end function
assert x < y;
public static double code(double x, double y) {
	return y + ((y + 1.0) * x);
}
[x, y] = sort([x, y])
def code(x, y):
	return y + ((y + 1.0) * x)
x, y = sort([x, y])
function code(x, y)
	return Float64(y + Float64(Float64(y + 1.0) * x))
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
	tmp = y + ((y + 1.0) * x);
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := N[(y + N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
y + \left(y + 1\right) \cdot x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x \cdot y + x\right) + y \]
  2. Step-by-step derivation
    1. *-commutative100.0%

      \[\leadsto \left(\color{blue}{y \cdot x} + x\right) + y \]
    2. distribute-lft1-in100.0%

      \[\leadsto \color{blue}{\left(y + 1\right) \cdot x} + y \]
  3. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\left(y + 1\right) \cdot x} + y \]
  4. Final simplification100.0%

    \[\leadsto y + \left(y + 1\right) \cdot x \]

Alternative 2: 99.1% accurate, 0.8× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq -1 \lor \neg \left(y \leq 0.052\right):\\ \;\;\;\;y + y \cdot x\\ \mathbf{else}:\\ \;\;\;\;y + x\\ \end{array} \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
 :precision binary64
 (if (or (<= y -1.0) (not (<= y 0.052))) (+ y (* y x)) (+ y x)))
assert(x < y);
double code(double x, double y) {
	double tmp;
	if ((y <= -1.0) || !(y <= 0.052)) {
		tmp = y + (y * x);
	} else {
		tmp = y + x;
	}
	return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y <= (-1.0d0)) .or. (.not. (y <= 0.052d0))) then
        tmp = y + (y * x)
    else
        tmp = y + x
    end if
    code = tmp
end function
assert x < y;
public static double code(double x, double y) {
	double tmp;
	if ((y <= -1.0) || !(y <= 0.052)) {
		tmp = y + (y * x);
	} else {
		tmp = y + x;
	}
	return tmp;
}
[x, y] = sort([x, y])
def code(x, y):
	tmp = 0
	if (y <= -1.0) or not (y <= 0.052):
		tmp = y + (y * x)
	else:
		tmp = y + x
	return tmp
x, y = sort([x, y])
function code(x, y)
	tmp = 0.0
	if ((y <= -1.0) || !(y <= 0.052))
		tmp = Float64(y + Float64(y * x));
	else
		tmp = Float64(y + x);
	end
	return tmp
end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= -1.0) || ~((y <= 0.052)))
		tmp = y + (y * x);
	else
		tmp = y + x;
	end
	tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := If[Or[LessEqual[y, -1.0], N[Not[LessEqual[y, 0.052]], $MachinePrecision]], N[(y + N[(y * x), $MachinePrecision]), $MachinePrecision], N[(y + x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \lor \neg \left(y \leq 0.052\right):\\
\;\;\;\;y + y \cdot x\\

\mathbf{else}:\\
\;\;\;\;y + x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1 or 0.0519999999999999976 < y

    1. Initial program 100.0%

      \[\left(x \cdot y + x\right) + y \]
    2. Taylor expanded in y around inf 98.8%

      \[\leadsto \color{blue}{x \cdot y} + y \]

    if -1 < y < 0.0519999999999999976

    1. Initial program 100.0%

      \[\left(x \cdot y + x\right) + y \]
    2. Taylor expanded in y around 0 97.9%

      \[\leadsto \color{blue}{x} + y \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1 \lor \neg \left(y \leq 0.052\right):\\ \;\;\;\;y + y \cdot x\\ \mathbf{else}:\\ \;\;\;\;y + x\\ \end{array} \]

Alternative 3: 74.2% accurate, 2.3× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ y + x \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y) :precision binary64 (+ y x))
assert(x < y);
double code(double x, double y) {
	return y + x;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = y + x
end function
assert x < y;
public static double code(double x, double y) {
	return y + x;
}
[x, y] = sort([x, y])
def code(x, y):
	return y + x
x, y = sort([x, y])
function code(x, y)
	return Float64(y + x)
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
	tmp = y + x;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := N[(y + x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
y + x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x \cdot y + x\right) + y \]
  2. Taylor expanded in y around 0 76.0%

    \[\leadsto \color{blue}{x} + y \]
  3. Final simplification76.0%

    \[\leadsto y + x \]

Alternative 4: 37.8% accurate, 7.0× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ y \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y) :precision binary64 y)
assert(x < y);
double code(double x, double y) {
	return y;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = y
end function
assert x < y;
public static double code(double x, double y) {
	return y;
}
[x, y] = sort([x, y])
def code(x, y):
	return y
x, y = sort([x, y])
function code(x, y)
	return y
end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
	tmp = y;
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := y
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
y
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x \cdot y + x\right) + y \]
  2. Step-by-step derivation
    1. *-commutative100.0%

      \[\leadsto \left(\color{blue}{y \cdot x} + x\right) + y \]
    2. distribute-lft1-in100.0%

      \[\leadsto \color{blue}{\left(y + 1\right) \cdot x} + y \]
    3. fma-def100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y + 1, x, y\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y + 1, x, y\right)} \]
  4. Taylor expanded in x around 0 41.6%

    \[\leadsto \color{blue}{y} \]
  5. Final simplification41.6%

    \[\leadsto y \]

Reproduce

?
herbie shell --seed 2023331 
(FPCore (x y)
  :name "Numeric.Log:$cexpm1 from log-domain-0.10.2.1, B"
  :precision binary64
  (+ (+ (* x y) x) y))