expfmod (used to be hard to sample)

Percentage Accurate: 7.1% → 9.4%
Time: 18.8s
Alternatives: 6
Speedup: 1.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 6 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: 7.1% 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: 9.4% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}\\ \frac{\mathsf{fma}\left(t_0, t_0, -1\right) + -1}{e^{x}} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sqrt (+ (fmod (exp x) (sqrt (cos x))) 2.0))))
   (/ (+ (fma t_0 t_0 -1.0) -1.0) (exp x))))
double code(double x) {
	double t_0 = sqrt((fmod(exp(x), sqrt(cos(x))) + 2.0));
	return (fma(t_0, t_0, -1.0) + -1.0) / exp(x);
}
function code(x)
	t_0 = sqrt(Float64(rem(exp(x), sqrt(cos(x))) + 2.0))
	return Float64(Float64(fma(t_0, t_0, -1.0) + -1.0) / exp(x))
end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] + 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(t$95$0 * t$95$0 + -1.0), $MachinePrecision] + -1.0), $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}\\
\frac{\mathsf{fma}\left(t_0, t_0, -1\right) + -1}{e^{x}}
\end{array}
\end{array}
Derivation
  1. Initial program 5.7%

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

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

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

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

      \[\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-udef5.8%

      \[\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-udef5.8%

      \[\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. rem-exp-log5.8%

      \[\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-rr5.8%

    \[\leadsto \frac{\color{blue}{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) - 1}}{e^{x}} \]
  6. Step-by-step derivation
    1. expm1-log1p-u5.8%

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

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

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

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

      \[\leadsto \frac{\left(\color{blue}{\left(\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1\right)} - 1\right) - 1}{e^{x}} \]
    6. add-sqr-sqrt8.5%

      \[\leadsto \frac{\left(\color{blue}{\sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1} \cdot \sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}} - 1\right) - 1}{e^{x}} \]
    7. fma-neg8.5%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}, \sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}, -1\right)} - 1}{e^{x}} \]
  7. Applied egg-rr8.5%

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -1\right)} - 1}{e^{x}} \]
  8. Final simplification8.5%

    \[\leadsto \frac{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -1\right) + -1}{e^{x}} \]

Alternative 2: 9.4% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}\\ \frac{\mathsf{fma}\left(t_0, t_0, -2\right)}{e^{x}} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sqrt (+ (fmod (exp x) (sqrt (cos x))) 2.0))))
   (/ (fma t_0 t_0 -2.0) (exp x))))
double code(double x) {
	double t_0 = sqrt((fmod(exp(x), sqrt(cos(x))) + 2.0));
	return fma(t_0, t_0, -2.0) / exp(x);
}
function code(x)
	t_0 = sqrt(Float64(rem(exp(x), sqrt(cos(x))) + 2.0))
	return Float64(fma(t_0, t_0, -2.0) / exp(x))
end
code[x_] := Block[{t$95$0 = N[Sqrt[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] + 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(t$95$0 * t$95$0 + -2.0), $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}\\
\frac{\mathsf{fma}\left(t_0, t_0, -2\right)}{e^{x}}
\end{array}
\end{array}
Derivation
  1. Initial program 5.7%

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

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

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

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

      \[\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-udef5.8%

      \[\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-udef5.8%

      \[\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. rem-exp-log5.8%

      \[\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-rr5.8%

    \[\leadsto \frac{\color{blue}{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) - 1}}{e^{x}} \]
  6. Step-by-step derivation
    1. expm1-log1p-u5.8%

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

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

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

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

      \[\leadsto \frac{\left(\color{blue}{\left(\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1\right)} - 1\right) - 1}{e^{x}} \]
    6. add-sqr-sqrt8.5%

      \[\leadsto \frac{\left(\color{blue}{\sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1} \cdot \sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}} - 1\right) - 1}{e^{x}} \]
    7. fma-neg8.5%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}, \sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}, -1\right)} - 1}{e^{x}} \]
  7. Applied egg-rr8.5%

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -1\right)} - 1}{e^{x}} \]
  8. Step-by-step derivation
    1. fma-udef8.5%

      \[\leadsto \frac{\color{blue}{\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} \cdot \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} + -1\right)} - 1}{e^{x}} \]
    2. difference-of-sqr--18.5%

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

    \[\leadsto \frac{\color{blue}{\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} + 1\right) \cdot \left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} - 1\right)} - 1}{e^{x}} \]
  10. Step-by-step derivation
    1. difference-of-sqr--18.5%

      \[\leadsto \frac{\color{blue}{\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} \cdot \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} + -1\right)} - 1}{e^{x}} \]
    2. add-sqr-sqrt5.7%

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

      \[\leadsto \frac{\color{blue}{\left(\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2\right) + \left(-1 - 1\right)}}{e^{x}} \]
    4. metadata-eval5.7%

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

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -2\right)}}{e^{x}} \]
  11. Applied egg-rr8.5%

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -2\right)}}{e^{x}} \]
  12. Final simplification8.5%

    \[\leadsto \frac{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -2\right)}{e^{x}} \]

Alternative 3: 9.4% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
t_0 := \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}\\
\frac{\left(t_0 + 1\right) \cdot \left(t_0 + -1\right) + -1}{e^{x}}
\end{array}
\end{array}
Derivation
  1. Initial program 5.7%

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

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

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

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

      \[\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-udef5.8%

      \[\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-udef5.8%

      \[\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. rem-exp-log5.8%

      \[\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-rr5.8%

    \[\leadsto \frac{\color{blue}{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) - 1}}{e^{x}} \]
  6. Step-by-step derivation
    1. expm1-log1p-u5.8%

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

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

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

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

      \[\leadsto \frac{\left(\color{blue}{\left(\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1\right)} - 1\right) - 1}{e^{x}} \]
    6. add-sqr-sqrt8.5%

      \[\leadsto \frac{\left(\color{blue}{\sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1} \cdot \sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}} - 1\right) - 1}{e^{x}} \]
    7. fma-neg8.5%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}, \sqrt{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + 1}, -1\right)} - 1}{e^{x}} \]
  7. Applied egg-rr8.5%

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2}, -1\right)} - 1}{e^{x}} \]
  8. Step-by-step derivation
    1. fma-udef8.5%

      \[\leadsto \frac{\color{blue}{\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} \cdot \sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} + -1\right)} - 1}{e^{x}} \]
    2. difference-of-sqr--18.5%

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

    \[\leadsto \frac{\color{blue}{\left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} + 1\right) \cdot \left(\sqrt{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 2} - 1\right)} - 1}{e^{x}} \]
  10. Final simplification8.5%

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

Alternative 4: 6.9% accurate, 0.7× speedup?

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

\\
\frac{\log \left(e^{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}\right)}{e^{x}}
\end{array}
Derivation
  1. Initial program 5.7%

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

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

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

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

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

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

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

Alternative 5: 6.9% accurate, 1.0× speedup?

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

\\
\frac{\left(\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) + 1\right) + -1}{e^{x}}
\end{array}
Derivation
  1. Initial program 5.7%

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

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

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

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

      \[\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-udef5.8%

      \[\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-udef5.8%

      \[\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. rem-exp-log5.8%

      \[\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-rr5.8%

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

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

Alternative 6: 7.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{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(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}

\\
\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}
\end{array}
Derivation
  1. Initial program 5.7%

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

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

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

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

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

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

Reproduce

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