NMSE Section 6.1 mentioned, A

Percentage Accurate: 73.0% → 99.9%
Time: 7.2s
Alternatives: 3
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (/
  (-
   (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x))))
   (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x)))))
  2.0))
double code(double x, double eps) {
	return (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, eps)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = (((1.0d0 + (1.0d0 / eps)) * exp(-((1.0d0 - eps) * x))) - (((1.0d0 / eps) - 1.0d0) * exp(-((1.0d0 + eps) * x)))) / 2.0d0
end function
public static double code(double x, double eps) {
	return (((1.0 + (1.0 / eps)) * Math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * Math.exp(-((1.0 + eps) * x)))) / 2.0;
}
def code(x, eps):
	return (((1.0 + (1.0 / eps)) * math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * math.exp(-((1.0 + eps) * x)))) / 2.0
function code(x, eps)
	return Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * exp(Float64(-Float64(Float64(1.0 - eps) * x)))) - Float64(Float64(Float64(1.0 / eps) - 1.0) * exp(Float64(-Float64(Float64(1.0 + eps) * x))))) / 2.0)
end
function tmp = code(x, eps)
	tmp = (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
end
code[x_, eps_] := N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[(N[(1.0 - eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] - N[(N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision] * N[Exp[(-N[(N[(1.0 + eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2}
\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 3 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: 73.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (/
  (-
   (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x))))
   (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x)))))
  2.0))
double code(double x, double eps) {
	return (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, eps)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = (((1.0d0 + (1.0d0 / eps)) * exp(-((1.0d0 - eps) * x))) - (((1.0d0 / eps) - 1.0d0) * exp(-((1.0d0 + eps) * x)))) / 2.0d0
end function
public static double code(double x, double eps) {
	return (((1.0 + (1.0 / eps)) * Math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * Math.exp(-((1.0 + eps) * x)))) / 2.0;
}
def code(x, eps):
	return (((1.0 + (1.0 / eps)) * math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * math.exp(-((1.0 + eps) * x)))) / 2.0
function code(x, eps)
	return Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * exp(Float64(-Float64(Float64(1.0 - eps) * x)))) - Float64(Float64(Float64(1.0 / eps) - 1.0) * exp(Float64(-Float64(Float64(1.0 + eps) * x))))) / 2.0)
end
function tmp = code(x, eps)
	tmp = (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
end
code[x_, eps_] := N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[(N[(1.0 - eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] - N[(N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision] * N[Exp[(-N[(N[(1.0 + eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2}
\end{array}

Alternative 1: 99.9% accurate, N/A× speedup?

\[\begin{array}{l} eps_m = \left|\varepsilon\right| \\ \begin{array}{l} t_0 := \left(x - -1\right) \cdot e^{-x}\\ \mathbf{if}\;eps\_m \leq 4 \cdot 10^{-17}:\\ \;\;\;\;\frac{t\_0 + t\_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(1 + \frac{1}{eps\_m}\right) \cdot e^{\left(-1 + eps\_m\right) \cdot x} + \left(\frac{-1}{eps\_m} - -1\right) \cdot e^{e^{\mathsf{log1p}\left(eps\_m\right)} \cdot \left(-x\right)}}{2}\\ \end{array} \end{array} \]
eps_m = (fabs.f64 eps)
(FPCore (x eps_m)
 :precision binary64
 (let* ((t_0 (* (- x -1.0) (exp (- x)))))
   (if (<= eps_m 4e-17)
     (/ (+ t_0 t_0) 2.0)
     (/
      (+
       (* (+ 1.0 (/ 1.0 eps_m)) (exp (* (+ -1.0 eps_m) x)))
       (* (- (/ -1.0 eps_m) -1.0) (exp (* (exp (log1p eps_m)) (- x)))))
      2.0))))
eps_m = fabs(eps);
double code(double x, double eps_m) {
	double t_0 = (x - -1.0) * exp(-x);
	double tmp;
	if (eps_m <= 4e-17) {
		tmp = (t_0 + t_0) / 2.0;
	} else {
		tmp = (((1.0 + (1.0 / eps_m)) * exp(((-1.0 + eps_m) * x))) + (((-1.0 / eps_m) - -1.0) * exp((exp(log1p(eps_m)) * -x)))) / 2.0;
	}
	return tmp;
}
eps_m = Math.abs(eps);
public static double code(double x, double eps_m) {
	double t_0 = (x - -1.0) * Math.exp(-x);
	double tmp;
	if (eps_m <= 4e-17) {
		tmp = (t_0 + t_0) / 2.0;
	} else {
		tmp = (((1.0 + (1.0 / eps_m)) * Math.exp(((-1.0 + eps_m) * x))) + (((-1.0 / eps_m) - -1.0) * Math.exp((Math.exp(Math.log1p(eps_m)) * -x)))) / 2.0;
	}
	return tmp;
}
eps_m = math.fabs(eps)
def code(x, eps_m):
	t_0 = (x - -1.0) * math.exp(-x)
	tmp = 0
	if eps_m <= 4e-17:
		tmp = (t_0 + t_0) / 2.0
	else:
		tmp = (((1.0 + (1.0 / eps_m)) * math.exp(((-1.0 + eps_m) * x))) + (((-1.0 / eps_m) - -1.0) * math.exp((math.exp(math.log1p(eps_m)) * -x)))) / 2.0
	return tmp
eps_m = abs(eps)
function code(x, eps_m)
	t_0 = Float64(Float64(x - -1.0) * exp(Float64(-x)))
	tmp = 0.0
	if (eps_m <= 4e-17)
		tmp = Float64(Float64(t_0 + t_0) / 2.0);
	else
		tmp = Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps_m)) * exp(Float64(Float64(-1.0 + eps_m) * x))) + Float64(Float64(Float64(-1.0 / eps_m) - -1.0) * exp(Float64(exp(log1p(eps_m)) * Float64(-x))))) / 2.0);
	end
	return tmp
end
eps_m = N[Abs[eps], $MachinePrecision]
code[x_, eps$95$m_] := Block[{t$95$0 = N[(N[(x - -1.0), $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[eps$95$m, 4e-17], N[(N[(t$95$0 + t$95$0), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(N[(N[(1.0 + N[(1.0 / eps$95$m), $MachinePrecision]), $MachinePrecision] * N[Exp[N[(N[(-1.0 + eps$95$m), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(-1.0 / eps$95$m), $MachinePrecision] - -1.0), $MachinePrecision] * N[Exp[N[(N[Exp[N[Log[1 + eps$95$m], $MachinePrecision]], $MachinePrecision] * (-x)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]]
\begin{array}{l}
eps_m = \left|\varepsilon\right|

\\
\begin{array}{l}
t_0 := \left(x - -1\right) \cdot e^{-x}\\
\mathbf{if}\;eps\_m \leq 4 \cdot 10^{-17}:\\
\;\;\;\;\frac{t\_0 + t\_0}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{\left(1 + \frac{1}{eps\_m}\right) \cdot e^{\left(-1 + eps\_m\right) \cdot x} + \left(\frac{-1}{eps\_m} - -1\right) \cdot e^{e^{\mathsf{log1p}\left(eps\_m\right)} \cdot \left(-x\right)}}{2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if eps < 4.00000000000000029e-17

    1. Initial program 61.7%

      \[\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \]
    2. Add Preprocessing
    3. Taylor expanded in eps around 0

      \[\leadsto \frac{\color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
    4. Step-by-step derivation
      1. lower--.f64N/A

        \[\leadsto \frac{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \color{blue}{\left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
      2. distribute-rgt1-inN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      3. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      4. lower-+.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1} \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      5. lower-exp.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(-1 \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      6. lower-neg.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - \left(-1 \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      7. distribute-lft-outN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
      8. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
      9. distribute-rgt1-inN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      10. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      11. lower-+.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      12. lower-exp.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)}\right)}{2} \]
      13. lower-neg.f6466.6

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}{2} \]
    5. Applied rewrites66.6%

      \[\leadsto \frac{\color{blue}{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}}{2} \]

    if 4.00000000000000029e-17 < eps

    1. Initial program 100.0%

      \[\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\color{blue}{\left(1 + \varepsilon\right)} \cdot x}}{2} \]
      2. unpow1N/A

        \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\color{blue}{{\left(1 + \varepsilon\right)}^{1}} \cdot x}}{2} \]
      3. pow-to-expN/A

        \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\color{blue}{e^{\log \left(1 + \varepsilon\right) \cdot 1}} \cdot x}}{2} \]
      4. lower-exp.f64N/A

        \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\color{blue}{e^{\log \left(1 + \varepsilon\right) \cdot 1}} \cdot x}}{2} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-e^{\color{blue}{\log \left(1 + \varepsilon\right) \cdot 1}} \cdot x}}{2} \]
      6. lower-log1p.f64100.0

        \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-e^{\color{blue}{\mathsf{log1p}\left(\varepsilon\right)} \cdot 1} \cdot x}}{2} \]
    4. Applied rewrites100.0%

      \[\leadsto \frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\color{blue}{e^{\mathsf{log1p}\left(\varepsilon\right) \cdot 1}} \cdot x}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification76.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq 4 \cdot 10^{-17}:\\ \;\;\;\;\frac{\left(x - -1\right) \cdot e^{-x} + \left(x - -1\right) \cdot e^{-x}}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{\left(-1 + \varepsilon\right) \cdot x} + \left(\frac{-1}{\varepsilon} - -1\right) \cdot e^{e^{\mathsf{log1p}\left(\varepsilon\right)} \cdot \left(-x\right)}}{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 67.1% accurate, N/A× speedup?

\[\begin{array}{l} eps_m = \left|\varepsilon\right| \\ \begin{array}{l} t_0 := \left(x - -1\right) \cdot e^{-x}\\ \mathbf{if}\;eps\_m \leq 1.15 \cdot 10^{-6}:\\ \;\;\;\;\frac{t\_0 + t\_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2}\\ \end{array} \end{array} \]
eps_m = (fabs.f64 eps)
(FPCore (x eps_m)
 :precision binary64
 (let* ((t_0 (* (- x -1.0) (exp (- x)))))
   (if (<= eps_m 1.15e-6)
     (/ (+ t_0 t_0) 2.0)
     (/ (fma (- (* 0.6666666666666666 x) 1.0) (* x x) 2.0) 2.0))))
eps_m = fabs(eps);
double code(double x, double eps_m) {
	double t_0 = (x - -1.0) * exp(-x);
	double tmp;
	if (eps_m <= 1.15e-6) {
		tmp = (t_0 + t_0) / 2.0;
	} else {
		tmp = fma(((0.6666666666666666 * x) - 1.0), (x * x), 2.0) / 2.0;
	}
	return tmp;
}
eps_m = abs(eps)
function code(x, eps_m)
	t_0 = Float64(Float64(x - -1.0) * exp(Float64(-x)))
	tmp = 0.0
	if (eps_m <= 1.15e-6)
		tmp = Float64(Float64(t_0 + t_0) / 2.0);
	else
		tmp = Float64(fma(Float64(Float64(0.6666666666666666 * x) - 1.0), Float64(x * x), 2.0) / 2.0);
	end
	return tmp
end
eps_m = N[Abs[eps], $MachinePrecision]
code[x_, eps$95$m_] := Block[{t$95$0 = N[(N[(x - -1.0), $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[eps$95$m, 1.15e-6], N[(N[(t$95$0 + t$95$0), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(N[(N[(0.6666666666666666 * x), $MachinePrecision] - 1.0), $MachinePrecision] * N[(x * x), $MachinePrecision] + 2.0), $MachinePrecision] / 2.0), $MachinePrecision]]]
\begin{array}{l}
eps_m = \left|\varepsilon\right|

\\
\begin{array}{l}
t_0 := \left(x - -1\right) \cdot e^{-x}\\
\mathbf{if}\;eps\_m \leq 1.15 \cdot 10^{-6}:\\
\;\;\;\;\frac{t\_0 + t\_0}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if eps < 1.15e-6

    1. Initial program 61.9%

      \[\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \]
    2. Add Preprocessing
    3. Taylor expanded in eps around 0

      \[\leadsto \frac{\color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
    4. Step-by-step derivation
      1. lower--.f64N/A

        \[\leadsto \frac{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \color{blue}{\left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
      2. distribute-rgt1-inN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      3. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      4. lower-+.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1} \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      5. lower-exp.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(-1 \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      6. lower-neg.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - \left(-1 \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      7. distribute-lft-outN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
      8. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
      9. distribute-rgt1-inN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      10. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      11. lower-+.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      12. lower-exp.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)}\right)}{2} \]
      13. lower-neg.f6466.7

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}{2} \]
    5. Applied rewrites66.7%

      \[\leadsto \frac{\color{blue}{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}}{2} \]

    if 1.15e-6 < eps

    1. Initial program 100.0%

      \[\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \]
    2. Add Preprocessing
    3. Taylor expanded in eps around 0

      \[\leadsto \frac{\color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
    4. Step-by-step derivation
      1. lower--.f64N/A

        \[\leadsto \frac{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \color{blue}{\left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
      2. distribute-rgt1-inN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      3. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      4. lower-+.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1} \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      5. lower-exp.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(-1 \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      6. lower-neg.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - \left(-1 \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
      7. distribute-lft-outN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
      8. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
      9. distribute-rgt1-inN/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      10. lower-*.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      11. lower-+.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}}\right)}{2} \]
      12. lower-exp.f64N/A

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)}\right)}{2} \]
      13. lower-neg.f6424.5

        \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}{2} \]
    5. Applied rewrites24.5%

      \[\leadsto \frac{\color{blue}{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}}{2} \]
    6. Taylor expanded in x around 0

      \[\leadsto \frac{2 + \color{blue}{{x}^{2} \cdot \left(\frac{2}{3} \cdot x - 1\right)}}{2} \]
    7. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \frac{{x}^{2} \cdot \left(\frac{2}{3} \cdot x - 1\right) + 2}{2} \]
      2. *-commutativeN/A

        \[\leadsto \frac{\left(\frac{2}{3} \cdot x - 1\right) \cdot {x}^{2} + 2}{2} \]
      3. lower-fma.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, {x}^{\color{blue}{2}}, 2\right)}{2} \]
      4. lower--.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, {x}^{2}, 2\right)}{2} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, {x}^{2}, 2\right)}{2} \]
      6. unpow2N/A

        \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, x \cdot x, 2\right)}{2} \]
      7. lower-*.f6442.4

        \[\leadsto \frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2} \]
    8. Applied rewrites42.4%

      \[\leadsto \frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, \color{blue}{x \cdot x}, 2\right)}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification59.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq 1.15 \cdot 10^{-6}:\\ \;\;\;\;\frac{\left(x - -1\right) \cdot e^{-x} + \left(x - -1\right) \cdot e^{-x}}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 53.5% accurate, N/A× speedup?

\[\begin{array}{l} eps_m = \left|\varepsilon\right| \\ \frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2} \end{array} \]
eps_m = (fabs.f64 eps)
(FPCore (x eps_m)
 :precision binary64
 (/ (fma (- (* 0.6666666666666666 x) 1.0) (* x x) 2.0) 2.0))
eps_m = fabs(eps);
double code(double x, double eps_m) {
	return fma(((0.6666666666666666 * x) - 1.0), (x * x), 2.0) / 2.0;
}
eps_m = abs(eps)
function code(x, eps_m)
	return Float64(fma(Float64(Float64(0.6666666666666666 * x) - 1.0), Float64(x * x), 2.0) / 2.0)
end
eps_m = N[Abs[eps], $MachinePrecision]
code[x_, eps$95$m_] := N[(N[(N[(N[(0.6666666666666666 * x), $MachinePrecision] - 1.0), $MachinePrecision] * N[(x * x), $MachinePrecision] + 2.0), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
eps_m = \left|\varepsilon\right|

\\
\frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2}
\end{array}
Derivation
  1. Initial program 72.8%

    \[\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2} \]
  2. Add Preprocessing
  3. Taylor expanded in eps around 0

    \[\leadsto \frac{\color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
  4. Step-by-step derivation
    1. lower--.f64N/A

      \[\leadsto \frac{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right) - \color{blue}{\left(-1 \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}}{2} \]
    2. distribute-rgt1-inN/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
    3. lower-*.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1 \cdot e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
    4. lower-+.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(\color{blue}{-1} \cdot e^{\mathsf{neg}\left(x\right)} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
    5. lower-exp.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)} - \left(-1 \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
    6. lower-neg.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - \left(-1 \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}} + -1 \cdot \left(x \cdot e^{\mathsf{neg}\left(x\right)}\right)\right)}{2} \]
    7. distribute-lft-outN/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
    8. lower-*.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \color{blue}{\left(e^{\mathsf{neg}\left(x\right)} + x \cdot e^{\mathsf{neg}\left(x\right)}\right)}}{2} \]
    9. distribute-rgt1-inN/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
    10. lower-*.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot \color{blue}{e^{\mathsf{neg}\left(x\right)}}\right)}{2} \]
    11. lower-+.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\color{blue}{\mathsf{neg}\left(x\right)}}\right)}{2} \]
    12. lower-exp.f64N/A

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{\mathsf{neg}\left(x\right)}\right)}{2} \]
    13. lower-neg.f6454.7

      \[\leadsto \frac{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}{2} \]
  5. Applied rewrites54.7%

    \[\leadsto \frac{\color{blue}{\left(x + 1\right) \cdot e^{-x} - -1 \cdot \left(\left(x + 1\right) \cdot e^{-x}\right)}}{2} \]
  6. Taylor expanded in x around 0

    \[\leadsto \frac{2 + \color{blue}{{x}^{2} \cdot \left(\frac{2}{3} \cdot x - 1\right)}}{2} \]
  7. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \frac{{x}^{2} \cdot \left(\frac{2}{3} \cdot x - 1\right) + 2}{2} \]
    2. *-commutativeN/A

      \[\leadsto \frac{\left(\frac{2}{3} \cdot x - 1\right) \cdot {x}^{2} + 2}{2} \]
    3. lower-fma.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, {x}^{\color{blue}{2}}, 2\right)}{2} \]
    4. lower--.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, {x}^{2}, 2\right)}{2} \]
    5. lower-*.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, {x}^{2}, 2\right)}{2} \]
    6. unpow2N/A

      \[\leadsto \frac{\mathsf{fma}\left(\frac{2}{3} \cdot x - 1, x \cdot x, 2\right)}{2} \]
    7. lower-*.f6452.3

      \[\leadsto \frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, x \cdot x, 2\right)}{2} \]
  8. Applied rewrites52.3%

    \[\leadsto \frac{\mathsf{fma}\left(0.6666666666666666 \cdot x - 1, \color{blue}{x \cdot x}, 2\right)}{2} \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2025059 
(FPCore (x eps)
  :name "NMSE Section 6.1 mentioned, A"
  :precision binary64
  (/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))