
(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 (cbrt (exp (cos x)))) (t_1 (exp (- x))))
(if (<= (* (fmod (exp x) (sqrt (cos x))) t_1) 2.0)
(/ (fmod (exp x) (sqrt (+ (log (pow t_0 2.0)) (log t_0)))) (exp x))
t_1)))
double code(double x) {
double t_0 = cbrt(exp(cos(x)));
double t_1 = exp(-x);
double tmp;
if ((fmod(exp(x), sqrt(cos(x))) * t_1) <= 2.0) {
tmp = fmod(exp(x), sqrt((log(pow(t_0, 2.0)) + log(t_0)))) / exp(x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x) t_0 = cbrt(exp(cos(x))) t_1 = exp(Float64(-x)) tmp = 0.0 if (Float64(rem(exp(x), sqrt(cos(x))) * t_1) <= 2.0) tmp = Float64(rem(exp(x), sqrt(Float64(log((t_0 ^ 2.0)) + log(t_0)))) / exp(x)); else tmp = t_1; end return tmp end
code[x_] := Block[{t$95$0 = N[Power[N[Exp[N[Cos[x], $MachinePrecision]], $MachinePrecision], 1/3], $MachinePrecision]}, Block[{t$95$1 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision], 2.0], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[(N[Log[N[Power[t$95$0, 2.0], $MachinePrecision]], $MachinePrecision] + N[Log[t$95$0], $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[3]{e^{\cos x}}\\
t_1 := e^{-x}\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot t\_1 \leq 2:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\log \left({t\_0}^{2}\right) + \log t\_0}\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.5%
/-rgt-identity10.5%
associate-/r/10.5%
exp-neg10.5%
remove-double-neg10.5%
Simplified10.5%
add-log-exp10.5%
add-cube-cbrt56.5%
log-prod56.5%
pow256.5%
Applied egg-rr56.5%
if 2 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 0.0%
exp-neg0.0%
div-inv0.0%
add-cbrt-cube0.0%
pow1/30.0%
pow-to-exp0.0%
Applied egg-rr0.0%
Taylor expanded in x around inf 1.6%
*-commutative1.6%
Simplified1.6%
associate-*l*1.6%
metadata-eval1.6%
pow-exp1.6%
pow11.6%
add-sqr-sqrt1.6%
sqrt-unprod1.6%
sqr-neg1.6%
sqrt-unprod0.0%
add-sqr-sqrt100.0%
exp-neg100.0%
Applied egg-rr100.0%
rec-exp100.0%
Simplified100.0%
(FPCore (x) :precision binary64 (if (<= x -4e-310) 1.0 (/ (fmod (+ x 1.0) (sqrt (cos x))) (exp x))))
double code(double x) {
double tmp;
if (x <= -4e-310) {
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 <= (-4d-310)) 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 <= -4e-310: 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 <= -4e-310) 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, -4e-310], 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 -4 \cdot 10^{-310}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(\left(x + 1\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\
\end{array}
\end{array}
if x < -3.999999999999988e-310Initial program 9.5%
exp-neg9.5%
div-inv9.5%
add-cbrt-cube9.5%
pow1/39.5%
pow-to-exp9.5%
Applied egg-rr6.9%
Taylor expanded in x around inf 97.5%
*-commutative97.5%
Simplified97.5%
Taylor expanded in x around 0 100.0%
if -3.999999999999988e-310 < x Initial program 7.9%
/-rgt-identity7.9%
associate-/r/7.9%
exp-neg7.9%
remove-double-neg7.9%
Simplified7.9%
Taylor expanded in x around 0 38.4%
+-commutative38.4%
Simplified38.4%
(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%
exp-neg8.6%
div-inv8.6%
add-cbrt-cube8.6%
pow1/38.6%
pow-to-exp8.6%
Applied egg-rr6.4%
Taylor expanded in x around inf 43.3%
*-commutative43.3%
Simplified43.3%
associate-*l*43.3%
metadata-eval43.3%
pow-exp43.3%
pow143.3%
add-sqr-sqrt2.5%
sqrt-unprod43.3%
sqr-neg43.3%
sqrt-unprod40.8%
add-sqr-sqrt61.8%
exp-neg61.8%
Applied egg-rr61.8%
rec-exp61.8%
Simplified61.8%
(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}
Initial program 8.6%
exp-neg8.6%
div-inv8.6%
add-cbrt-cube8.6%
pow1/38.6%
pow-to-exp8.6%
Applied egg-rr6.4%
Taylor expanded in x around inf 43.3%
*-commutative43.3%
Simplified43.3%
Taylor expanded in x around 0 44.6%
herbie shell --seed 2024132
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))