expfmod (used to be hard to sample)

Percentage Accurate: 6.9% → 62.7%
Time: 15.8s
Alternatives: 4
Speedup: 5.0×

Specification

?
\[\begin{array}{l} \\ \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \end{array} \]
(FPCore (x) :precision binary64 (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
	return fmod(exp(x), sqrt(cos(x))) * exp(-x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x):
	return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x)
	return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x)))
end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-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 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: 6.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \end{array} \]
(FPCore (x) :precision binary64 (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
	return fmod(exp(x), sqrt(cos(x))) * exp(-x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x):
	return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x)
	return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x)))
end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x}
\end{array}

Alternative 1: 62.7% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{\cos x}\\ t_1 := e^{-x}\\ \mathbf{if}\;\left(\left(e^{x}\right) \bmod t\_0\right) \cdot t\_1 \leq 2:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(t\_0 \cdot 0.6666666666666666 + \log \left(\sqrt[3]{e^{t\_0}}\right)\right)\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sqrt (cos x))) (t_1 (exp (- x))))
   (if (<= (* (fmod (exp x) t_0) t_1) 2.0)
     (/
      (fmod (exp x) (+ (* t_0 0.6666666666666666) (log (cbrt (exp t_0)))))
      (exp x))
     t_1)))
double code(double x) {
	double t_0 = sqrt(cos(x));
	double t_1 = exp(-x);
	double tmp;
	if ((fmod(exp(x), t_0) * t_1) <= 2.0) {
		tmp = fmod(exp(x), ((t_0 * 0.6666666666666666) + log(cbrt(exp(t_0))))) / exp(x);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x)
	t_0 = sqrt(cos(x))
	t_1 = exp(Float64(-x))
	tmp = 0.0
	if (Float64(rem(exp(x), t_0) * t_1) <= 2.0)
		tmp = Float64(rem(exp(x), Float64(Float64(t_0 * 0.6666666666666666) + log(cbrt(exp(t_0))))) / exp(x));
	else
		tmp = t_1;
	end
	return tmp
end
code[x_] := Block[{t$95$0 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision], 2.0], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(t$95$0 * 0.6666666666666666), $MachinePrecision] + N[Log[N[Power[N[Exp[t$95$0], $MachinePrecision], 1/3], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], t$95$1]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
t_1 := e^{-x}\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod t\_0\right) \cdot t\_1 \leq 2:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(t\_0 \cdot 0.6666666666666666 + \log \left(\sqrt[3]{e^{t\_0}}\right)\right)\right)}{e^{x}}\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 2

    1. Initial program 10.8%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Step-by-step derivation
      1. /-rgt-identity10.8%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
      2. associate-/r/10.8%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
      3. exp-neg10.8%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg10.8%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
    3. Simplified10.8%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-log-exp10.8%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\log \left(e^{\sqrt{\cos x}}\right)}\right)}{e^{x}} \]
      2. add-cube-cbrt58.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \log \color{blue}{\left(\left(\sqrt[3]{e^{\sqrt{\cos x}}} \cdot \sqrt[3]{e^{\sqrt{\cos x}}}\right) \cdot \sqrt[3]{e^{\sqrt{\cos x}}}\right)}\right)}{e^{x}} \]
      3. log-prod58.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(\log \left(\sqrt[3]{e^{\sqrt{\cos x}}} \cdot \sqrt[3]{e^{\sqrt{\cos x}}}\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)}\right)}{e^{x}} \]
      4. pow258.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\log \color{blue}{\left({\left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)}^{2}\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
    6. Applied egg-rr58.5%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(\log \left({\left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)}^{2}\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)}\right)}{e^{x}} \]
    7. Step-by-step derivation
      1. unpow258.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\log \color{blue}{\left(\sqrt[3]{e^{\sqrt{\cos x}}} \cdot \sqrt[3]{e^{\sqrt{\cos x}}}\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      2. log-prod58.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\color{blue}{\left(\log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      3. pow1/358.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\left(\log \color{blue}{\left({\left(e^{\sqrt{\cos x}}\right)}^{0.3333333333333333}\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      4. log-pow58.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\left(\color{blue}{0.3333333333333333 \cdot \log \left(e^{\sqrt{\cos x}}\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      5. add-log-exp58.5%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\left(0.3333333333333333 \cdot \color{blue}{\sqrt{\cos x}} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      6. pow1/358.6%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\left(0.3333333333333333 \cdot \sqrt{\cos x} + \log \color{blue}{\left({\left(e^{\sqrt{\cos x}}\right)}^{0.3333333333333333}\right)}\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      7. log-pow58.6%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\left(0.3333333333333333 \cdot \sqrt{\cos x} + \color{blue}{0.3333333333333333 \cdot \log \left(e^{\sqrt{\cos x}}\right)}\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      8. add-log-exp58.6%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\left(0.3333333333333333 \cdot \sqrt{\cos x} + 0.3333333333333333 \cdot \color{blue}{\sqrt{\cos x}}\right) + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
    8. Applied egg-rr58.6%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\color{blue}{\left(0.3333333333333333 \cdot \sqrt{\cos x} + 0.3333333333333333 \cdot \sqrt{\cos x}\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
    9. Step-by-step derivation
      1. distribute-rgt-out58.6%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\color{blue}{\sqrt{\cos x} \cdot \left(0.3333333333333333 + 0.3333333333333333\right)} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
      2. metadata-eval58.6%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x} \cdot \color{blue}{0.6666666666666666} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]
    10. Simplified58.6%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\color{blue}{\sqrt{\cos x} \cdot 0.6666666666666666} + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}} \]

    if 2 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x)))

    1. Initial program 0.0%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Step-by-step derivation
      1. /-rgt-identity0.0%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
      2. associate-/r/0.0%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
      3. exp-neg0.0%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg0.0%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
    3. Simplified0.0%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log0.0%

        \[\leadsto \frac{\color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}}{e^{x}} \]
      2. div-exp0.1%

        \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Applied egg-rr0.1%

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    7. Taylor expanded in x around 0 0.1%

      \[\leadsto e^{\log \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) - x} \]
    8. Taylor expanded in x around inf 96.3%

      \[\leadsto e^{\color{blue}{-1 \cdot x}} \]
    9. Step-by-step derivation
      1. neg-mul-196.3%

        \[\leadsto e^{\color{blue}{-x}} \]
    10. Simplified96.3%

      \[\leadsto e^{\color{blue}{-x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification66.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \leq 2:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x} \cdot 0.6666666666666666 + \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 61.7% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-16}:\\ \;\;\;\;e^{\log \left(\left(e^{x}\right) \bmod 1\right) - x}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -2e-16) (exp (- (log (fmod (exp x) 1.0)) x)) (exp (- x))))
double code(double x) {
	double tmp;
	if (x <= -2e-16) {
		tmp = exp((log(fmod(exp(x), 1.0)) - x));
	} else {
		tmp = exp(-x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-2d-16)) then
        tmp = exp((log(mod(exp(x), 1.0d0)) - x))
    else
        tmp = exp(-x)
    end if
    code = tmp
end function
def code(x):
	tmp = 0
	if x <= -2e-16:
		tmp = math.exp((math.log(math.fmod(math.exp(x), 1.0)) - x))
	else:
		tmp = math.exp(-x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -2e-16)
		tmp = exp(Float64(log(rem(exp(x), 1.0)) - x));
	else
		tmp = exp(Float64(-x));
	end
	return tmp
end
code[x_] := If[LessEqual[x, -2e-16], N[Exp[N[(N[Log[N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]], $MachinePrecision] - x), $MachinePrecision]], $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-16}:\\
\;\;\;\;e^{\log \left(\left(e^{x}\right) \bmod 1\right) - x}\\

\mathbf{else}:\\
\;\;\;\;e^{-x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2e-16

    1. Initial program 85.4%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Step-by-step derivation
      1. /-rgt-identity85.4%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
      2. associate-/r/85.4%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
      3. exp-neg85.7%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg85.7%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
    3. Simplified85.7%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log85.7%

        \[\leadsto \frac{\color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}}{e^{x}} \]
      2. div-exp86.2%

        \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Applied egg-rr86.2%

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    7. Taylor expanded in x around 0 86.2%

      \[\leadsto e^{\log \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) - x} \]

    if -2e-16 < x

    1. Initial program 4.2%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Step-by-step derivation
      1. /-rgt-identity4.2%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
      2. associate-/r/4.2%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
      3. exp-neg4.2%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg4.2%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
    3. Simplified4.2%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log4.2%

        \[\leadsto \frac{\color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}}{e^{x}} \]
      2. div-exp4.2%

        \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Applied egg-rr4.2%

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    7. Taylor expanded in x around 0 4.0%

      \[\leadsto e^{\log \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) - x} \]
    8. Taylor expanded in x around inf 64.4%

      \[\leadsto e^{\color{blue}{-1 \cdot x}} \]
    9. Step-by-step derivation
      1. neg-mul-164.4%

        \[\leadsto e^{\color{blue}{-x}} \]
    10. Simplified64.4%

      \[\leadsto e^{\color{blue}{-x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification65.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-16}:\\ \;\;\;\;e^{\log \left(\left(e^{x}\right) \bmod 1\right) - x}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 61.7% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-16}:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod 1\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -2e-16) (/ (fmod (exp x) 1.0) (exp x)) (exp (- x))))
double code(double x) {
	double tmp;
	if (x <= -2e-16) {
		tmp = fmod(exp(x), 1.0) / exp(x);
	} else {
		tmp = exp(-x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-2d-16)) then
        tmp = mod(exp(x), 1.0d0) / exp(x)
    else
        tmp = exp(-x)
    end if
    code = tmp
end function
def code(x):
	tmp = 0
	if x <= -2e-16:
		tmp = math.fmod(math.exp(x), 1.0) / math.exp(x)
	else:
		tmp = math.exp(-x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -2e-16)
		tmp = Float64(rem(exp(x), 1.0) / exp(x));
	else
		tmp = exp(Float64(-x));
	end
	return tmp
end
code[x_] := If[LessEqual[x, -2e-16], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-16}:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod 1\right)}{e^{x}}\\

\mathbf{else}:\\
\;\;\;\;e^{-x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2e-16

    1. Initial program 85.4%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Step-by-step derivation
      1. /-rgt-identity85.4%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
      2. associate-/r/85.4%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
      3. exp-neg85.7%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg85.7%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
    3. Simplified85.7%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0 85.7%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{1}\right)}{e^{x}} \]

    if -2e-16 < x

    1. Initial program 4.2%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Step-by-step derivation
      1. /-rgt-identity4.2%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
      2. associate-/r/4.2%

        \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
      3. exp-neg4.2%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg4.2%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
    3. Simplified4.2%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log4.2%

        \[\leadsto \frac{\color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}}{e^{x}} \]
      2. div-exp4.2%

        \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Applied egg-rr4.2%

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    7. Taylor expanded in x around 0 4.0%

      \[\leadsto e^{\log \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) - x} \]
    8. Taylor expanded in x around inf 64.4%

      \[\leadsto e^{\color{blue}{-1 \cdot x}} \]
    9. Step-by-step derivation
      1. neg-mul-164.4%

        \[\leadsto e^{\color{blue}{-x}} \]
    10. Simplified64.4%

      \[\leadsto e^{\color{blue}{-x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification65.6%

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

Alternative 4: 60.7% accurate, 5.0× speedup?

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

\\
e^{-x}
\end{array}
Derivation
  1. Initial program 8.6%

    \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  2. Step-by-step derivation
    1. /-rgt-identity8.6%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{1}} \cdot e^{-x} \]
    2. associate-/r/8.6%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\frac{1}{e^{-x}}}} \]
    3. exp-neg8.6%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
    4. remove-double-neg8.6%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{\color{blue}{x}}} \]
  3. Simplified8.6%

    \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. add-exp-log8.6%

      \[\leadsto \frac{\color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}}{e^{x}} \]
    2. div-exp8.7%

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
  6. Applied egg-rr8.7%

    \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
  7. Taylor expanded in x around 0 8.5%

    \[\leadsto e^{\log \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) - x} \]
  8. Taylor expanded in x around inf 63.3%

    \[\leadsto e^{\color{blue}{-1 \cdot x}} \]
  9. Step-by-step derivation
    1. neg-mul-163.3%

      \[\leadsto e^{\color{blue}{-x}} \]
  10. Simplified63.3%

    \[\leadsto e^{\color{blue}{-x}} \]
  11. Final simplification63.3%

    \[\leadsto e^{-x} \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024060 
(FPCore (x)
  :name "expfmod (used to be hard to sample)"
  :precision binary64
  (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))