Numeric.SpecFunctions:logGamma from math-functions-0.1.5.2, A

Percentage Accurate: 100.0% → 100.0%
Time: 3.4s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \end{array} \]
(FPCore (x y)
 :precision binary64
 (+ (- (* x (- y 1.0)) (* y 0.5)) 0.918938533204673))
double code(double x, double y) {
	return ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = ((x * (y - 1.0d0)) - (y * 0.5d0)) + 0.918938533204673d0
end function
public static double code(double x, double y) {
	return ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673;
}
def code(x, y):
	return ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673
function code(x, y)
	return Float64(Float64(Float64(x * Float64(y - 1.0)) - Float64(y * 0.5)) + 0.918938533204673)
end
function tmp = code(x, y)
	tmp = ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673;
end
code[x_, y_] := N[(N[(N[(x * N[(y - 1.0), $MachinePrecision]), $MachinePrecision] - N[(y * 0.5), $MachinePrecision]), $MachinePrecision] + 0.918938533204673), $MachinePrecision]
\begin{array}{l}

\\
\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673
\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 6 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 \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \end{array} \]
(FPCore (x y)
 :precision binary64
 (+ (- (* x (- y 1.0)) (* y 0.5)) 0.918938533204673))
double code(double x, double y) {
	return ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = ((x * (y - 1.0d0)) - (y * 0.5d0)) + 0.918938533204673d0
end function
public static double code(double x, double y) {
	return ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673;
}
def code(x, y):
	return ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673
function code(x, y)
	return Float64(Float64(Float64(x * Float64(y - 1.0)) - Float64(y * 0.5)) + 0.918938533204673)
end
function tmp = code(x, y)
	tmp = ((x * (y - 1.0)) - (y * 0.5)) + 0.918938533204673;
end
code[x_, y_] := N[(N[(N[(x * N[(y - 1.0), $MachinePrecision]), $MachinePrecision] - N[(y * 0.5), $MachinePrecision]), $MachinePrecision] + 0.918938533204673), $MachinePrecision]
\begin{array}{l}

\\
\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673
\end{array}

Alternative 1: 100.0% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y, x + -0.5, 0.918938533204673 - x\right) \end{array} \]
(FPCore (x y) :precision binary64 (fma y (+ x -0.5) (- 0.918938533204673 x)))
double code(double x, double y) {
	return fma(y, (x + -0.5), (0.918938533204673 - x));
}
function code(x, y)
	return fma(y, Float64(x + -0.5), Float64(0.918938533204673 - x))
end
code[x_, y_] := N[(y * N[(x + -0.5), $MachinePrecision] + N[(0.918938533204673 - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y, x + -0.5, 0.918938533204673 - x\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
  2. Step-by-step derivation
    1. sub-neg100.0%

      \[\leadsto \color{blue}{\left(x \cdot \left(y - 1\right) + \left(-y \cdot 0.5\right)\right)} + 0.918938533204673 \]
    2. +-commutative100.0%

      \[\leadsto \color{blue}{\left(\left(-y \cdot 0.5\right) + x \cdot \left(y - 1\right)\right)} + 0.918938533204673 \]
    3. sub-neg100.0%

      \[\leadsto \left(\left(-y \cdot 0.5\right) + x \cdot \color{blue}{\left(y + \left(-1\right)\right)}\right) + 0.918938533204673 \]
    4. distribute-rgt-in100.0%

      \[\leadsto \left(\left(-y \cdot 0.5\right) + \color{blue}{\left(y \cdot x + \left(-1\right) \cdot x\right)}\right) + 0.918938533204673 \]
    5. associate-+r+100.0%

      \[\leadsto \color{blue}{\left(\left(\left(-y \cdot 0.5\right) + y \cdot x\right) + \left(-1\right) \cdot x\right)} + 0.918938533204673 \]
    6. associate-+l+100.0%

      \[\leadsto \color{blue}{\left(\left(-y \cdot 0.5\right) + y \cdot x\right) + \left(\left(-1\right) \cdot x + 0.918938533204673\right)} \]
    7. distribute-rgt-neg-in100.0%

      \[\leadsto \left(\color{blue}{y \cdot \left(-0.5\right)} + y \cdot x\right) + \left(\left(-1\right) \cdot x + 0.918938533204673\right) \]
    8. distribute-lft-out100.0%

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, \left(-0.5\right) + x, \left(-1\right) \cdot x + 0.918938533204673\right)} \]
    10. +-commutative100.0%

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{x + \left(-0.5\right)}, \left(-1\right) \cdot x + 0.918938533204673\right) \]
    11. metadata-eval100.0%

      \[\leadsto \mathsf{fma}\left(y, x + \color{blue}{-0.5}, \left(-1\right) \cdot x + 0.918938533204673\right) \]
    12. +-commutative100.0%

      \[\leadsto \mathsf{fma}\left(y, x + -0.5, \color{blue}{0.918938533204673 + \left(-1\right) \cdot x}\right) \]
    13. cancel-sign-sub-inv100.0%

      \[\leadsto \mathsf{fma}\left(y, x + -0.5, \color{blue}{0.918938533204673 - 1 \cdot x}\right) \]
    14. *-lft-identity100.0%

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(y, x + -0.5, 0.918938533204673 - x\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(y, x + -0.5, 0.918938533204673 - x\right) \]

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.918938533204673 + \left(x \cdot \left(y + -1\right) - y \cdot 0.5\right) \end{array} \]
(FPCore (x y)
 :precision binary64
 (+ 0.918938533204673 (- (* x (+ y -1.0)) (* y 0.5))))
double code(double x, double y) {
	return 0.918938533204673 + ((x * (y + -1.0)) - (y * 0.5));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 0.918938533204673d0 + ((x * (y + (-1.0d0))) - (y * 0.5d0))
end function
public static double code(double x, double y) {
	return 0.918938533204673 + ((x * (y + -1.0)) - (y * 0.5));
}
def code(x, y):
	return 0.918938533204673 + ((x * (y + -1.0)) - (y * 0.5))
function code(x, y)
	return Float64(0.918938533204673 + Float64(Float64(x * Float64(y + -1.0)) - Float64(y * 0.5)))
end
function tmp = code(x, y)
	tmp = 0.918938533204673 + ((x * (y + -1.0)) - (y * 0.5));
end
code[x_, y_] := N[(0.918938533204673 + N[(N[(x * N[(y + -1.0), $MachinePrecision]), $MachinePrecision] - N[(y * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.918938533204673 + \left(x \cdot \left(y + -1\right) - y \cdot 0.5\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
  2. Final simplification100.0%

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

Alternative 3: 97.8% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.3 \lor \neg \left(y \leq 1.4\right):\\ \;\;\;\;y \cdot \left(x - 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;0.918938533204673 - x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= y -1.3) (not (<= y 1.4)))
   (* y (- x 0.5))
   (- 0.918938533204673 x)))
double code(double x, double y) {
	double tmp;
	if ((y <= -1.3) || !(y <= 1.4)) {
		tmp = y * (x - 0.5);
	} else {
		tmp = 0.918938533204673 - 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.3d0)) .or. (.not. (y <= 1.4d0))) then
        tmp = y * (x - 0.5d0)
    else
        tmp = 0.918938533204673d0 - x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y <= -1.3) || !(y <= 1.4)) {
		tmp = y * (x - 0.5);
	} else {
		tmp = 0.918938533204673 - x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y <= -1.3) or not (y <= 1.4):
		tmp = y * (x - 0.5)
	else:
		tmp = 0.918938533204673 - x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y <= -1.3) || !(y <= 1.4))
		tmp = Float64(y * Float64(x - 0.5));
	else
		tmp = Float64(0.918938533204673 - x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= -1.3) || ~((y <= 1.4)))
		tmp = y * (x - 0.5);
	else
		tmp = 0.918938533204673 - x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[y, -1.3], N[Not[LessEqual[y, 1.4]], $MachinePrecision]], N[(y * N[(x - 0.5), $MachinePrecision]), $MachinePrecision], N[(0.918938533204673 - x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.3 \lor \neg \left(y \leq 1.4\right):\\
\;\;\;\;y \cdot \left(x - 0.5\right)\\

\mathbf{else}:\\
\;\;\;\;0.918938533204673 - x\\


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

    1. Initial program 100.0%

      \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      2. fma-neg100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
      3. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      4. +-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      5. remove-double-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      6. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      7. fma-neg100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      8. sub-neg100.0%

        \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      9. remove-double-neg100.0%

        \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      10. +-commutative100.0%

        \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    4. Taylor expanded in y around inf 98.3%

      \[\leadsto \color{blue}{y \cdot \left(x - 0.5\right)} \]

    if -1.30000000000000004 < y < 1.3999999999999999

    1. Initial program 100.0%

      \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      2. fma-neg100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
      3. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      4. +-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      5. remove-double-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      6. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      7. fma-neg100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      8. sub-neg100.0%

        \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      9. remove-double-neg100.0%

        \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      10. +-commutative100.0%

        \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    4. Taylor expanded in y around 0 98.0%

      \[\leadsto \color{blue}{0.918938533204673 + -1 \cdot x} \]
    5. Step-by-step derivation
      1. neg-mul-198.0%

        \[\leadsto 0.918938533204673 + \color{blue}{\left(-x\right)} \]
      2. unsub-neg98.0%

        \[\leadsto \color{blue}{0.918938533204673 - x} \]
    6. Simplified98.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.3 \lor \neg \left(y \leq 1.4\right):\\ \;\;\;\;y \cdot \left(x - 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;0.918938533204673 - x\\ \end{array} \]

Alternative 4: 49.9% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= y -1.0) (* y x) (if (<= y 1.0) (- x) (* y x))))
double code(double x, double y) {
	double tmp;
	if (y <= -1.0) {
		tmp = y * x;
	} else if (y <= 1.0) {
		tmp = -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 (y <= (-1.0d0)) then
        tmp = y * x
    else if (y <= 1.0d0) then
        tmp = -x
    else
        tmp = y * x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (y <= -1.0) {
		tmp = y * x;
	} else if (y <= 1.0) {
		tmp = -x;
	} else {
		tmp = y * x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if y <= -1.0:
		tmp = y * x
	elif y <= 1.0:
		tmp = -x
	else:
		tmp = y * x
	return tmp
function code(x, y)
	tmp = 0.0
	if (y <= -1.0)
		tmp = Float64(y * x);
	elseif (y <= 1.0)
		tmp = Float64(-x);
	else
		tmp = Float64(y * x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= -1.0)
		tmp = y * x;
	elseif (y <= 1.0)
		tmp = -x;
	else
		tmp = y * x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[y, -1.0], N[(y * x), $MachinePrecision], If[LessEqual[y, 1.0], (-x), N[(y * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1:\\
\;\;\;\;y \cdot x\\

\mathbf{elif}\;y \leq 1:\\
\;\;\;\;-x\\

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


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

    1. Initial program 100.0%

      \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      2. fma-neg100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
      3. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      4. +-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      5. remove-double-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      6. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      7. fma-neg100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      8. sub-neg100.0%

        \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      9. remove-double-neg100.0%

        \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      10. +-commutative100.0%

        \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    4. Taylor expanded in x around inf 47.2%

      \[\leadsto \color{blue}{\left(y - 1\right) \cdot x} \]
    5. Step-by-step derivation
      1. sub-neg47.2%

        \[\leadsto \color{blue}{\left(y + \left(-1\right)\right)} \cdot x \]
      2. metadata-eval47.2%

        \[\leadsto \left(y + \color{blue}{-1}\right) \cdot x \]
      3. *-commutative47.2%

        \[\leadsto \color{blue}{x \cdot \left(y + -1\right)} \]
      4. distribute-rgt-in47.2%

        \[\leadsto \color{blue}{y \cdot x + -1 \cdot x} \]
      5. fma-def47.2%

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, -1 \cdot x\right)} \]
      6. neg-mul-147.2%

        \[\leadsto \mathsf{fma}\left(y, x, \color{blue}{-x}\right) \]
      7. fma-neg47.2%

        \[\leadsto \color{blue}{y \cdot x - x} \]
    6. Simplified47.2%

      \[\leadsto \color{blue}{y \cdot x - x} \]
    7. Taylor expanded in y around inf 46.3%

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

    if -1 < y < 1

    1. Initial program 100.0%

      \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      2. fma-neg100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
      3. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      4. +-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      5. remove-double-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      6. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      7. fma-neg100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      8. sub-neg100.0%

        \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      9. remove-double-neg100.0%

        \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      10. +-commutative100.0%

        \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    4. Taylor expanded in x around inf 45.1%

      \[\leadsto \color{blue}{\left(y - 1\right) \cdot x} \]
    5. Step-by-step derivation
      1. sub-neg45.1%

        \[\leadsto \color{blue}{\left(y + \left(-1\right)\right)} \cdot x \]
      2. metadata-eval45.1%

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

        \[\leadsto \color{blue}{x \cdot \left(y + -1\right)} \]
      4. distribute-rgt-in45.1%

        \[\leadsto \color{blue}{y \cdot x + -1 \cdot x} \]
      5. fma-def45.1%

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, -1 \cdot x\right)} \]
      6. neg-mul-145.1%

        \[\leadsto \mathsf{fma}\left(y, x, \color{blue}{-x}\right) \]
      7. fma-neg45.1%

        \[\leadsto \color{blue}{y \cdot x - x} \]
    6. Simplified45.1%

      \[\leadsto \color{blue}{y \cdot x - x} \]
    7. Taylor expanded in y around 0 43.4%

      \[\leadsto \color{blue}{-1 \cdot x} \]
    8. Step-by-step derivation
      1. neg-mul-143.4%

        \[\leadsto \color{blue}{-x} \]
    9. Simplified43.4%

      \[\leadsto \color{blue}{-x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification44.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]

Alternative 5: 73.8% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -900:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 1.75:\\ \;\;\;\;0.918938533204673 - x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= y -900.0) (* y x) (if (<= y 1.75) (- 0.918938533204673 x) (* y x))))
double code(double x, double y) {
	double tmp;
	if (y <= -900.0) {
		tmp = y * x;
	} else if (y <= 1.75) {
		tmp = 0.918938533204673 - 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 (y <= (-900.0d0)) then
        tmp = y * x
    else if (y <= 1.75d0) then
        tmp = 0.918938533204673d0 - x
    else
        tmp = y * x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (y <= -900.0) {
		tmp = y * x;
	} else if (y <= 1.75) {
		tmp = 0.918938533204673 - x;
	} else {
		tmp = y * x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if y <= -900.0:
		tmp = y * x
	elif y <= 1.75:
		tmp = 0.918938533204673 - x
	else:
		tmp = y * x
	return tmp
function code(x, y)
	tmp = 0.0
	if (y <= -900.0)
		tmp = Float64(y * x);
	elseif (y <= 1.75)
		tmp = Float64(0.918938533204673 - x);
	else
		tmp = Float64(y * x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= -900.0)
		tmp = y * x;
	elseif (y <= 1.75)
		tmp = 0.918938533204673 - x;
	else
		tmp = y * x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[y, -900.0], N[(y * x), $MachinePrecision], If[LessEqual[y, 1.75], N[(0.918938533204673 - x), $MachinePrecision], N[(y * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -900:\\
\;\;\;\;y \cdot x\\

\mathbf{elif}\;y \leq 1.75:\\
\;\;\;\;0.918938533204673 - x\\

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


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

    1. Initial program 100.0%

      \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      2. fma-neg100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
      3. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      4. +-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      5. remove-double-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      6. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      7. fma-neg100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      8. sub-neg100.0%

        \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      9. remove-double-neg100.0%

        \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      10. +-commutative100.0%

        \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    4. Taylor expanded in x around inf 47.4%

      \[\leadsto \color{blue}{\left(y - 1\right) \cdot x} \]
    5. Step-by-step derivation
      1. sub-neg47.4%

        \[\leadsto \color{blue}{\left(y + \left(-1\right)\right)} \cdot x \]
      2. metadata-eval47.4%

        \[\leadsto \left(y + \color{blue}{-1}\right) \cdot x \]
      3. *-commutative47.4%

        \[\leadsto \color{blue}{x \cdot \left(y + -1\right)} \]
      4. distribute-rgt-in47.4%

        \[\leadsto \color{blue}{y \cdot x + -1 \cdot x} \]
      5. fma-def47.4%

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, -1 \cdot x\right)} \]
      6. neg-mul-147.4%

        \[\leadsto \mathsf{fma}\left(y, x, \color{blue}{-x}\right) \]
      7. fma-neg47.4%

        \[\leadsto \color{blue}{y \cdot x - x} \]
    6. Simplified47.4%

      \[\leadsto \color{blue}{y \cdot x - x} \]
    7. Taylor expanded in y around inf 46.9%

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

    if -900 < y < 1.75

    1. Initial program 100.0%

      \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      2. fma-neg100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
      3. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      4. +-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      5. remove-double-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      6. sub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
      7. fma-neg100.0%

        \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
      8. sub-neg100.0%

        \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      9. remove-double-neg100.0%

        \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      10. +-commutative100.0%

        \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    4. Taylor expanded in y around 0 96.8%

      \[\leadsto \color{blue}{0.918938533204673 + -1 \cdot x} \]
    5. Step-by-step derivation
      1. neg-mul-196.8%

        \[\leadsto 0.918938533204673 + \color{blue}{\left(-x\right)} \]
      2. unsub-neg96.8%

        \[\leadsto \color{blue}{0.918938533204673 - x} \]
    6. Simplified96.8%

      \[\leadsto \color{blue}{0.918938533204673 - x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -900:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 1.75:\\ \;\;\;\;0.918938533204673 - x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]

Alternative 6: 26.9% accurate, 5.5× speedup?

\[\begin{array}{l} \\ -x \end{array} \]
(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 Float64(-x)
end
function tmp = code(x, y)
	tmp = -x;
end
code[x_, y_] := (-x)
\begin{array}{l}

\\
-x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x \cdot \left(y - 1\right) - y \cdot 0.5\right) + 0.918938533204673 \]
  2. Step-by-step derivation
    1. associate-+l-100.0%

      \[\leadsto \color{blue}{x \cdot \left(y - 1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    2. fma-neg100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - 1, -\left(y \cdot 0.5 - 0.918938533204673\right)\right)} \]
    3. sub-neg100.0%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + \left(-1\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
    4. +-commutative100.0%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) + y}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
    5. remove-double-neg100.0%

      \[\leadsto \mathsf{fma}\left(x, \left(-1\right) + \color{blue}{\left(-\left(-y\right)\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
    6. sub-neg100.0%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(-1\right) - \left(-y\right)}, -\left(y \cdot 0.5 - 0.918938533204673\right)\right) \]
    7. fma-neg100.0%

      \[\leadsto \color{blue}{x \cdot \left(\left(-1\right) - \left(-y\right)\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
    8. sub-neg100.0%

      \[\leadsto x \cdot \color{blue}{\left(\left(-1\right) + \left(-\left(-y\right)\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
    9. remove-double-neg100.0%

      \[\leadsto x \cdot \left(\left(-1\right) + \color{blue}{y}\right) - \left(y \cdot 0.5 - 0.918938533204673\right) \]
    10. +-commutative100.0%

      \[\leadsto x \cdot \color{blue}{\left(y + \left(-1\right)\right)} - \left(y \cdot 0.5 - 0.918938533204673\right) \]
    11. metadata-eval100.0%

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

    \[\leadsto \color{blue}{x \cdot \left(y + -1\right) - \left(y \cdot 0.5 - 0.918938533204673\right)} \]
  4. Taylor expanded in x around inf 46.0%

    \[\leadsto \color{blue}{\left(y - 1\right) \cdot x} \]
  5. Step-by-step derivation
    1. sub-neg46.0%

      \[\leadsto \color{blue}{\left(y + \left(-1\right)\right)} \cdot x \]
    2. metadata-eval46.0%

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

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right)} \]
    4. distribute-rgt-in46.1%

      \[\leadsto \color{blue}{y \cdot x + -1 \cdot x} \]
    5. fma-def46.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, -1 \cdot x\right)} \]
    6. neg-mul-146.1%

      \[\leadsto \mathsf{fma}\left(y, x, \color{blue}{-x}\right) \]
    7. fma-neg46.1%

      \[\leadsto \color{blue}{y \cdot x - x} \]
  6. Simplified46.1%

    \[\leadsto \color{blue}{y \cdot x - x} \]
  7. Taylor expanded in y around 0 24.4%

    \[\leadsto \color{blue}{-1 \cdot x} \]
  8. Step-by-step derivation
    1. neg-mul-124.4%

      \[\leadsto \color{blue}{-x} \]
  9. Simplified24.4%

    \[\leadsto \color{blue}{-x} \]
  10. Final simplification24.4%

    \[\leadsto -x \]

Reproduce

?
herbie shell --seed 2023221 
(FPCore (x y)
  :name "Numeric.SpecFunctions:logGamma from math-functions-0.1.5.2, A"
  :precision binary64
  (+ (- (* x (- y 1.0)) (* y 0.5)) 0.918938533204673))