expfmod (used to be hard to sample)

Percentage Accurate: 7.1% → 59.5%
Time: 10.8s
Alternatives: 4
Speedup: 3.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);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x):
	return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x)
	return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x)))
end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 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: 59.5% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
\mathbf{if}\;e^{-x} \cdot \left(\left(e^{x}\right) \bmod t\_0\right) \leq 0.001:\\
\;\;\;\;\left(1 - x\right) \cdot \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod t\_0\right)\\

\mathbf{else}:\\
\;\;\;\;\left(\left(1 + x\right) \bmod 1\right) \cdot \left(1 - x\right)\\


\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))) < 1e-3

    1. Initial program 6.4%

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

      \[\leadsto \left(\color{blue}{1} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
    4. Step-by-step derivation
      1. Applied rewrites4.5%

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

        \[\leadsto \left(1 \bmod \left(\sqrt{\cos x}\right)\right) \cdot \color{blue}{\left(1 + -1 \cdot x\right)} \]
      3. Step-by-step derivation
        1. neg-mul-1N/A

          \[\leadsto \left(1 \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right) \]
        2. unsub-negN/A

          \[\leadsto \left(1 \bmod \left(\sqrt{\cos x}\right)\right) \cdot \color{blue}{\left(1 - x\right)} \]
        3. lower--.f644.5

          \[\leadsto \left(1 \bmod \left(\sqrt{\cos x}\right)\right) \cdot \color{blue}{\left(1 - x\right)} \]
      4. Applied rewrites4.5%

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

        \[\leadsto \left(\color{blue}{\left(1 + x \cdot \left(1 + \frac{1}{2} \cdot x\right)\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
      6. Step-by-step derivation
        1. +-commutativeN/A

          \[\leadsto \left(\color{blue}{\left(x \cdot \left(1 + \frac{1}{2} \cdot x\right) + 1\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
        2. *-commutativeN/A

          \[\leadsto \left(\left(\color{blue}{\left(1 + \frac{1}{2} \cdot x\right) \cdot x} + 1\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
        3. lower-fma.f64N/A

          \[\leadsto \left(\color{blue}{\left(\mathsf{fma}\left(1 + \frac{1}{2} \cdot x, x, 1\right)\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
        4. +-commutativeN/A

          \[\leadsto \left(\left(\mathsf{fma}\left(\color{blue}{\frac{1}{2} \cdot x + 1}, x, 1\right)\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
        5. lower-fma.f646.2

          \[\leadsto \left(\left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(0.5, x, 1\right)}, x, 1\right)\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
      7. Applied rewrites6.2%

        \[\leadsto \left(\color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, 1\right), x, 1\right)\right)} \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
      8. Taylor expanded in x around inf

        \[\leadsto \left(\left({x}^{2} \cdot \color{blue}{\left(\frac{1}{2} + \frac{1}{x}\right)}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]
      9. Step-by-step derivation
        1. Applied rewrites57.6%

          \[\leadsto \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot \color{blue}{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot \left(1 - x\right) \]

        if 1e-3 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x)))

        1. Initial program 8.1%

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

          \[\leadsto \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) \cdot e^{-x} \]
        4. Step-by-step derivation
          1. Applied rewrites8.1%

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

            \[\leadsto \left(\left(e^{x}\right) \bmod 1\right) \cdot \color{blue}{1} \]
          3. Step-by-step derivation
            1. Applied rewrites5.8%

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

              \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
            3. Step-by-step derivation
              1. lower-+.f6495.8

                \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
            4. Applied rewrites95.8%

              \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
            5. Taylor expanded in x around 0

              \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 + -1 \cdot x\right)} \]
            6. Step-by-step derivation
              1. neg-mul-1N/A

                \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right) \]
              2. unsub-negN/A

                \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 - x\right)} \]
              3. lower--.f6496.7

                \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 - x\right)} \]
            7. Applied rewrites96.7%

              \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 - x\right)} \]
          4. Recombined 2 regimes into one program.
          5. Final simplification66.9%

            \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-x} \cdot \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \leq 0.001:\\ \;\;\;\;\left(1 - x\right) \cdot \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod \left(\sqrt{\cos x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\left(1 + x\right) \bmod 1\right) \cdot \left(1 - x\right)\\ \end{array} \]
          6. Add Preprocessing

          Alternative 2: 24.5% accurate, 3.7× speedup?

          \[\begin{array}{l} \\ \left(\left(1 + x\right) \bmod 1\right) \cdot \left(1 - x\right) \end{array} \]
          (FPCore (x) :precision binary64 (* (fmod (+ 1.0 x) 1.0) (- 1.0 x)))
          double code(double x) {
          	return fmod((1.0 + x), 1.0) * (1.0 - x);
          }
          
          real(8) function code(x)
              real(8), intent (in) :: x
              code = mod((1.0d0 + x), 1.0d0) * (1.0d0 - x)
          end function
          
          def code(x):
          	return math.fmod((1.0 + x), 1.0) * (1.0 - x)
          
          function code(x)
          	return Float64(rem(Float64(1.0 + x), 1.0) * Float64(1.0 - x))
          end
          
          code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \left(\left(1 + x\right) \bmod 1\right) \cdot \left(1 - x\right)
          \end{array}
          
          Derivation
          1. Initial program 6.8%

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

            \[\leadsto \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) \cdot e^{-x} \]
          4. Step-by-step derivation
            1. Applied rewrites6.5%

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

              \[\leadsto \left(\left(e^{x}\right) \bmod 1\right) \cdot \color{blue}{1} \]
            3. Step-by-step derivation
              1. Applied rewrites5.9%

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

                \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
              3. Step-by-step derivation
                1. lower-+.f6427.4

                  \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
              4. Applied rewrites27.4%

                \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
              5. Taylor expanded in x around 0

                \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 + -1 \cdot x\right)} \]
              6. Step-by-step derivation
                1. neg-mul-1N/A

                  \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right) \]
                2. unsub-negN/A

                  \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 - x\right)} \]
                3. lower--.f6427.6

                  \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 - x\right)} \]
              7. Applied rewrites27.6%

                \[\leadsto \left(\left(1 + x\right) \bmod 1\right) \cdot \color{blue}{\left(1 - x\right)} \]
              8. Add Preprocessing

              Alternative 3: 24.1% accurate, 3.8× speedup?

              \[\begin{array}{l} \\ 1 \cdot \left(\left(1 + x\right) \bmod 1\right) \end{array} \]
              (FPCore (x) :precision binary64 (* 1.0 (fmod (+ 1.0 x) 1.0)))
              double code(double x) {
              	return 1.0 * fmod((1.0 + x), 1.0);
              }
              
              real(8) function code(x)
                  real(8), intent (in) :: x
                  code = 1.0d0 * mod((1.0d0 + x), 1.0d0)
              end function
              
              def code(x):
              	return 1.0 * math.fmod((1.0 + x), 1.0)
              
              function code(x)
              	return Float64(1.0 * rem(Float64(1.0 + x), 1.0))
              end
              
              code[x_] := N[(1.0 * N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              1 \cdot \left(\left(1 + x\right) \bmod 1\right)
              \end{array}
              
              Derivation
              1. Initial program 6.8%

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

                \[\leadsto \left(\left(e^{x}\right) \bmod \color{blue}{1}\right) \cdot e^{-x} \]
              4. Step-by-step derivation
                1. Applied rewrites6.5%

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

                  \[\leadsto \left(\left(e^{x}\right) \bmod 1\right) \cdot \color{blue}{1} \]
                3. Step-by-step derivation
                  1. Applied rewrites5.9%

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

                    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
                  3. Step-by-step derivation
                    1. lower-+.f6427.4

                      \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
                  4. Applied rewrites27.4%

                    \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \cdot 1 \]
                  5. Final simplification27.4%

                    \[\leadsto 1 \cdot \left(\left(1 + x\right) \bmod 1\right) \]
                  6. Add Preprocessing

                  Alternative 4: 22.8% accurate, 3.9× speedup?

                  \[\begin{array}{l} \\ \left(1 \bmod 1\right) \cdot 1 \end{array} \]
                  (FPCore (x) :precision binary64 (* (fmod 1.0 1.0) 1.0))
                  double code(double x) {
                  	return fmod(1.0, 1.0) * 1.0;
                  }
                  
                  real(8) function code(x)
                      real(8), intent (in) :: x
                      code = mod(1.0d0, 1.0d0) * 1.0d0
                  end function
                  
                  def code(x):
                  	return math.fmod(1.0, 1.0) * 1.0
                  
                  function code(x)
                  	return Float64(rem(1.0, 1.0) * 1.0)
                  end
                  
                  code[x_] := N[(N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * 1.0), $MachinePrecision]
                  
                  \begin{array}{l}
                  
                  \\
                  \left(1 \bmod 1\right) \cdot 1
                  \end{array}
                  
                  Derivation
                  1. Initial program 6.8%

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

                    \[\leadsto \left(\color{blue}{1} \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x} \]
                  4. Step-by-step derivation
                    1. Applied rewrites25.4%

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

                      \[\leadsto \left(1 \bmod \color{blue}{1}\right) \cdot e^{-x} \]
                    3. Step-by-step derivation
                      1. Applied rewrites25.3%

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

                        \[\leadsto \left(1 \bmod 1\right) \cdot \color{blue}{1} \]
                      3. Step-by-step derivation
                        1. Applied rewrites25.3%

                          \[\leadsto \left(1 \bmod 1\right) \cdot \color{blue}{1} \]
                        2. Add Preprocessing

                        Reproduce

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