Average Error: 15.8 → 0.3
Time: 16.9s
Precision: binary64
\[x \cdot \log \left(\frac{x}{y}\right) - z \]
\[\begin{array}{l} \mathbf{if}\;x \leq -4.369006329198465 \cdot 10^{-309}:\\ \;\;\;\;x \cdot \log \left(\frac{-1}{y}\right) - \left(x \cdot \log \left(\frac{-1}{x}\right) + z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, \log x + \log \left(\frac{1}{y}\right), -z\right)\\ \end{array} \]
x \cdot \log \left(\frac{x}{y}\right) - z
\begin{array}{l}
\mathbf{if}\;x \leq -4.369006329198465 \cdot 10^{-309}:\\
\;\;\;\;x \cdot \log \left(\frac{-1}{y}\right) - \left(x \cdot \log \left(\frac{-1}{x}\right) + z\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, \log x + \log \left(\frac{1}{y}\right), -z\right)\\


\end{array}
(FPCore (x y z) :precision binary64 (- (* x (log (/ x y))) z))
(FPCore (x y z)
 :precision binary64
 (if (<= x -4.369006329198465e-309)
   (- (* x (log (/ -1.0 y))) (+ (* x (log (/ -1.0 x))) z))
   (fma x (+ (log x) (log (/ 1.0 y))) (- z))))
double code(double x, double y, double z) {
	return (x * log((x / y))) - z;
}
double code(double x, double y, double z) {
	double tmp;
	if (x <= -4.369006329198465e-309) {
		tmp = (x * log((-1.0 / y))) - ((x * log((-1.0 / x))) + z);
	} else {
		tmp = fma(x, (log(x) + log((1.0 / y))), -z);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original15.8
Target8.1
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;y < 7.595077799083773 \cdot 10^{-308}:\\ \;\;\;\;x \cdot \log \left(\frac{x}{y}\right) - z\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\log x - \log y\right) - z\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if x < -4.3690063291984648e-309

    1. Initial program 15.8

      \[x \cdot \log \left(\frac{x}{y}\right) - z \]
    2. Taylor expanded in x around -inf 0.4

      \[\leadsto \color{blue}{\log \left(\frac{-1}{y}\right) \cdot x - \left(\log \left(\frac{-1}{x}\right) \cdot x + z\right)} \]

    if -4.3690063291984648e-309 < x

    1. Initial program 15.8

      \[x \cdot \log \left(\frac{x}{y}\right) - z \]
    2. Applied egg-rr15.8

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \log \left(\frac{x}{y}\right), -z\right)} \]
    3. Taylor expanded in x around 0 0.3

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\log x + \log \left(\frac{1}{y}\right)}, -z\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.369006329198465 \cdot 10^{-309}:\\ \;\;\;\;x \cdot \log \left(\frac{-1}{y}\right) - \left(x \cdot \log \left(\frac{-1}{x}\right) + z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, \log x + \log \left(\frac{1}{y}\right), -z\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022130 
(FPCore (x y z)
  :name "Numeric.SpecFunctions.Extra:bd0 from math-functions-0.1.5.2"
  :precision binary64

  :herbie-target
  (if (< y 7.595077799083773e-308) (- (* x (log (/ x y))) z) (- (* x (- (log x) (log y))) z))

  (- (* x (log (/ x y))) z))