Average Error: 25.1 → 8.5
Time: 1.1m
Precision: 64
\[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.464209615268306636703404924042409599662 \cdot 10^{-92}:\\ \;\;\;\;x - \log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right) \cdot \frac{1}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \mathsf{fma}\left(1, \frac{z \cdot y}{t}, \mathsf{fma}\left(0.5, \frac{\left(z \cdot z\right) \cdot y}{t}, \frac{\log 1}{t}\right)\right)\\ \end{array}\]
x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\begin{array}{l}
\mathbf{if}\;z \le -3.464209615268306636703404924042409599662 \cdot 10^{-92}:\\
\;\;\;\;x - \log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right) \cdot \frac{1}{t}\\

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

\end{array}
double f(double x, double y, double z, double t) {
        double r84484463 = x;
        double r84484464 = 1.0;
        double r84484465 = y;
        double r84484466 = r84484464 - r84484465;
        double r84484467 = z;
        double r84484468 = exp(r84484467);
        double r84484469 = r84484465 * r84484468;
        double r84484470 = r84484466 + r84484469;
        double r84484471 = log(r84484470);
        double r84484472 = t;
        double r84484473 = r84484471 / r84484472;
        double r84484474 = r84484463 - r84484473;
        return r84484474;
}

double f(double x, double y, double z, double t) {
        double r84484475 = z;
        double r84484476 = -3.4642096152683066e-92;
        bool r84484477 = r84484475 <= r84484476;
        double r84484478 = x;
        double r84484479 = expm1(r84484475);
        double r84484480 = y;
        double r84484481 = 1.0;
        double r84484482 = fma(r84484479, r84484480, r84484481);
        double r84484483 = log(r84484482);
        double r84484484 = 1.0;
        double r84484485 = t;
        double r84484486 = r84484484 / r84484485;
        double r84484487 = r84484483 * r84484486;
        double r84484488 = r84484478 - r84484487;
        double r84484489 = r84484475 * r84484480;
        double r84484490 = r84484489 / r84484485;
        double r84484491 = 0.5;
        double r84484492 = r84484475 * r84484475;
        double r84484493 = r84484492 * r84484480;
        double r84484494 = r84484493 / r84484485;
        double r84484495 = log(r84484481);
        double r84484496 = r84484495 / r84484485;
        double r84484497 = fma(r84484491, r84484494, r84484496);
        double r84484498 = fma(r84484481, r84484490, r84484497);
        double r84484499 = r84484478 - r84484498;
        double r84484500 = r84484477 ? r84484488 : r84484499;
        return r84484500;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Bits error versus t

Target

Original25.1
Target16.3
Herbie8.5
\[\begin{array}{l} \mathbf{if}\;z \lt -2.887462308820794658905265984545350618896 \cdot 10^{119}:\\ \;\;\;\;\left(x - \frac{\frac{-0.5}{y \cdot t}}{z \cdot z}\right) - \frac{-0.5}{y \cdot t} \cdot \frac{\frac{2}{z}}{z \cdot z}\\ \mathbf{else}:\\ \;\;\;\;x - \frac{\log \left(1 + z \cdot y\right)}{t}\\ \end{array}\]

Derivation

  1. Split input into 2 regimes
  2. if z < -3.4642096152683066e-92

    1. Initial program 16.0

      \[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}\]
    2. Simplified12.0

      \[\leadsto \color{blue}{x - \frac{\log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right)}{t}}\]
    3. Using strategy rm
    4. Applied div-inv12.1

      \[\leadsto x - \color{blue}{\log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right) \cdot \frac{1}{t}}\]

    if -3.4642096152683066e-92 < z

    1. Initial program 31.0

      \[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}\]
    2. Simplified11.4

      \[\leadsto \color{blue}{x - \frac{\log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right)}{t}}\]
    3. Using strategy rm
    4. Applied div-inv11.4

      \[\leadsto x - \color{blue}{\log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right) \cdot \frac{1}{t}}\]
    5. Taylor expanded around 0 6.3

      \[\leadsto x - \color{blue}{\left(1 \cdot \frac{z \cdot y}{t} + \left(0.5 \cdot \frac{{z}^{2} \cdot y}{t} + \frac{\log 1}{t}\right)\right)}\]
    6. Simplified6.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -3.464209615268306636703404924042409599662 \cdot 10^{-92}:\\ \;\;\;\;x - \log \left(\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)\right) \cdot \frac{1}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \mathsf{fma}\left(1, \frac{z \cdot y}{t}, \mathsf{fma}\left(0.5, \frac{\left(z \cdot z\right) \cdot y}{t}, \frac{\log 1}{t}\right)\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019173 +o rules:numerics
(FPCore (x y z t)
  :name "System.Random.MWC.Distributions:truncatedExp from mwc-random-0.13.3.2"

  :herbie-target
  (if (< z -2.8874623088207947e+119) (- (- x (/ (/ (- 0.5) (* y t)) (* z z))) (* (/ (- 0.5) (* y t)) (/ (/ 2.0 z) (* z z)))) (- x (/ (log (+ 1.0 (* z y))) t)))

  (- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))