expfmod (used to be hard to sample)

Percentage Accurate: 8.6% → 40.6%
Time: 38.5s
Alternatives: 7
Speedup: 1.9×

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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    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}

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 7 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: 8.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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    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: 40.6% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\left|\pi\right| + \pi}{2}\\ t_1 := \pi + \left|\pi\right|\\ t_2 := 2 \cdot \left(\cos \left(0.25 \cdot t\_1\right) \cdot \sin \left(-0.25 \cdot t\_1\right)\right)\\ \mathbf{if}\;x \leq -5 \cdot 10^{-19}:\\ \;\;\;\;\left(\left(1 + t\_2\right) \bmod \left(\sqrt{\cos t\_2}\right)\right) \cdot e^{-t\_2}\\ \mathbf{else}:\\ \;\;\;\;\left(\left(1 + x\right) \bmod \left(\sqrt{\mathsf{fma}\left(2, \sin \left(\frac{0 - t\_0}{2}\right) \cdot \cos \left(\frac{t\_0}{2}\right), \cos x\right)}\right)\right) \cdot e^{-x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (/ (+ (fabs PI) PI) 2.0))
        (t_1 (+ PI (fabs PI)))
        (t_2 (* 2.0 (* (cos (* 0.25 t_1)) (sin (* -0.25 t_1))))))
   (if (<= x -5e-19)
     (* (fmod (+ 1.0 t_2) (sqrt (cos t_2))) (exp (- t_2)))
     (*
      (fmod
       (+ 1.0 x)
       (sqrt
        (fma 2.0 (* (sin (/ (- 0.0 t_0) 2.0)) (cos (/ t_0 2.0))) (cos x))))
      (exp (- x))))))
double code(double x) {
	double t_0 = (fabs(((double) M_PI)) + ((double) M_PI)) / 2.0;
	double t_1 = ((double) M_PI) + fabs(((double) M_PI));
	double t_2 = 2.0 * (cos((0.25 * t_1)) * sin((-0.25 * t_1)));
	double tmp;
	if (x <= -5e-19) {
		tmp = fmod((1.0 + t_2), sqrt(cos(t_2))) * exp(-t_2);
	} else {
		tmp = fmod((1.0 + x), sqrt(fma(2.0, (sin(((0.0 - t_0) / 2.0)) * cos((t_0 / 2.0))), cos(x)))) * exp(-x);
	}
	return tmp;
}
function code(x)
	t_0 = Float64(Float64(abs(pi) + pi) / 2.0)
	t_1 = Float64(pi + abs(pi))
	t_2 = Float64(2.0 * Float64(cos(Float64(0.25 * t_1)) * sin(Float64(-0.25 * t_1))))
	tmp = 0.0
	if (x <= -5e-19)
		tmp = Float64(rem(Float64(1.0 + t_2), sqrt(cos(t_2))) * exp(Float64(-t_2)));
	else
		tmp = Float64(rem(Float64(1.0 + x), sqrt(fma(2.0, Float64(sin(Float64(Float64(0.0 - t_0) / 2.0)) * cos(Float64(t_0 / 2.0))), cos(x)))) * exp(Float64(-x)));
	end
	return tmp
end
code[x_] := Block[{t$95$0 = N[(N[(N[Abs[Pi], $MachinePrecision] + Pi), $MachinePrecision] / 2.0), $MachinePrecision]}, Block[{t$95$1 = N[(Pi + N[Abs[Pi], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(2.0 * N[(N[Cos[N[(0.25 * t$95$1), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(-0.25 * t$95$1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -5e-19], N[(N[With[{TMP1 = N[(1.0 + t$95$2), $MachinePrecision], TMP2 = N[Sqrt[N[Cos[t$95$2], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-t$95$2)], $MachinePrecision]), $MachinePrecision], N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[Sqrt[N[(2.0 * N[(N[Sin[N[(N[(0.0 - t$95$0), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(t$95$0 / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[Cos[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\left|\pi\right| + \pi}{2}\\
t_1 := \pi + \left|\pi\right|\\
t_2 := 2 \cdot \left(\cos \left(0.25 \cdot t\_1\right) \cdot \sin \left(-0.25 \cdot t\_1\right)\right)\\
\mathbf{if}\;x \leq -5 \cdot 10^{-19}:\\
\;\;\;\;\left(\left(1 + t\_2\right) \bmod \left(\sqrt{\cos t\_2}\right)\right) \cdot e^{-t\_2}\\

\mathbf{else}:\\
\;\;\;\;\left(\left(1 + x\right) \bmod \left(\sqrt{\mathsf{fma}\left(2, \sin \left(\frac{0 - t\_0}{2}\right) \cdot \cos \left(\frac{t\_0}{2}\right), \cos x\right)}\right)\right) \cdot e^{-x}\\


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

    1. Initial program 8.6%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Taylor expanded in x around 0

      \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    3. Applied rewrites38.8%

      \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    4. Applied rewrites39.3%

      \[\leadsto \left(\left(1 + \mathsf{fma}\left(2, \color{blue}{\sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right)}, x\right)\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    5. Applied rewrites39.3%

      \[\leadsto \left(\left(1 + \mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)\right) \bmod \left(\sqrt{\cos \color{blue}{\left(\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)\right)}}\right)\right) \cdot e^{-x} \]
    6. Applied rewrites39.4%

      \[\leadsto \left(\left(1 + \mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)\right) \bmod \left(\sqrt{\cos \left(\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)\right)}\right)\right) \cdot e^{-\color{blue}{\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)}} \]
    7. Taylor expanded in x around 0

      \[\leadsto \left(\left(1 + 2 \cdot \color{blue}{\left(\cos \left(\frac{1}{4} \cdot \left(\mathsf{PI}\left(\right) + \left|\mathsf{PI}\left(\right)\right|\right)\right) \cdot \sin \left(\frac{-1}{4} \cdot \left(\mathsf{PI}\left(\right) + \left|\mathsf{PI}\left(\right)\right|\right)\right)\right)}\right) \bmod \left(\sqrt{\cos \left(\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)\right)}\right)\right) \cdot e^{-\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)} \]
    8. Applied rewrites37.6%

      \[\leadsto \left(\left(1 + 2 \cdot \color{blue}{\left(\cos \left(0.25 \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(-0.25 \cdot \left(\pi + \left|\pi\right|\right)\right)\right)}\right) \bmod \left(\sqrt{\cos \left(\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)\right)}\right)\right) \cdot e^{-\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)} \]
    9. Taylor expanded in x around 0

      \[\leadsto \left(\left(1 + 2 \cdot \left(\cos \left(\frac{1}{4} \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(\frac{-1}{4} \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right) \bmod \left(\sqrt{\cos \color{blue}{\left(2 \cdot \left(\cos \left(\frac{1}{4} \cdot \left(\mathsf{PI}\left(\right) + \left|\mathsf{PI}\left(\right)\right|\right)\right) \cdot \sin \left(\frac{-1}{4} \cdot \left(\mathsf{PI}\left(\right) + \left|\mathsf{PI}\left(\right)\right|\right)\right)\right)\right)}}\right)\right) \cdot e^{-\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)} \]
    10. Applied rewrites38.0%

      \[\leadsto \left(\left(1 + 2 \cdot \left(\cos \left(0.25 \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(-0.25 \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right) \bmod \left(\sqrt{\cos \color{blue}{\left(2 \cdot \left(\cos \left(0.25 \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(-0.25 \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right)}}\right)\right) \cdot e^{-\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), x\right)} \]
    11. Taylor expanded in x around 0

      \[\leadsto \left(\left(1 + 2 \cdot \left(\cos \left(\frac{1}{4} \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(\frac{-1}{4} \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right) \bmod \left(\sqrt{\cos \left(2 \cdot \left(\cos \left(\frac{1}{4} \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(\frac{-1}{4} \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right)}\right)\right) \cdot e^{-\color{blue}{2 \cdot \left(\cos \left(\frac{1}{4} \cdot \left(\mathsf{PI}\left(\right) + \left|\mathsf{PI}\left(\right)\right|\right)\right) \cdot \sin \left(\frac{-1}{4} \cdot \left(\mathsf{PI}\left(\right) + \left|\mathsf{PI}\left(\right)\right|\right)\right)\right)}} \]
    12. Applied rewrites9.2%

      \[\leadsto \left(\left(1 + 2 \cdot \left(\cos \left(0.25 \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(-0.25 \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right) \bmod \left(\sqrt{\cos \left(2 \cdot \left(\cos \left(0.25 \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(-0.25 \cdot \left(\pi + \left|\pi\right|\right)\right)\right)\right)}\right)\right) \cdot e^{-\color{blue}{2 \cdot \left(\cos \left(0.25 \cdot \left(\pi + \left|\pi\right|\right)\right) \cdot \sin \left(-0.25 \cdot \left(\pi + \left|\pi\right|\right)\right)\right)}} \]

    if -5.0000000000000004e-19 < x

    1. Initial program 8.6%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Taylor expanded in x around 0

      \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    3. Applied rewrites38.8%

      \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    4. Applied rewrites38.8%

      \[\leadsto \left(\left(1 + x\right) \bmod \left(\sqrt{\color{blue}{\mathsf{fma}\left(2, \sin \left(\frac{0 - \frac{\left|\pi\right| + \pi}{2}}{2}\right) \cdot \cos \left(\frac{\frac{\left|\pi\right| + \pi}{2}}{2}\right), \cos x\right)}}\right)\right) \cdot e^{-x} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 40.4% accurate, 0.5× 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}\\ \mathbf{if}\;t\_0 \cdot t\_1 \leq 2:\\ \;\;\;\;\frac{t\_0}{e^{x}}\\ \mathbf{else}:\\ \;\;\;\;\left(1 \bmod \left(1 + -0.25 \cdot {x}^{2}\right)\right) \cdot t\_1\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (fmod (exp x) (sqrt (cos x)))) (t_1 (exp (- x))))
   (if (<= (* t_0 t_1) 2.0)
     (/ t_0 (exp x))
     (* (fmod 1.0 (+ 1.0 (* -0.25 (pow x 2.0)))) t_1))))
double code(double x) {
	double t_0 = fmod(exp(x), sqrt(cos(x)));
	double t_1 = exp(-x);
	double tmp;
	if ((t_0 * t_1) <= 2.0) {
		tmp = t_0 / exp(x);
	} else {
		tmp = fmod(1.0, (1.0 + (-0.25 * pow(x, 2.0)))) * t_1;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = mod(exp(x), sqrt(cos(x)))
    t_1 = exp(-x)
    if ((t_0 * t_1) <= 2.0d0) then
        tmp = t_0 / exp(x)
    else
        tmp = mod(1.0d0, (1.0d0 + ((-0.25d0) * (x ** 2.0d0)))) * t_1
    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)
	tmp = 0
	if (t_0 * t_1) <= 2.0:
		tmp = t_0 / math.exp(x)
	else:
		tmp = math.fmod(1.0, (1.0 + (-0.25 * math.pow(x, 2.0)))) * t_1
	return tmp
function code(x)
	t_0 = rem(exp(x), sqrt(cos(x)))
	t_1 = exp(Float64(-x))
	tmp = 0.0
	if (Float64(t_0 * t_1) <= 2.0)
		tmp = Float64(t_0 / exp(x));
	else
		tmp = Float64(rem(1.0, Float64(1.0 + Float64(-0.25 * (x ^ 2.0)))) * t_1);
	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]}, If[LessEqual[N[(t$95$0 * t$95$1), $MachinePrecision], 2.0], N[(t$95$0 / N[Exp[x], $MachinePrecision]), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = N[(1.0 + N[(-0.25 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $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}\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 2:\\
\;\;\;\;\frac{t\_0}{e^{x}}\\

\mathbf{else}:\\
\;\;\;\;\left(1 \bmod \left(1 + -0.25 \cdot {x}^{2}\right)\right) \cdot 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.6%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Applied rewrites8.6%

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\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 8.6%

      \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    2. Taylor expanded in x around 0

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

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

      \[\leadsto \left(1 \bmod \color{blue}{\left(1 + \frac{-1}{4} \cdot {x}^{2}\right)}\right) \cdot e^{-x} \]
    5. Applied rewrites35.6%

      \[\leadsto \left(1 \bmod \color{blue}{\left(1 + -0.25 \cdot {x}^{2}\right)}\right) \cdot e^{-x} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 38.8% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \left(\left(1 + x\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \frac{1}{e^{x}} \end{array} \]
(FPCore (x)
 :precision binary64
 (* (fmod (+ 1.0 x) (sqrt (cos x))) (/ 1.0 (exp x))))
double code(double x) {
	return fmod((1.0 + x), sqrt(cos(x))) * (1.0 / exp(x));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = mod((1.0d0 + x), sqrt(cos(x))) * (1.0d0 / exp(x))
end function
def code(x):
	return math.fmod((1.0 + x), math.sqrt(math.cos(x))) * (1.0 / math.exp(x))
function code(x)
	return Float64(rem(Float64(1.0 + x), sqrt(cos(x))) * Float64(1.0 / exp(x)))
end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(1.0 / N[Exp[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

    \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  2. Taylor expanded in x around 0

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  3. Applied rewrites38.8%

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  4. Applied rewrites38.8%

    \[\leadsto \left(\left(1 + x\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \color{blue}{\frac{1}{e^{x}}} \]
  5. Add Preprocessing

Alternative 4: 38.8% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \left(\left(1 + x\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \end{array} \]
(FPCore (x)
 :precision binary64
 (* (fmod (+ 1.0 x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
	return fmod((1.0 + x), sqrt(cos(x))) * exp(-x);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = mod((1.0d0 + x), sqrt(cos(x))) * exp(-x)
end function
def code(x):
	return math.fmod((1.0 + x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x)
	return Float64(rem(Float64(1.0 + x), sqrt(cos(x))) * exp(Float64(-x)))
end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + 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(1 + x\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x}
\end{array}
Derivation
  1. Initial program 8.6%

    \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  2. Taylor expanded in x around 0

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  3. Applied rewrites38.8%

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  4. Add Preprocessing

Alternative 5: 38.8% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \left(\left(1 + x\right) \bmod \left(1 + -0.25 \cdot {x}^{2}\right)\right) \cdot \frac{1}{e^{x}} \end{array} \]
(FPCore (x)
 :precision binary64
 (* (fmod (+ 1.0 x) (+ 1.0 (* -0.25 (pow x 2.0)))) (/ 1.0 (exp x))))
double code(double x) {
	return fmod((1.0 + x), (1.0 + (-0.25 * pow(x, 2.0)))) * (1.0 / exp(x));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = mod((1.0d0 + x), (1.0d0 + ((-0.25d0) * (x ** 2.0d0)))) * (1.0d0 / exp(x))
end function
def code(x):
	return math.fmod((1.0 + x), (1.0 + (-0.25 * math.pow(x, 2.0)))) * (1.0 / math.exp(x))
function code(x)
	return Float64(rem(Float64(1.0 + x), Float64(1.0 + Float64(-0.25 * (x ^ 2.0)))) * Float64(1.0 / exp(x)))
end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[(1.0 + N[(-0.25 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(1.0 / N[Exp[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(1 + x\right) \bmod \left(1 + -0.25 \cdot {x}^{2}\right)\right) \cdot \frac{1}{e^{x}}
\end{array}
Derivation
  1. Initial program 8.6%

    \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  2. Taylor expanded in x around 0

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  3. Applied rewrites38.8%

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  4. Taylor expanded in x around 0

    \[\leadsto \left(\left(1 + x\right) \bmod \color{blue}{\left(1 + \frac{-1}{4} \cdot {x}^{2}\right)}\right) \cdot e^{-x} \]
  5. Applied rewrites38.8%

    \[\leadsto \left(\left(1 + x\right) \bmod \color{blue}{\left(1 + -0.25 \cdot {x}^{2}\right)}\right) \cdot e^{-x} \]
  6. Applied rewrites38.8%

    \[\leadsto \left(\left(1 + x\right) \bmod \left(1 + -0.25 \cdot {x}^{2}\right)\right) \cdot \color{blue}{\frac{1}{e^{x}}} \]
  7. Add Preprocessing

Alternative 6: 38.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot e^{-x} \end{array} \]
(FPCore (x)
 :precision binary64
 (* (fmod (+ 1.0 x) (fma (* x x) -0.25 1.0)) (exp (- x))))
double code(double x) {
	return fmod((1.0 + x), fma((x * x), -0.25, 1.0)) * exp(-x);
}
function code(x)
	return Float64(rem(Float64(1.0 + x), fma(Float64(x * x), -0.25, 1.0)) * exp(Float64(-x)))
end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot e^{-x}
\end{array}
Derivation
  1. Initial program 8.6%

    \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  2. Taylor expanded in x around 0

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  3. Applied rewrites38.8%

    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  4. Taylor expanded in x around 0

    \[\leadsto \left(\left(1 + x\right) \bmod \color{blue}{\left(1 + \frac{-1}{4} \cdot {x}^{2}\right)}\right) \cdot e^{-x} \]
  5. Applied rewrites38.8%

    \[\leadsto \left(\left(1 + x\right) \bmod \color{blue}{\left(1 + -0.25 \cdot {x}^{2}\right)}\right) \cdot e^{-x} \]
  6. Applied rewrites38.8%

    \[\leadsto \color{blue}{\left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot e^{-x}} \]
  7. Add Preprocessing

Alternative 7: 6.4% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \end{array} \]
(FPCore (x) :precision binary64 (fmod (exp x) (fma (* x x) -0.25 1.0)))
double code(double x) {
	return fmod(exp(x), fma((x * x), -0.25, 1.0));
}
function code(x)
	return rem(exp(x), fma(Float64(x * x), -0.25, 1.0))
end
code[x_] := N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]
\begin{array}{l}

\\
\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right)
\end{array}
Derivation
  1. Initial program 8.6%

    \[\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)} \]
  3. Applied rewrites6.4%

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

    \[\leadsto \left(\left(e^{x}\right) \bmod \left(1 + \color{blue}{\frac{-1}{4} \cdot {x}^{2}}\right)\right) \]
  5. Applied rewrites6.4%

    \[\leadsto \left(\left(e^{x}\right) \bmod \left(1 + \color{blue}{-0.25 \cdot {x}^{2}}\right)\right) \]
  6. Applied rewrites6.4%

    \[\leadsto \color{blue}{\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right)} \]
  7. Add Preprocessing

Reproduce

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