expfmod (used to be hard to sample)

Percentage Accurate: 6.7% → 62.7%
Time: 22.8s
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.7% 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.7% accurate, 1.2× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;\frac{\left(\left(x + 1\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\


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

    1. Initial program 7.3%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg7.3%

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

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log7.3%

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified96.3%

      \[\leadsto e^{\color{blue}{-x}} \]
    10. Taylor expanded in x around 0 100.0%

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

    if -1.9999999999999e-311 < x

    1. Initial program 5.6%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg5.6%

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

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

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

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

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

Alternative 2: 62.7% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(\left(x + 1\right) \bmod \left(1 + {x}^{2} \cdot -0.25\right)\right)}{e^{x}}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -2e-311)
   1.0
   (/ (fmod (+ x 1.0) (+ 1.0 (* (pow x 2.0) -0.25))) (exp x))))
double code(double x) {
	double tmp;
	if (x <= -2e-311) {
		tmp = 1.0;
	} else {
		tmp = fmod((x + 1.0), (1.0 + (pow(x, 2.0) * -0.25))) / exp(x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-2d-311)) then
        tmp = 1.0d0
    else
        tmp = mod((x + 1.0d0), (1.0d0 + ((x ** 2.0d0) * (-0.25d0)))) / exp(x)
    end if
    code = tmp
end function
def code(x):
	tmp = 0
	if x <= -2e-311:
		tmp = 1.0
	else:
		tmp = math.fmod((x + 1.0), (1.0 + (math.pow(x, 2.0) * -0.25))) / math.exp(x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -2e-311)
		tmp = 1.0;
	else
		tmp = Float64(rem(Float64(x + 1.0), Float64(1.0 + Float64((x ^ 2.0) * -0.25))) / exp(x));
	end
	return tmp
end
code[x_] := If[LessEqual[x, -2e-311], 1.0, N[(N[With[{TMP1 = N[(x + 1.0), $MachinePrecision], TMP2 = N[(1.0 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;\frac{\left(\left(x + 1\right) \bmod \left(1 + {x}^{2} \cdot -0.25\right)\right)}{e^{x}}\\


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

    1. Initial program 7.3%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg7.3%

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

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log7.3%

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified96.3%

      \[\leadsto e^{\color{blue}{-x}} \]
    10. Taylor expanded in x around 0 100.0%

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

    if -1.9999999999999e-311 < x

    1. Initial program 5.6%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg5.6%

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

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

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \color{blue}{\left(1 + -0.25 \cdot {x}^{2}\right)}\right)}{e^{x}} \]
    6. Step-by-step derivation
      1. *-commutative5.6%

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

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

      \[\leadsto \frac{\left(\color{blue}{\left(1 + x\right)} \bmod \left(1 + {x}^{2} \cdot -0.25\right)\right)}{e^{x}} \]
    9. Step-by-step derivation
      1. +-commutative37.0%

        \[\leadsto \frac{\left(\color{blue}{\left(x + 1\right)} \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}} \]
    10. Simplified37.0%

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

Alternative 3: 62.1% accurate, 4.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-311}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\left(\left(x + 1\right) \bmod 1\right)\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= x -2e-311) 1.0 (fmod (+ x 1.0) 1.0)))
double code(double x) {
	double tmp;
	if (x <= -2e-311) {
		tmp = 1.0;
	} else {
		tmp = fmod((x + 1.0), 1.0);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-2d-311)) then
        tmp = 1.0d0
    else
        tmp = mod((x + 1.0d0), 1.0d0)
    end if
    code = tmp
end function
def code(x):
	tmp = 0
	if x <= -2e-311:
		tmp = 1.0
	else:
		tmp = math.fmod((x + 1.0), 1.0)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -2e-311)
		tmp = 1.0;
	else
		tmp = rem(Float64(x + 1.0), 1.0);
	end
	return tmp
end
code[x_] := If[LessEqual[x, -2e-311], 1.0, N[With[{TMP1 = N[(x + 1.0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 7.3%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg7.3%

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

      \[\leadsto \color{blue}{\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log7.3%

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified96.3%

      \[\leadsto e^{\color{blue}{-x}} \]
    10. Taylor expanded in x around 0 100.0%

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

    if -1.9999999999999e-311 < x

    1. Initial program 5.6%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg5.6%

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

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

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

      \[\leadsto \left(\left(e^{x}\right) \bmod \left(\sqrt{\color{blue}{1}}\right)\right) \]
    7. Taylor expanded in x around 0 5.0%

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

      \[\leadsto \left(\color{blue}{\left(1 + x\right)} \bmod 1\right) \]
    9. Step-by-step derivation
      1. +-commutative37.0%

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

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

Alternative 4: 62.0% accurate, 4.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4 \cdot 10^{-14}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;e^{-x}\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= x -4e-14) 1.0 (exp (- x))))
double code(double x) {
	double tmp;
	if (x <= -4e-14) {
		tmp = 1.0;
	} else {
		tmp = exp(-x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-4d-14)) then
        tmp = 1.0d0
    else
        tmp = exp(-x)
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= -4e-14) {
		tmp = 1.0;
	} else {
		tmp = Math.exp(-x);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -4e-14:
		tmp = 1.0
	else:
		tmp = math.exp(-x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -4e-14)
		tmp = 1.0;
	else
		tmp = exp(Float64(-x));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -4e-14)
		tmp = 1.0;
	else
		tmp = exp(-x);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, -4e-14], 1.0, N[Exp[(-x)], $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 71.2%

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

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

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

        \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
      4. remove-double-neg71.4%

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

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

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

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

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

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

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

      \[\leadsto e^{\color{blue}{-x}} \]
    10. Taylor expanded in x around 0 100.0%

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

    if -4e-14 < x

    1. Initial program 4.5%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{-x}} \]
    9. Simplified63.5%

      \[\leadsto e^{\color{blue}{-x}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 43.0% 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 6.4%

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

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

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

      \[\leadsto \frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{\color{blue}{e^{-\left(-x\right)}}} \]
    4. remove-double-neg6.4%

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

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

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

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

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

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

      \[\leadsto e^{\color{blue}{-x}} \]
  9. Simplified62.8%

    \[\leadsto e^{\color{blue}{-x}} \]
  10. Taylor expanded in x around 0 47.5%

    \[\leadsto \color{blue}{1} \]
  11. Add Preprocessing

Reproduce

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