
(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))))
(if (<= x 0.2)
(/
(fmod (exp x) (+ (* t_0 0.6666666666666666) (log (cbrt (exp t_0)))))
(exp x))
(exp (- x)))))
double code(double x) {
double t_0 = sqrt(cos(x));
double tmp;
if (x <= 0.2) {
tmp = fmod(exp(x), ((t_0 * 0.6666666666666666) + log(cbrt(exp(t_0))))) / exp(x);
} else {
tmp = exp(-x);
}
return tmp;
}
function code(x) t_0 = sqrt(cos(x)) tmp = 0.0 if (x <= 0.2) tmp = Float64(rem(exp(x), Float64(Float64(t_0 * 0.6666666666666666) + log(cbrt(exp(t_0))))) / exp(x)); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := Block[{t$95$0 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[x, 0.2], 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], N[Exp[(-x)], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
\mathbf{if}\;x \leq 0.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}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < 0.20000000000000001Initial program 9.7%
/-rgt-identity9.7%
associate-/r/9.7%
exp-neg9.7%
remove-double-neg9.7%
Simplified9.7%
add-log-exp9.7%
add-cube-cbrt53.7%
log-prod53.7%
pow253.7%
Applied egg-rr53.7%
unpow253.7%
log-prod53.7%
pow1/353.7%
log-pow53.8%
add-log-exp53.8%
pow1/353.8%
log-pow53.8%
add-log-exp53.8%
Applied egg-rr53.8%
distribute-rgt-out53.8%
metadata-eval53.8%
Simplified53.8%
if 0.20000000000000001 < x Initial program 0.0%
/-rgt-identity0.0%
associate-/r/0.0%
exp-neg0.0%
remove-double-neg0.0%
Simplified0.0%
add-cbrt-cube0.0%
pow1/30.0%
pow-to-exp0.0%
pow30.0%
log-pow0.0%
log-div0.0%
add-log-exp0.0%
Applied egg-rr0.0%
Taylor expanded in x around inf 100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
neg-mul-1100.0%
Simplified100.0%
(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 99.7%
/-rgt-identity99.7%
associate-/r/99.5%
exp-neg100.0%
remove-double-neg100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
if -2e-16 < x Initial program 4.5%
/-rgt-identity4.5%
associate-/r/4.5%
exp-neg4.5%
remove-double-neg4.5%
Simplified4.5%
add-cbrt-cube4.5%
pow1/34.5%
pow-to-exp4.5%
pow34.5%
log-pow4.5%
log-div4.5%
add-log-exp4.5%
Applied egg-rr4.5%
Taylor expanded in x around inf 60.5%
*-commutative60.5%
Simplified60.5%
Taylor expanded in x around 0 60.5%
neg-mul-160.5%
Simplified60.5%
(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.8%
/-rgt-identity7.8%
associate-/r/7.8%
exp-neg7.8%
remove-double-neg7.8%
Simplified7.8%
add-cbrt-cube7.8%
pow1/37.8%
pow-to-exp7.8%
pow37.8%
log-pow7.8%
log-div7.8%
add-log-exp7.8%
Applied egg-rr7.8%
Taylor expanded in x around inf 60.3%
*-commutative60.3%
Simplified60.3%
Taylor expanded in x around 0 60.3%
neg-mul-160.3%
Simplified60.3%
(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(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.8%
/-rgt-identity7.8%
associate-/r/7.8%
exp-neg7.8%
remove-double-neg7.8%
Simplified7.8%
add-cbrt-cube7.8%
pow1/37.8%
pow-to-exp7.8%
pow37.8%
log-pow7.8%
log-div7.8%
add-log-exp7.8%
Applied egg-rr7.8%
Taylor expanded in x around inf 60.3%
*-commutative60.3%
Simplified60.3%
add-sqr-sqrt38.6%
sqrt-unprod41.1%
associate-*l*41.1%
metadata-eval41.1%
associate-*l*41.1%
metadata-eval41.1%
swap-sqr41.1%
unpow241.1%
metadata-eval41.1%
*-commutative41.1%
*-un-lft-identity41.1%
sqrt-pow141.1%
metadata-eval41.1%
pow141.1%
add-log-exp41.1%
*-un-lft-identity41.1%
log-prod41.1%
metadata-eval41.1%
add-log-exp41.1%
Applied egg-rr41.1%
+-lft-identity41.1%
Simplified41.1%
herbie shell --seed 2024102
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))