Kahan's exp quotient

Percentage Accurate: 53.5% → 100.0%
Time: 11.2s
Alternatives: 18
Speedup: 8.8×

Specification

?
\[\begin{array}{l} \\ \frac{e^{x} - 1}{x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- (exp x) 1.0) x))
double code(double x) {
	return (exp(x) - 1.0) / x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - 1.0d0) / x
end function
public static double code(double x) {
	return (Math.exp(x) - 1.0) / x;
}
def code(x):
	return (math.exp(x) - 1.0) / x
function code(x)
	return Float64(Float64(exp(x) - 1.0) / x)
end
function tmp = code(x)
	tmp = (exp(x) - 1.0) / x;
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{e^{x} - 1}{x}
\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 18 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: 53.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{e^{x} - 1}{x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- (exp x) 1.0) x))
double code(double x) {
	return (exp(x) - 1.0) / x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - 1.0d0) / x
end function
public static double code(double x) {
	return (Math.exp(x) - 1.0) / x;
}
def code(x):
	return (math.exp(x) - 1.0) / x
function code(x)
	return Float64(Float64(exp(x) - 1.0) / x)
end
function tmp = code(x)
	tmp = (exp(x) - 1.0) / x;
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{e^{x} - 1}{x}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\mathsf{expm1}\left(x\right)}{x} \end{array} \]
(FPCore (x) :precision binary64 (/ (expm1 x) x))
double code(double x) {
	return expm1(x) / x;
}
public static double code(double x) {
	return Math.expm1(x) / x;
}
def code(x):
	return math.expm1(x) / x
function code(x)
	return Float64(expm1(x) / x)
end
code[x_] := N[(N[(Exp[x] - 1), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{\mathsf{expm1}\left(x\right)}{x}
\end{array}
Derivation
  1. Initial program 49.3%

    \[\frac{e^{x} - 1}{x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \frac{\color{blue}{e^{x} - 1}}{x} \]
    2. lift-exp.f64N/A

      \[\leadsto \frac{\color{blue}{e^{x}} - 1}{x} \]
    3. lower-expm1.f64100.0

      \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(x\right)}}{x} \]
  4. Applied rewrites100.0%

    \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(x\right)}}{x} \]
  5. Add Preprocessing

Alternative 2: 74.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\\ \mathbf{if}\;\frac{-1 + e^{x}}{x} \leq 2:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{fma}\left(x, -0.5, 1\right)}{x}}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x \cdot \left(x \cdot 0.16666666666666666\right), t\_0 \cdot t\_0, 1\right)}{1}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (* x (fma x 0.16666666666666666 0.5))))
   (if (<= (/ (+ -1.0 (exp x)) x) 2.0)
     (/ (/ 1.0 (/ (fma x -0.5 1.0) x)) x)
     (/ (fma (* x (* x 0.16666666666666666)) (* t_0 t_0) 1.0) 1.0))))
double code(double x) {
	double t_0 = x * fma(x, 0.16666666666666666, 0.5);
	double tmp;
	if (((-1.0 + exp(x)) / x) <= 2.0) {
		tmp = (1.0 / (fma(x, -0.5, 1.0) / x)) / x;
	} else {
		tmp = fma((x * (x * 0.16666666666666666)), (t_0 * t_0), 1.0) / 1.0;
	}
	return tmp;
}
function code(x)
	t_0 = Float64(x * fma(x, 0.16666666666666666, 0.5))
	tmp = 0.0
	if (Float64(Float64(-1.0 + exp(x)) / x) <= 2.0)
		tmp = Float64(Float64(1.0 / Float64(fma(x, -0.5, 1.0) / x)) / x);
	else
		tmp = Float64(fma(Float64(x * Float64(x * 0.16666666666666666)), Float64(t_0 * t_0), 1.0) / 1.0);
	end
	return tmp
end
code[x_] := Block[{t$95$0 = N[(x * N[(x * 0.16666666666666666 + 0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(-1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], 2.0], N[(N[(1.0 / N[(N[(x * -0.5 + 1.0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(N[(x * N[(x * 0.16666666666666666), $MachinePrecision]), $MachinePrecision] * N[(t$95$0 * t$95$0), $MachinePrecision] + 1.0), $MachinePrecision] / 1.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\\
\mathbf{if}\;\frac{-1 + e^{x}}{x} \leq 2:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{fma}\left(x, -0.5, 1\right)}{x}}}{x}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x \cdot \left(x \cdot 0.16666666666666666\right), t\_0 \cdot t\_0, 1\right)}{1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (-.f64 (exp.f64 x) #s(literal 1 binary64)) x) < 2

    1. Initial program 37.7%

      \[\frac{e^{x} - 1}{x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

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

        \[\leadsto \frac{x \cdot \color{blue}{\left(x \cdot \left(\frac{1}{2} + x \cdot \left(\frac{1}{6} + \frac{1}{24} \cdot x\right)\right) + 1\right)}}{x} \]
      2. distribute-lft-inN/A

        \[\leadsto \frac{\color{blue}{x \cdot \left(x \cdot \left(\frac{1}{2} + x \cdot \left(\frac{1}{6} + \frac{1}{24} \cdot x\right)\right)\right) + x \cdot 1}}{x} \]
      3. *-rgt-identityN/A

        \[\leadsto \frac{x \cdot \left(x \cdot \left(\frac{1}{2} + x \cdot \left(\frac{1}{6} + \frac{1}{24} \cdot x\right)\right)\right) + \color{blue}{x}}{x} \]
      4. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(x, x \cdot \left(\frac{1}{2} + x \cdot \left(\frac{1}{6} + \frac{1}{24} \cdot x\right)\right), x\right)}}{x} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(x, \color{blue}{x \cdot \left(\frac{1}{2} + x \cdot \left(\frac{1}{6} + \frac{1}{24} \cdot x\right)\right)}, x\right)}{x} \]
      6. +-commutativeN/A

        \[\leadsto \frac{\mathsf{fma}\left(x, x \cdot \color{blue}{\left(x \cdot \left(\frac{1}{6} + \frac{1}{24} \cdot x\right) + \frac{1}{2}\right)}, x\right)}{x} \]
      7. lower-fma.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(x, x \cdot \color{blue}{\mathsf{fma}\left(x, \frac{1}{6} + \frac{1}{24} \cdot x, \frac{1}{2}\right)}, x\right)}{x} \]
      8. +-commutativeN/A

        \[\leadsto \frac{\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, \color{blue}{\frac{1}{24} \cdot x + \frac{1}{6}}, \frac{1}{2}\right), x\right)}{x} \]
      9. *-commutativeN/A

        \[\leadsto \frac{\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{1}{24}} + \frac{1}{6}, \frac{1}{2}\right), x\right)}{x} \]
      10. lower-fma.f6466.5

        \[\leadsto \frac{\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.041666666666666664, 0.16666666666666666\right)}, 0.5\right), x\right)}{x} \]
    5. Applied rewrites66.5%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.041666666666666664, 0.16666666666666666\right), 0.5\right), x\right)}}{x} \]
    6. Step-by-step derivation
      1. Applied rewrites66.3%

        \[\leadsto \frac{\frac{1}{\color{blue}{\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.041666666666666664, 0.16666666666666666\right), 0.5\right), x \cdot x, x\right)}}}}{x} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{\frac{1}{\frac{1 + \frac{-1}{2} \cdot x}{\color{blue}{x}}}}{x} \]
      3. Step-by-step derivation
        1. Applied rewrites71.9%

          \[\leadsto \frac{\frac{1}{\frac{\mathsf{fma}\left(x, -0.5, 1\right)}{\color{blue}{x}}}}{x} \]

        if 2 < (/.f64 (-.f64 (exp.f64 x) #s(literal 1 binary64)) x)

        1. Initial program 100.0%

          \[\frac{e^{x} - 1}{x} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

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

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

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

            \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{1}{6} \cdot x + \frac{1}{2}}, 1\right) \]
          4. *-commutativeN/A

            \[\leadsto \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{1}{6}} + \frac{1}{2}, 1\right) \]
          5. lower-fma.f6454.0

            \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)}, 1\right) \]
        5. Applied rewrites54.0%

          \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right), 1\right)} \]
        6. Step-by-step derivation
          1. Applied rewrites8.3%

            \[\leadsto \frac{\mathsf{fma}\left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right), \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right) \cdot \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right), 1\right)}{\color{blue}{1 + \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right), -1\right)}} \]
          2. Taylor expanded in x around 0

            \[\leadsto \frac{\mathsf{fma}\left(x \cdot \mathsf{fma}\left(x, \frac{1}{6}, \frac{1}{2}\right), \left(x \cdot \mathsf{fma}\left(x, \frac{1}{6}, \frac{1}{2}\right)\right) \cdot \left(x \cdot \mathsf{fma}\left(x, \frac{1}{6}, \frac{1}{2}\right)\right), 1\right)}{1} \]
          3. Step-by-step derivation
            1. Applied rewrites83.7%

              \[\leadsto \frac{\mathsf{fma}\left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right), \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right) \cdot \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right), 1\right)}{1} \]
            2. Taylor expanded in x around inf

              \[\leadsto \frac{\mathsf{fma}\left(x \cdot \left(\frac{1}{6} \cdot x\right), \left(x \cdot \mathsf{fma}\left(x, \frac{1}{6}, \frac{1}{2}\right)\right) \cdot \left(x \cdot \mathsf{fma}\left(x, \frac{1}{6}, \frac{1}{2}\right)\right), 1\right)}{1} \]
            3. Step-by-step derivation
              1. Applied rewrites83.7%

                \[\leadsto \frac{\mathsf{fma}\left(x \cdot \left(x \cdot 0.16666666666666666\right), \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right) \cdot \left(x \cdot \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right)\right), 1\right)}{1} \]
            4. Recombined 2 regimes into one program.
            5. Final simplification74.9%

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

            Developer Target 1: 53.1% accurate, 0.4× speedup?

            \[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{x} - 1\\ \mathbf{if}\;x < 1 \land x > -1:\\ \;\;\;\;\frac{t\_0}{\log \left(e^{x}\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{t\_0}{x}\\ \end{array} \end{array} \]
            (FPCore (x)
             :precision binary64
             (let* ((t_0 (- (exp x) 1.0)))
               (if (and (< x 1.0) (> x -1.0)) (/ t_0 (log (exp x))) (/ t_0 x))))
            double code(double x) {
            	double t_0 = exp(x) - 1.0;
            	double tmp;
            	if ((x < 1.0) && (x > -1.0)) {
            		tmp = t_0 / log(exp(x));
            	} else {
            		tmp = t_0 / x;
            	}
            	return tmp;
            }
            
            real(8) function code(x)
                real(8), intent (in) :: x
                real(8) :: t_0
                real(8) :: tmp
                t_0 = exp(x) - 1.0d0
                if ((x < 1.0d0) .and. (x > (-1.0d0))) then
                    tmp = t_0 / log(exp(x))
                else
                    tmp = t_0 / x
                end if
                code = tmp
            end function
            
            public static double code(double x) {
            	double t_0 = Math.exp(x) - 1.0;
            	double tmp;
            	if ((x < 1.0) && (x > -1.0)) {
            		tmp = t_0 / Math.log(Math.exp(x));
            	} else {
            		tmp = t_0 / x;
            	}
            	return tmp;
            }
            
            def code(x):
            	t_0 = math.exp(x) - 1.0
            	tmp = 0
            	if (x < 1.0) and (x > -1.0):
            		tmp = t_0 / math.log(math.exp(x))
            	else:
            		tmp = t_0 / x
            	return tmp
            
            function code(x)
            	t_0 = Float64(exp(x) - 1.0)
            	tmp = 0.0
            	if ((x < 1.0) && (x > -1.0))
            		tmp = Float64(t_0 / log(exp(x)));
            	else
            		tmp = Float64(t_0 / x);
            	end
            	return tmp
            end
            
            function tmp_2 = code(x)
            	t_0 = exp(x) - 1.0;
            	tmp = 0.0;
            	if ((x < 1.0) && (x > -1.0))
            		tmp = t_0 / log(exp(x));
            	else
            		tmp = t_0 / x;
            	end
            	tmp_2 = tmp;
            end
            
            code[x_] := Block[{t$95$0 = N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision]}, If[And[Less[x, 1.0], Greater[x, -1.0]], N[(t$95$0 / N[Log[N[Exp[x], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(t$95$0 / x), $MachinePrecision]]]
            
            \begin{array}{l}
            
            \\
            \begin{array}{l}
            t_0 := e^{x} - 1\\
            \mathbf{if}\;x < 1 \land x > -1:\\
            \;\;\;\;\frac{t\_0}{\log \left(e^{x}\right)}\\
            
            \mathbf{else}:\\
            \;\;\;\;\frac{t\_0}{x}\\
            
            
            \end{array}
            \end{array}
            

            Reproduce

            ?
            herbie shell --seed 2024223 
            (FPCore (x)
              :name "Kahan's exp quotient"
              :precision binary64
            
              :alt
              (! :herbie-platform default (if (and (< x 1) (> x -1)) (/ (- (exp x) 1) (log (exp x))) (/ (- (exp x) 1) x)))
            
              (/ (- (exp x) 1.0) x))