expfmod (used to be hard to sample)

Percentage Accurate: 6.6% → 62.5%
Time: 17.5s
Alternatives: 5
Speedup: 505.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.6% 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.5% accurate, 0.2× speedup?

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

\\
\begin{array}{l}
t_0 := \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\\
t_1 := 1 - t_0\\
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq 40:\\
\;\;\;\;\frac{1}{t_1} - \frac{{t_0}^{2}}{\sqrt[3]{{t_1}^{3}}}\\

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


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

    1. Initial program 9.8%

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

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

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

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

      \[\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-exp-log9.9%

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

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

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Step-by-step derivation
      1. add-cube-cbrt9.9%

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

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

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

      \[\leadsto \color{blue}{1} \]

    if -5.00000000000023e-311 < x < 40

    1. Initial program 10.2%

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

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

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

        \[\leadsto \frac{\color{blue}{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}{e^{x}} \]
    3. Simplified10.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-exp-log10.2%

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

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

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Step-by-step derivation
      1. exp-diff10.2%

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

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

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\right)\right)} \]
      4. expm1-udef10.2%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\right)} - 1} \]
      5. log1p-udef10.2%

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

        \[\leadsto \color{blue}{\left(1 + \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\right)} - 1 \]
      7. associate-+r-10.2%

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

        \[\leadsto \color{blue}{\frac{1 \cdot 1 - \left(\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}} - 1\right) \cdot \left(\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}} - 1\right)}{1 - \left(\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}} - 1\right)}} \]
      9. metadata-eval10.2%

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

      \[\leadsto \color{blue}{\frac{1}{1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)} - \frac{{\left(\mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{2}}{1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)}} \]
    8. Step-by-step derivation
      1. add-cbrt-cube10.3%

        \[\leadsto \frac{1}{1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)} - \frac{{\left(\mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{2}}{\color{blue}{\sqrt[3]{\left(\left(1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right) \cdot \left(1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)\right) \cdot \left(1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}}} \]
      2. pow310.3%

        \[\leadsto \frac{1}{1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)} - \frac{{\left(\mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{2}}{\sqrt[3]{\color{blue}{{\left(1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{3}}}} \]
    9. Applied egg-rr10.3%

      \[\leadsto \frac{1}{1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)} - \frac{{\left(\mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{2}}{\color{blue}{\sqrt[3]{{\left(1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{3}}}} \]

    if 40 < 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. 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.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 40:\\ \;\;\;\;\frac{1}{1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)} - \frac{{\left(\mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{2}}{\sqrt[3]{{\left(1 - \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\right)}^{3}}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]

Alternative 2: 62.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 400:\\ \;\;\;\;\frac{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + -1}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -5e-311)
   1.0
   (if (<= x 400.0)
     (/ (+ (+ 1.0 (fmod (exp x) (sqrt (cos x)))) -1.0) (exp x))
     (exp (- x)))))
double code(double x) {
	double tmp;
	if (x <= -5e-311) {
		tmp = 1.0;
	} else if (x <= 400.0) {
		tmp = ((1.0 + fmod(exp(x), sqrt(cos(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 <= (-5d-311)) then
        tmp = 1.0d0
    else if (x <= 400.0d0) then
        tmp = ((1.0d0 + mod(exp(x), sqrt(cos(x)))) + (-1.0d0)) / exp(x)
    else
        tmp = exp(-x)
    end if
    code = tmp
end function
def code(x):
	tmp = 0
	if x <= -5e-311:
		tmp = 1.0
	elif x <= 400.0:
		tmp = ((1.0 + math.fmod(math.exp(x), math.sqrt(math.cos(x)))) + -1.0) / math.exp(x)
	else:
		tmp = math.exp(-x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -5e-311)
		tmp = 1.0;
	elseif (x <= 400.0)
		tmp = Float64(Float64(Float64(1.0 + rem(exp(x), sqrt(cos(x)))) + -1.0) / exp(x));
	else
		tmp = exp(Float64(-x));
	end
	return tmp
end
code[x_] := If[LessEqual[x, -5e-311], 1.0, If[LessEqual[x, 400.0], N[(N[(N[(1.0 + N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq 400:\\
\;\;\;\;\frac{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + -1}{e^{x}}\\

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


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

    1. Initial program 9.8%

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

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

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

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

      \[\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-exp-log9.9%

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

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

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Step-by-step derivation
      1. add-cube-cbrt9.9%

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

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

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

      \[\leadsto \color{blue}{1} \]

    if -5.00000000000023e-311 < x < 400

    1. Initial program 10.2%

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

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

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

        \[\leadsto \frac{\color{blue}{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}}{e^{x}} \]
    3. Simplified10.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. expm1-log1p-u10.2%

        \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right)\right)}}{e^{x}} \]
      2. expm1-udef10.3%

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

        \[\leadsto \frac{e^{\color{blue}{\log \left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right)}} - 1}{e^{x}} \]
      4. add-exp-log10.3%

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

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

    if 400 < 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. 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.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 400:\\ \;\;\;\;\frac{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + -1}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]

Alternative 3: 62.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 400:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -5e-311)
   1.0
   (if (<= x 400.0) (/ (fmod (exp x) (sqrt (cos x))) (exp x)) (exp (- x)))))
double code(double x) {
	double tmp;
	if (x <= -5e-311) {
		tmp = 1.0;
	} else if (x <= 400.0) {
		tmp = fmod(exp(x), sqrt(cos(x))) / exp(x);
	} else {
		tmp = exp(-x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-5d-311)) then
        tmp = 1.0d0
    else if (x <= 400.0d0) then
        tmp = mod(exp(x), sqrt(cos(x))) / exp(x)
    else
        tmp = exp(-x)
    end if
    code = tmp
end function
def code(x):
	tmp = 0
	if x <= -5e-311:
		tmp = 1.0
	elif x <= 400.0:
		tmp = math.fmod(math.exp(x), math.sqrt(math.cos(x))) / math.exp(x)
	else:
		tmp = math.exp(-x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -5e-311)
		tmp = 1.0;
	elseif (x <= 400.0)
		tmp = Float64(rem(exp(x), sqrt(cos(x))) / exp(x));
	else
		tmp = exp(Float64(-x));
	end
	return tmp
end
code[x_] := If[LessEqual[x, -5e-311], 1.0, If[LessEqual[x, 400.0], 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], N[Exp[(-x)], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq 400:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\

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


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

    1. Initial program 9.8%

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

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

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

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

      \[\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-exp-log9.9%

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

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

      \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
    6. Step-by-step derivation
      1. add-cube-cbrt9.9%

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

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

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

      \[\leadsto \color{blue}{1} \]

    if -5.00000000000023e-311 < x < 400

    1. Initial program 10.2%

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

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

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

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

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

    if 400 < 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. 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.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 400:\\ \;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \]

Alternative 4: 60.0% 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.9%

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

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

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

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

    \[\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-exp-log7.9%

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

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

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

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

      \[\leadsto e^{\color{blue}{-x}} \]
  8. Simplified61.4%

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

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

Alternative 5: 42.4% accurate, 505.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (x) :precision binary64 1.0)
double code(double x) {
	return 1.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0
end function
public static double code(double x) {
	return 1.0;
}
def code(x):
	return 1.0
function code(x)
	return 1.0
end
function tmp = code(x)
	tmp = 1.0;
end
code[x_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 7.9%

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

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

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

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

    \[\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-exp-log7.9%

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

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

    \[\leadsto \color{blue}{e^{\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x}} \]
  6. Step-by-step derivation
    1. add-cube-cbrt7.9%

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

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

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

    \[\leadsto \color{blue}{1} \]
  9. Final simplification42.0%

    \[\leadsto 1 \]

Reproduce

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