Average Error: 24.8 → 8.1
Time: 2.8m
Precision: 64
\[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}\]
\[\begin{array}{l} \mathbf{if}\;z \le -4.85643818738512154771445265159152931803 \cdot 10^{-65}:\\ \;\;\;\;x - \frac{\log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right) + \left(\log \left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right) + \log \left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right)\right)}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \mathsf{fma}\left(\frac{z}{t}, \frac{y}{1}, \mathsf{fma}\left(\frac{z \cdot z}{t}, \frac{y}{1} \cdot \frac{1}{2}, \frac{\log \left(\sqrt{1}\right) \cdot 2}{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 -4.85643818738512154771445265159152931803 \cdot 10^{-65}:\\
\;\;\;\;x - \frac{\log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right) + \left(\log \left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right) + \log \left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right)\right)}{t}\\

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

\end{array}
double f(double x, double y, double z, double t) {
        double r16945004 = x;
        double r16945005 = 1.0;
        double r16945006 = y;
        double r16945007 = r16945005 - r16945006;
        double r16945008 = z;
        double r16945009 = exp(r16945008);
        double r16945010 = r16945006 * r16945009;
        double r16945011 = r16945007 + r16945010;
        double r16945012 = log(r16945011);
        double r16945013 = t;
        double r16945014 = r16945012 / r16945013;
        double r16945015 = r16945004 - r16945014;
        return r16945015;
}

double f(double x, double y, double z, double t) {
        double r16945016 = z;
        double r16945017 = -4.8564381873851215e-65;
        bool r16945018 = r16945016 <= r16945017;
        double r16945019 = x;
        double r16945020 = expm1(r16945016);
        double r16945021 = y;
        double r16945022 = 1.0;
        double r16945023 = fma(r16945020, r16945021, r16945022);
        double r16945024 = sqrt(r16945023);
        double r16945025 = log(r16945024);
        double r16945026 = log1p(r16945024);
        double r16945027 = expm1(r16945026);
        double r16945028 = sqrt(r16945027);
        double r16945029 = log(r16945028);
        double r16945030 = r16945029 + r16945029;
        double r16945031 = r16945025 + r16945030;
        double r16945032 = t;
        double r16945033 = r16945031 / r16945032;
        double r16945034 = r16945019 - r16945033;
        double r16945035 = r16945016 / r16945032;
        double r16945036 = r16945021 / r16945022;
        double r16945037 = r16945016 * r16945016;
        double r16945038 = r16945037 / r16945032;
        double r16945039 = 0.5;
        double r16945040 = r16945036 * r16945039;
        double r16945041 = sqrt(r16945022);
        double r16945042 = log(r16945041);
        double r16945043 = 2.0;
        double r16945044 = r16945042 * r16945043;
        double r16945045 = r16945044 / r16945032;
        double r16945046 = fma(r16945038, r16945040, r16945045);
        double r16945047 = fma(r16945035, r16945036, r16945046);
        double r16945048 = r16945019 - r16945047;
        double r16945049 = r16945018 ? r16945034 : r16945048;
        return r16945049;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Bits error versus t

Target

Original24.8
Target16.2
Herbie8.1
\[\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 < -4.8564381873851215e-65

    1. Initial program 14.1

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

      \[\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 add-sqr-sqrt11.6

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

      \[\leadsto x - \frac{\color{blue}{\log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right) + \log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)}}{t}\]
    6. Using strategy rm
    7. Applied expm1-log1p-u11.6

      \[\leadsto x - \frac{\log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right) + \log \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)\right)}}{t}\]
    8. Using strategy rm
    9. Applied add-sqr-sqrt11.6

      \[\leadsto x - \frac{\log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right) + \log \color{blue}{\left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)} \cdot \sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right)}}{t}\]
    10. Applied log-prod11.6

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

    if -4.8564381873851215e-65 < z

    1. Initial program 30.9

      \[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 add-sqr-sqrt11.5

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -4.85643818738512154771445265159152931803 \cdot 10^{-65}:\\ \;\;\;\;x - \frac{\log \left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right) + \left(\log \left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right) + \log \left(\sqrt{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\mathsf{fma}\left(\mathsf{expm1}\left(z\right), y, 1\right)}\right)\right)}\right)\right)}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \mathsf{fma}\left(\frac{z}{t}, \frac{y}{1}, \mathsf{fma}\left(\frac{z \cdot z}{t}, \frac{y}{1} \cdot \frac{1}{2}, \frac{\log \left(\sqrt{1}\right) \cdot 2}{t}\right)\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019200 +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)))