expax (section 3.5)

Percentage Accurate: 54.1% → 100.0%
Time: 6.1s
Alternatives: 4
Speedup: 18.2×

Specification

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

\\
e^{a \cdot x} - 1
\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 4 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: 54.1% accurate, 1.0× speedup?

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

\\
e^{a \cdot x} - 1
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
\mathsf{expm1}\left(a \cdot x\right)
\end{array}
Derivation
  1. Initial program 55.7%

    \[e^{a \cdot x} - 1 \]
  2. Step-by-step derivation
    1. lift--.f64N/A

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

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

      \[\leadsto \color{blue}{\mathsf{expm1}\left(a \cdot x\right)} \]
    4. lift-*.f64N/A

      \[\leadsto \mathsf{expm1}\left(\color{blue}{a \cdot x}\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
    6. lower-*.f64100.0

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

    \[\leadsto \color{blue}{\mathsf{expm1}\left(x \cdot a\right)} \]
  4. Add Preprocessing
  5. Final simplification100.0%

    \[\leadsto \mathsf{expm1}\left(a \cdot x\right) \]
  6. Add Preprocessing

Alternative 2: 70.1% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a \cdot x \leq -10:\\ \;\;\;\;\frac{a \cdot x}{\left(-0.5 \cdot a\right) \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\left(a \cdot x\right) \cdot a, 0.5, a\right) \cdot x\\ \end{array} \end{array} \]
(FPCore (a x)
 :precision binary64
 (if (<= (* a x) -10.0)
   (/ (* a x) (* (* -0.5 a) x))
   (* (fma (* (* a x) a) 0.5 a) x)))
double code(double a, double x) {
	double tmp;
	if ((a * x) <= -10.0) {
		tmp = (a * x) / ((-0.5 * a) * x);
	} else {
		tmp = fma(((a * x) * a), 0.5, a) * x;
	}
	return tmp;
}
function code(a, x)
	tmp = 0.0
	if (Float64(a * x) <= -10.0)
		tmp = Float64(Float64(a * x) / Float64(Float64(-0.5 * a) * x));
	else
		tmp = Float64(fma(Float64(Float64(a * x) * a), 0.5, a) * x);
	end
	return tmp
end
code[a_, x_] := If[LessEqual[N[(a * x), $MachinePrecision], -10.0], N[(N[(a * x), $MachinePrecision] / N[(N[(-0.5 * a), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(a * x), $MachinePrecision] * a), $MachinePrecision] * 0.5 + a), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a \cdot x \leq -10:\\
\;\;\;\;\frac{a \cdot x}{\left(-0.5 \cdot a\right) \cdot x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(a \cdot x\right) \cdot a, 0.5, a\right) \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 a x) < -10

    1. Initial program 100.0%

      \[e^{a \cdot x} - 1 \]
    2. Step-by-step derivation
      1. lift--.f64N/A

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

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

        \[\leadsto \color{blue}{\mathsf{expm1}\left(a \cdot x\right)} \]
      4. lift-*.f64N/A

        \[\leadsto \mathsf{expm1}\left(\color{blue}{a \cdot x}\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
      6. lower-*.f64100.0

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

      \[\leadsto \color{blue}{\mathsf{expm1}\left(x \cdot a\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in a around 0

      \[\leadsto \color{blue}{a \cdot \left(x + a \cdot \left(\frac{1}{2} \cdot {x}^{2} + a \cdot \left(\frac{1}{24} \cdot \left(a \cdot {x}^{4}\right) + \frac{1}{6} \cdot {x}^{3}\right)\right)\right)} \]
    6. Applied rewrites0.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.041666666666666664 \cdot x, a, 0.16666666666666666\right), a \cdot x, 0.5\right) \cdot a, x, 1\right) \cdot \left(a \cdot x\right)} \]
    7. Step-by-step derivation
      1. Applied rewrites0.8%

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

        \[\leadsto \frac{a \cdot x}{1 + \color{blue}{\frac{-1}{2} \cdot \left(a \cdot x\right)}} \]
      3. Step-by-step derivation
        1. Applied rewrites15.4%

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

          \[\leadsto \frac{a \cdot x}{\frac{-1}{2} \cdot \left(a \cdot \color{blue}{x}\right)} \]
        3. Step-by-step derivation
          1. Applied rewrites15.5%

            \[\leadsto \frac{a \cdot x}{\left(-0.5 \cdot a\right) \cdot x} \]

          if -10 < (*.f64 a x)

          1. Initial program 35.9%

            \[e^{a \cdot x} - 1 \]
          2. Step-by-step derivation
            1. lift--.f64N/A

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

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

              \[\leadsto \color{blue}{\mathsf{expm1}\left(a \cdot x\right)} \]
            4. lift-*.f64N/A

              \[\leadsto \mathsf{expm1}\left(\color{blue}{a \cdot x}\right) \]
            5. *-commutativeN/A

              \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
            6. lower-*.f64100.0

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

            \[\leadsto \color{blue}{\mathsf{expm1}\left(x \cdot a\right)} \]
          4. Add Preprocessing
          5. Taylor expanded in a around 0

            \[\leadsto \color{blue}{a \cdot \left(x + \frac{1}{2} \cdot \left(a \cdot {x}^{2}\right)\right)} \]
          6. Step-by-step derivation
            1. distribute-lft-inN/A

              \[\leadsto \color{blue}{a \cdot x + a \cdot \left(\frac{1}{2} \cdot \left(a \cdot {x}^{2}\right)\right)} \]
            2. associate-*r*N/A

              \[\leadsto a \cdot x + a \cdot \color{blue}{\left(\left(\frac{1}{2} \cdot a\right) \cdot {x}^{2}\right)} \]
            3. unpow2N/A

              \[\leadsto a \cdot x + a \cdot \left(\left(\frac{1}{2} \cdot a\right) \cdot \color{blue}{\left(x \cdot x\right)}\right) \]
            4. associate-*r*N/A

              \[\leadsto a \cdot x + a \cdot \color{blue}{\left(\left(\left(\frac{1}{2} \cdot a\right) \cdot x\right) \cdot x\right)} \]
            5. *-commutativeN/A

              \[\leadsto a \cdot x + a \cdot \color{blue}{\left(x \cdot \left(\left(\frac{1}{2} \cdot a\right) \cdot x\right)\right)} \]
            6. associate-*r*N/A

              \[\leadsto a \cdot x + \color{blue}{\left(a \cdot x\right) \cdot \left(\left(\frac{1}{2} \cdot a\right) \cdot x\right)} \]
            7. *-commutativeN/A

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

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

              \[\leadsto \left(\color{blue}{x \cdot \left(\frac{1}{2} \cdot a\right)} + 1\right) \cdot \left(a \cdot x\right) \]
            10. associate-*r*N/A

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

              \[\leadsto \color{blue}{a \cdot x + \left(\left(x \cdot \frac{1}{2}\right) \cdot a\right) \cdot \left(a \cdot x\right)} \]
            12. associate-*r*N/A

              \[\leadsto a \cdot x + \color{blue}{\left(x \cdot \frac{1}{2}\right) \cdot \left(a \cdot \left(a \cdot x\right)\right)} \]
            13. associate-*l*N/A

              \[\leadsto a \cdot x + \left(x \cdot \frac{1}{2}\right) \cdot \color{blue}{\left(\left(a \cdot a\right) \cdot x\right)} \]
            14. unpow2N/A

              \[\leadsto a \cdot x + \left(x \cdot \frac{1}{2}\right) \cdot \left(\color{blue}{{a}^{2}} \cdot x\right) \]
            15. associate-*r*N/A

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

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

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

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

              \[\leadsto \color{blue}{\left(a + \frac{1}{2} \cdot \left({a}^{2} \cdot x\right)\right) \cdot x} \]
          7. Applied rewrites95.0%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\left(a \cdot a\right) \cdot x, 0.5, a\right) \cdot x} \]
          8. Step-by-step derivation
            1. Applied rewrites98.2%

              \[\leadsto \mathsf{fma}\left(\left(a \cdot x\right) \cdot a, 0.5, a\right) \cdot x \]
          9. Recombined 2 regimes into one program.
          10. Add Preprocessing

          Alternative 3: 70.4% accurate, 3.9× speedup?

          \[\begin{array}{l} \\ \frac{a}{\mathsf{fma}\left(-0.5, a \cdot x, 1\right)} \cdot x \end{array} \]
          (FPCore (a x) :precision binary64 (* (/ a (fma -0.5 (* a x) 1.0)) x))
          double code(double a, double x) {
          	return (a / fma(-0.5, (a * x), 1.0)) * x;
          }
          
          function code(a, x)
          	return Float64(Float64(a / fma(-0.5, Float64(a * x), 1.0)) * x)
          end
          
          code[a_, x_] := N[(N[(a / N[(-0.5 * N[(a * x), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \frac{a}{\mathsf{fma}\left(-0.5, a \cdot x, 1\right)} \cdot x
          \end{array}
          
          Derivation
          1. Initial program 55.7%

            \[e^{a \cdot x} - 1 \]
          2. Step-by-step derivation
            1. lift--.f64N/A

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

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

              \[\leadsto \color{blue}{\mathsf{expm1}\left(a \cdot x\right)} \]
            4. lift-*.f64N/A

              \[\leadsto \mathsf{expm1}\left(\color{blue}{a \cdot x}\right) \]
            5. *-commutativeN/A

              \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
            6. lower-*.f64100.0

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

            \[\leadsto \color{blue}{\mathsf{expm1}\left(x \cdot a\right)} \]
          4. Add Preprocessing
          5. Taylor expanded in a around 0

            \[\leadsto \color{blue}{a \cdot \left(x + a \cdot \left(\frac{1}{2} \cdot {x}^{2} + a \cdot \left(\frac{1}{24} \cdot \left(a \cdot {x}^{4}\right) + \frac{1}{6} \cdot {x}^{3}\right)\right)\right)} \]
          6. Applied rewrites68.8%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.041666666666666664 \cdot x, a, 0.16666666666666666\right), a \cdot x, 0.5\right) \cdot a, x, 1\right) \cdot \left(a \cdot x\right)} \]
          7. Step-by-step derivation
            1. Applied rewrites68.8%

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

              \[\leadsto \frac{a \cdot x}{1 + \color{blue}{\frac{-1}{2} \cdot \left(a \cdot x\right)}} \]
            3. Step-by-step derivation
              1. Applied rewrites72.7%

                \[\leadsto \frac{a \cdot x}{\mathsf{fma}\left(-0.5, \color{blue}{a \cdot x}, 1\right)} \]
              2. Step-by-step derivation
                1. Applied rewrites72.9%

                  \[\leadsto x \cdot \color{blue}{\frac{a}{\mathsf{fma}\left(-0.5, x \cdot a, 1\right)}} \]
                2. Final simplification72.9%

                  \[\leadsto \frac{a}{\mathsf{fma}\left(-0.5, a \cdot x, 1\right)} \cdot x \]
                3. Add Preprocessing

                Alternative 4: 66.4% accurate, 18.2× speedup?

                \[\begin{array}{l} \\ a \cdot x \end{array} \]
                (FPCore (a x) :precision binary64 (* a x))
                double code(double a, double x) {
                	return a * x;
                }
                
                real(8) function code(a, x)
                    real(8), intent (in) :: a
                    real(8), intent (in) :: x
                    code = a * x
                end function
                
                public static double code(double a, double x) {
                	return a * x;
                }
                
                def code(a, x):
                	return a * x
                
                function code(a, x)
                	return Float64(a * x)
                end
                
                function tmp = code(a, x)
                	tmp = a * x;
                end
                
                code[a_, x_] := N[(a * x), $MachinePrecision]
                
                \begin{array}{l}
                
                \\
                a \cdot x
                \end{array}
                
                Derivation
                1. Initial program 55.7%

                  \[e^{a \cdot x} - 1 \]
                2. Step-by-step derivation
                  1. lift--.f64N/A

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

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

                    \[\leadsto \color{blue}{\mathsf{expm1}\left(a \cdot x\right)} \]
                  4. lift-*.f64N/A

                    \[\leadsto \mathsf{expm1}\left(\color{blue}{a \cdot x}\right) \]
                  5. *-commutativeN/A

                    \[\leadsto \mathsf{expm1}\left(\color{blue}{x \cdot a}\right) \]
                  6. lower-*.f64100.0

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

                  \[\leadsto \color{blue}{\mathsf{expm1}\left(x \cdot a\right)} \]
                4. Add Preprocessing
                5. Taylor expanded in a around 0

                  \[\leadsto \color{blue}{a \cdot x} \]
                6. Step-by-step derivation
                  1. lower-*.f6469.0

                    \[\leadsto \color{blue}{a \cdot x} \]
                7. Applied rewrites69.0%

                  \[\leadsto \color{blue}{a \cdot x} \]
                8. Add Preprocessing

                Developer Target 1: 100.0% accurate, 1.0× speedup?

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

                Reproduce

                ?
                herbie shell --seed 2024272 
                (FPCore (a x)
                  :name "expax (section 3.5)"
                  :precision binary64
                  :pre (> 710.0 (* a x))
                
                  :alt
                  (! :herbie-platform default (expm1 (* a x)))
                
                  (- (exp (* a x)) 1.0))