expfmod (used to be hard to sample)

Percentage Accurate: 6.8% → 62.6%
Time: 16.6s
Alternatives: 5
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 5 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.8% 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.6% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{\cos x}\\ t_1 := e^{-x}\\ t_2 := \left(\left(e^{x}\right) \bmod t_0\right) \cdot t_1\\ \mathbf{if}\;t_2 \leq 0 \lor \neg \left(t_2 \leq 2\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(3 \cdot \left(t_0 \cdot 0.3333333333333333\right)\right)\right)}{e^{x}}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sqrt (cos x)))
        (t_1 (exp (- x)))
        (t_2 (* (fmod (exp x) t_0) t_1)))
   (if (or (<= t_2 0.0) (not (<= t_2 2.0)))
     t_1
     (/ (fmod (exp x) (* 3.0 (* t_0 0.3333333333333333))) (exp x)))))
double code(double x) {
	double t_0 = sqrt(cos(x));
	double t_1 = exp(-x);
	double t_2 = fmod(exp(x), t_0) * t_1;
	double tmp;
	if ((t_2 <= 0.0) || !(t_2 <= 2.0)) {
		tmp = t_1;
	} else {
		tmp = fmod(exp(x), (3.0 * (t_0 * 0.3333333333333333))) / exp(x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = sqrt(cos(x))
    t_1 = exp(-x)
    t_2 = mod(exp(x), t_0) * t_1
    if ((t_2 <= 0.0d0) .or. (.not. (t_2 <= 2.0d0))) then
        tmp = t_1
    else
        tmp = mod(exp(x), (3.0d0 * (t_0 * 0.3333333333333333d0))) / exp(x)
    end if
    code = tmp
end function
def code(x):
	t_0 = math.sqrt(math.cos(x))
	t_1 = math.exp(-x)
	t_2 = math.fmod(math.exp(x), t_0) * t_1
	tmp = 0
	if (t_2 <= 0.0) or not (t_2 <= 2.0):
		tmp = t_1
	else:
		tmp = math.fmod(math.exp(x), (3.0 * (t_0 * 0.3333333333333333))) / math.exp(x)
	return tmp
function code(x)
	t_0 = sqrt(cos(x))
	t_1 = exp(Float64(-x))
	t_2 = Float64(rem(exp(x), t_0) * t_1)
	tmp = 0.0
	if ((t_2 <= 0.0) || !(t_2 <= 2.0))
		tmp = t_1;
	else
		tmp = Float64(rem(exp(x), Float64(3.0 * Float64(t_0 * 0.3333333333333333))) / exp(x));
	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]}, Block[{t$95$2 = N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision]}, If[Or[LessEqual[t$95$2, 0.0], N[Not[LessEqual[t$95$2, 2.0]], $MachinePrecision]], t$95$1, N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(3.0 * N[(t$95$0 * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
t_1 := e^{-x}\\
t_2 := \left(\left(e^{x}\right) \bmod t_0\right) \cdot t_1\\
\mathbf{if}\;t_2 \leq 0 \lor \neg \left(t_2 \leq 2\right):\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(3 \cdot \left(t_0 \cdot 0.3333333333333333\right)\right)\right)}{e^{x}}\\


\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))) < 0.0 or 2 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x)))

    1. Initial program 3.6%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified61.0%

      \[\leadsto e^{\color{blue}{-x}} \]

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

    1. Initial program 77.8%

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

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

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

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

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt[3]{{\color{blue}{\left({\cos x}^{0.5}\right)}}^{3}}\right)\right)}{e^{x}} \]
      4. pow-pow78.6%

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

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

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(\sqrt[3]{{\cos x}^{1.5}}\right)}\right)}{e^{x}} \]
    6. Step-by-step derivation
      1. pow1/378.0%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left({\left({\cos x}^{1.5}\right)}^{0.3333333333333333}\right)}\right)}{e^{x}} \]
      2. pow-pow78.2%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left({\cos x}^{\left(1.5 \cdot 0.3333333333333333\right)}\right)}\right)}{e^{x}} \]
      3. metadata-eval78.2%

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left({\cos x}^{\color{blue}{0.5}}\right)\right)}{e^{x}} \]
      4. pow1/278.2%

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

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

        \[\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}} \]
      7. pow375.4%

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

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

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

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

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

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

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(\left(0.3333333333333333 \cdot \sqrt{\cos x}\right) \cdot 3\right)}\right)}{e^{x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification61.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \leq 0 \lor \neg \left(\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \leq 2\right):\\ \;\;\;\;e^{-x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(3 \cdot \left(\sqrt{\cos x} \cdot 0.3333333333333333\right)\right)\right)}{e^{x}}\\ \end{array} \]

Alternative 2: 62.6% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\\ t_1 := e^{-x}\\ t_2 := t_0 \cdot t_1\\ \mathbf{if}\;t_2 \leq 0 \lor \neg \left(t_2 \leq 2\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{e^{x}}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (fmod (exp x) (sqrt (cos x))))
        (t_1 (exp (- x)))
        (t_2 (* t_0 t_1)))
   (if (or (<= t_2 0.0) (not (<= t_2 2.0))) t_1 (/ t_0 (exp x)))))
double code(double x) {
	double t_0 = fmod(exp(x), sqrt(cos(x)));
	double t_1 = exp(-x);
	double t_2 = t_0 * t_1;
	double tmp;
	if ((t_2 <= 0.0) || !(t_2 <= 2.0)) {
		tmp = t_1;
	} else {
		tmp = t_0 / exp(x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = mod(exp(x), sqrt(cos(x)))
    t_1 = exp(-x)
    t_2 = t_0 * t_1
    if ((t_2 <= 0.0d0) .or. (.not. (t_2 <= 2.0d0))) then
        tmp = t_1
    else
        tmp = t_0 / exp(x)
    end if
    code = tmp
end function
def code(x):
	t_0 = math.fmod(math.exp(x), math.sqrt(math.cos(x)))
	t_1 = math.exp(-x)
	t_2 = t_0 * t_1
	tmp = 0
	if (t_2 <= 0.0) or not (t_2 <= 2.0):
		tmp = t_1
	else:
		tmp = t_0 / math.exp(x)
	return tmp
function code(x)
	t_0 = rem(exp(x), sqrt(cos(x)))
	t_1 = exp(Float64(-x))
	t_2 = Float64(t_0 * t_1)
	tmp = 0.0
	if ((t_2 <= 0.0) || !(t_2 <= 2.0))
		tmp = t_1;
	else
		tmp = Float64(t_0 / exp(x));
	end
	return tmp
end
code[x_] := Block[{t$95$0 = N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]}, Block[{t$95$1 = N[Exp[(-x)], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$0 * t$95$1), $MachinePrecision]}, If[Or[LessEqual[t$95$2, 0.0], N[Not[LessEqual[t$95$2, 2.0]], $MachinePrecision]], t$95$1, N[(t$95$0 / N[Exp[x], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\\
t_1 := e^{-x}\\
t_2 := t_0 \cdot t_1\\
\mathbf{if}\;t_2 \leq 0 \lor \neg \left(t_2 \leq 2\right):\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{e^{x}}\\


\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))) < 0.0 or 2 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x)))

    1. Initial program 3.6%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified61.0%

      \[\leadsto e^{\color{blue}{-x}} \]

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

    1. Initial program 77.8%

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

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

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

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

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification61.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \leq 0 \lor \neg \left(\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \leq 2\right):\\ \;\;\;\;e^{-x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\ \end{array} \]

Alternative 3: 62.5% accurate, 0.4× 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(3 \cdot \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) (* 3.0 (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), (3.0 * 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(3.0 * 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[(3.0 * 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(3 \cdot \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 8.8%

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

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

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

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

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

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

        \[\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-prod52.6%

        \[\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. pow252.6%

        \[\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}} \]
    5. Applied egg-rr52.6%

      \[\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}} \]
    6. Step-by-step derivation
      1. log-pow52.6%

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

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

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

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(3 \cdot \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. exp-neg0.0%

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified100.0%

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

    \[\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(3 \cdot \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]

Alternative 4: 62.1% accurate, 0.5× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;t_0\\


\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 8.8%

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

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

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

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

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

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

        \[\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-prod52.6%

        \[\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. pow252.6%

        \[\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}} \]
    5. Applied egg-rr52.6%

      \[\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}} \]
    6. Step-by-step derivation
      1. log-pow52.6%

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

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

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

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(3 \cdot \log \left(\sqrt[3]{e^{\sqrt{\cos x}}}\right)\right)}\right)}{e^{x}} \]
    8. Taylor expanded in x around 0 8.0%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(3 \cdot \log \color{blue}{\left({\left(e^{1}\right)}^{0.3333333333333333}\right)}\right)\right)}{e^{x}} \]
    9. Step-by-step derivation
      1. unpow1/352.0%

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(3 \cdot \log \left(\sqrt[3]{\color{blue}{e}}\right)\right)\right)}{e^{x}} \]
    10. Simplified52.0%

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(3 \cdot \log \color{blue}{\left(\sqrt[3]{e}\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. exp-neg0.0%

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified100.0%

      \[\leadsto e^{\color{blue}{-x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification61.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(3 \cdot \log \left(\sqrt[3]{e}\right)\right)\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]

Alternative 5: 60.6% 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 7.1%

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

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

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot 1}{e^{x}}} \]
    3. *-rgt-identity7.1%

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

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

    \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{1}\right)}{e^{x}} \]
  5. Step-by-step derivation
    1. add-exp-log6.5%

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

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

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

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

      \[\leadsto e^{\color{blue}{-x}} \]
  9. Simplified59.4%

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

    \[\leadsto e^{-x} \]

Reproduce

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