
(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 4 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 (exp (- x))))
(if (<= (* (fmod (exp x) t_0) t_1) 2.0)
(/
(fmod (exp x) (+ (* t_0 0.6666666666666666) (log (cbrt (exp t_0)))))
(exp x))
t_1)))
double code(double x) {
double t_0 = sqrt(cos(x));
double t_1 = exp(-x);
double tmp;
if ((fmod(exp(x), t_0) * t_1) <= 2.0) {
tmp = fmod(exp(x), ((t_0 * 0.6666666666666666) + log(cbrt(exp(t_0))))) / exp(x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x) t_0 = sqrt(cos(x)) t_1 = exp(Float64(-x)) tmp = 0.0 if (Float64(rem(exp(x), t_0) * t_1) <= 2.0) tmp = Float64(rem(exp(x), Float64(Float64(t_0 * 0.6666666666666666) + log(cbrt(exp(t_0))))) / exp(x)); else tmp = t_1; end return tmp end
code[x_] := Block[{t$95$0 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = 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$1), $MachinePrecision], 2.0], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(t$95$0 * 0.6666666666666666), $MachinePrecision] + N[Log[N[Power[N[Exp[t$95$0], $MachinePrecision], 1/3], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
t_1 := e^{-x}\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod t\_0\right) \cdot t\_1 \leq 2:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(t\_0 \cdot 0.6666666666666666 + \log \left(\sqrt[3]{e^{t\_0}}\right)\right)\right)}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 2Initial program 10.8%
/-rgt-identity10.8%
associate-/r/10.8%
exp-neg10.8%
remove-double-neg10.8%
Simplified10.8%
add-log-exp10.8%
add-cube-cbrt58.5%
log-prod58.5%
pow258.5%
Applied egg-rr58.5%
unpow258.5%
log-prod58.5%
pow1/358.5%
log-pow58.5%
add-log-exp58.5%
pow1/358.6%
log-pow58.6%
add-log-exp58.6%
Applied egg-rr58.6%
distribute-rgt-out58.6%
metadata-eval58.6%
Simplified58.6%
if 2 < (*.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 0 0.1%
Taylor expanded in x around inf 96.3%
neg-mul-196.3%
Simplified96.3%
Final simplification66.2%
(FPCore (x) :precision binary64 (if (<= x -2e-16) (exp (- (log (fmod (exp x) 1.0)) x)) (exp (- x))))
double code(double x) {
double tmp;
if (x <= -2e-16) {
tmp = exp((log(fmod(exp(x), 1.0)) - x));
} else {
tmp = exp(-x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-2d-16)) then
tmp = exp((log(mod(exp(x), 1.0d0)) - x))
else
tmp = exp(-x)
end if
code = tmp
end function
def code(x): tmp = 0 if x <= -2e-16: tmp = math.exp((math.log(math.fmod(math.exp(x), 1.0)) - x)) else: tmp = math.exp(-x) return tmp
function code(x) tmp = 0.0 if (x <= -2e-16) tmp = exp(Float64(log(rem(exp(x), 1.0)) - x)); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := If[LessEqual[x, -2e-16], N[Exp[N[(N[Log[N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]], $MachinePrecision] - x), $MachinePrecision]], $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-16}:\\
\;\;\;\;e^{\log \left(\left(e^{x}\right) \bmod 1\right) - x}\\
\mathbf{else}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < -2e-16Initial program 85.4%
/-rgt-identity85.4%
associate-/r/85.4%
exp-neg85.7%
remove-double-neg85.7%
Simplified85.7%
add-exp-log85.7%
div-exp86.2%
Applied egg-rr86.2%
Taylor expanded in x around 0 86.2%
if -2e-16 < x Initial program 4.2%
/-rgt-identity4.2%
associate-/r/4.2%
exp-neg4.2%
remove-double-neg4.2%
Simplified4.2%
add-exp-log4.2%
div-exp4.2%
Applied egg-rr4.2%
Taylor expanded in x around 0 4.0%
Taylor expanded in x around inf 64.4%
neg-mul-164.4%
Simplified64.4%
Final simplification65.6%
(FPCore (x) :precision binary64 (if (<= x -2e-16) (/ (fmod (exp x) 1.0) (exp x)) (exp (- x))))
double code(double x) {
double tmp;
if (x <= -2e-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 <= (-2d-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 <= -2e-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 <= -2e-16) tmp = Float64(rem(exp(x), 1.0) / exp(x)); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := If[LessEqual[x, -2e-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 -2 \cdot 10^{-16}:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod 1\right)}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < -2e-16Initial program 85.4%
/-rgt-identity85.4%
associate-/r/85.4%
exp-neg85.7%
remove-double-neg85.7%
Simplified85.7%
Taylor expanded in x around 0 85.7%
if -2e-16 < x Initial program 4.2%
/-rgt-identity4.2%
associate-/r/4.2%
exp-neg4.2%
remove-double-neg4.2%
Simplified4.2%
add-exp-log4.2%
div-exp4.2%
Applied egg-rr4.2%
Taylor expanded in x around 0 4.0%
Taylor expanded in x around inf 64.4%
neg-mul-164.4%
Simplified64.4%
Final simplification65.6%
(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 8.6%
/-rgt-identity8.6%
associate-/r/8.6%
exp-neg8.6%
remove-double-neg8.6%
Simplified8.6%
add-exp-log8.6%
div-exp8.7%
Applied egg-rr8.7%
Taylor expanded in x around 0 8.5%
Taylor expanded in x around inf 63.3%
neg-mul-163.3%
Simplified63.3%
Final simplification63.3%
herbie shell --seed 2024060
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))