
(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:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x)
:precision binary64
(let* ((t_0 (sqrt (cos x))) (t_1 (cbrt (exp t_0))) (t_2 (exp (- x))))
(if (<= (* (fmod (exp x) t_0) t_2) INFINITY)
(/ (fmod (exp x) (+ (log (pow t_1 2.0)) (log t_1))) (exp x))
t_2)))
double code(double x) {
double t_0 = sqrt(cos(x));
double t_1 = cbrt(exp(t_0));
double t_2 = exp(-x);
double tmp;
if ((fmod(exp(x), t_0) * t_2) <= ((double) INFINITY)) {
tmp = fmod(exp(x), (log(pow(t_1, 2.0)) + log(t_1))) / exp(x);
} else {
tmp = t_2;
}
return tmp;
}
function code(x) t_0 = sqrt(cos(x)) t_1 = cbrt(exp(t_0)) t_2 = exp(Float64(-x)) tmp = 0.0 if (Float64(rem(exp(x), t_0) * t_2) <= Inf) tmp = Float64(rem(exp(x), Float64(log((t_1 ^ 2.0)) + log(t_1))) / exp(x)); else tmp = t_2; end return tmp end
code[x_] := Block[{t$95$0 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Power[N[Exp[t$95$0], $MachinePrecision], 1/3], $MachinePrecision]}, Block[{t$95$2 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$2), $MachinePrecision], Infinity], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[Log[N[Power[t$95$1, 2.0], $MachinePrecision]], $MachinePrecision] + N[Log[t$95$1], $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], t$95$2]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
t_1 := \sqrt[3]{e^{t\_0}}\\
t_2 := e^{-x}\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod t\_0\right) \cdot t\_2 \leq \infty:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\log \left({t\_1}^{2}\right) + \log t\_1\right)\right)}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;t\_2\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < +inf.0Initial program 9.6%
/-rgt-identity9.6%
associate-/r/9.6%
exp-neg10.1%
remove-double-neg10.1%
Simplified10.1%
add-log-exp10.1%
add-cube-cbrt56.7%
log-prod56.7%
pow256.7%
Applied egg-rr56.7%
if +inf.0 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 0.0%
/-rgt-identity0.0%
associate-/r/0.0%
exp-neg0.0%
remove-double-neg0.0%
Simplified0.0%
add-exp-log0.0%
div-exp0.1%
Applied egg-rr0.1%
Taylor expanded in x around inf 98.1%
neg-mul-198.1%
Simplified98.1%
(FPCore (x) :precision binary64 (if (<= x -1e-16) (/ (fmod (exp x) 1.0) (exp x)) (exp (- x))))
double code(double x) {
double tmp;
if (x <= -1e-16) {
tmp = fmod(exp(x), 1.0) / exp(x);
} else {
tmp = exp(-x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1d-16)) then
tmp = mod(exp(x), 1.0d0) / exp(x)
else
tmp = exp(-x)
end if
code = tmp
end function
def code(x): tmp = 0 if x <= -1e-16: tmp = math.fmod(math.exp(x), 1.0) / math.exp(x) else: tmp = math.exp(-x) return tmp
function code(x) tmp = 0.0 if (x <= -1e-16) tmp = Float64(rem(exp(x), 1.0) / exp(x)); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := If[LessEqual[x, -1e-16], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-16}:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod 1\right)}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < -9.9999999999999998e-17Initial program 79.8%
/-rgt-identity79.8%
associate-/r/79.7%
exp-neg90.0%
remove-double-neg90.0%
Simplified90.0%
Taylor expanded in x around 0 90.0%
if -9.9999999999999998e-17 < x Initial program 4.8%
/-rgt-identity4.8%
associate-/r/4.8%
exp-neg4.8%
remove-double-neg4.8%
Simplified4.8%
add-exp-log4.8%
div-exp4.8%
Applied egg-rr4.8%
Taylor expanded in x around inf 62.7%
neg-mul-162.7%
Simplified62.7%
(FPCore (x) :precision binary64 (exp (- x)))
double code(double x) {
return exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = exp(-x)
end function
public static double code(double x) {
return Math.exp(-x);
}
def code(x): return math.exp(-x)
function code(x) return exp(Float64(-x)) end
function tmp = code(x) tmp = exp(-x); end
code[x_] := N[Exp[(-x)], $MachinePrecision]
\begin{array}{l}
\\
e^{-x}
\end{array}
Initial program 7.7%
/-rgt-identity7.7%
associate-/r/7.7%
exp-neg8.1%
remove-double-neg8.1%
Simplified8.1%
add-exp-log8.1%
div-exp7.9%
Applied egg-rr7.9%
Taylor expanded in x around inf 62.1%
neg-mul-162.1%
Simplified62.1%
herbie shell --seed 2024085
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))