
(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 5 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 (expm1 (- (log (fmod (exp x) (sqrt (cos x)))) x)))
(t_1 (- 1.0 t_0)))
(if (<= x -5e-311)
1.0
(if (<= x 40.0)
(- (/ 1.0 t_1) (/ (pow t_0 2.0) (cbrt (pow t_1 3.0))))
(exp (- x))))))
double code(double x) {
double t_0 = expm1((log(fmod(exp(x), sqrt(cos(x)))) - x));
double t_1 = 1.0 - t_0;
double tmp;
if (x <= -5e-311) {
tmp = 1.0;
} else if (x <= 40.0) {
tmp = (1.0 / t_1) - (pow(t_0, 2.0) / cbrt(pow(t_1, 3.0)));
} else {
tmp = exp(-x);
}
return tmp;
}
function code(x) t_0 = expm1(Float64(log(rem(exp(x), sqrt(cos(x)))) - x)) t_1 = Float64(1.0 - t_0) tmp = 0.0 if (x <= -5e-311) tmp = 1.0; elseif (x <= 40.0) tmp = Float64(Float64(1.0 / t_1) - Float64((t_0 ^ 2.0) / cbrt((t_1 ^ 3.0)))); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := Block[{t$95$0 = N[(Exp[N[(N[Log[N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]], $MachinePrecision] - x), $MachinePrecision]] - 1), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 - t$95$0), $MachinePrecision]}, If[LessEqual[x, -5e-311], 1.0, If[LessEqual[x, 40.0], N[(N[(1.0 / t$95$1), $MachinePrecision] - N[(N[Power[t$95$0, 2.0], $MachinePrecision] / N[Power[N[Power[t$95$1, 3.0], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{expm1}\left(\log \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) - x\right)\\
t_1 := 1 - t_0\\
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 40:\\
\;\;\;\;\frac{1}{t_1} - \frac{{t_0}^{2}}{\sqrt[3]{{t_1}^{3}}}\\
\mathbf{else}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < -5.00000000000023e-311Initial program 9.8%
exp-neg9.9%
associate-*r/9.9%
*-rgt-identity9.9%
Simplified9.9%
add-exp-log9.9%
div-exp9.9%
Applied egg-rr9.9%
add-cube-cbrt9.9%
pow39.9%
Applied egg-rr9.9%
Taylor expanded in x around inf 100.0%
if -5.00000000000023e-311 < x < 40Initial program 10.2%
exp-neg10.2%
associate-*r/10.2%
*-rgt-identity10.2%
Simplified10.2%
add-exp-log10.2%
div-exp10.2%
Applied egg-rr10.2%
exp-diff10.2%
add-exp-log10.2%
expm1-log1p-u10.2%
expm1-udef10.2%
log1p-udef10.2%
add-exp-log10.2%
associate-+r-10.2%
flip-+10.2%
metadata-eval10.2%
Applied egg-rr10.3%
add-cbrt-cube10.3%
pow310.3%
Applied egg-rr10.3%
if 40 < x Initial program 0.0%
exp-neg0.0%
associate-*r/0.0%
*-rgt-identity0.0%
Simplified0.0%
add-exp-log0.0%
div-exp0.0%
Applied egg-rr0.0%
Taylor expanded in x around inf 100.0%
neg-mul-1100.0%
Simplified100.0%
Final simplification64.6%
(FPCore (x)
:precision binary64
(if (<= x -5e-311)
1.0
(if (<= x 400.0)
(/ (+ (+ 1.0 (fmod (exp x) (sqrt (cos x)))) -1.0) (exp x))
(exp (- x)))))
double code(double x) {
double tmp;
if (x <= -5e-311) {
tmp = 1.0;
} else if (x <= 400.0) {
tmp = ((1.0 + fmod(exp(x), sqrt(cos(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 <= (-5d-311)) then
tmp = 1.0d0
else if (x <= 400.0d0) then
tmp = ((1.0d0 + mod(exp(x), sqrt(cos(x)))) + (-1.0d0)) / exp(x)
else
tmp = exp(-x)
end if
code = tmp
end function
def code(x): tmp = 0 if x <= -5e-311: tmp = 1.0 elif x <= 400.0: tmp = ((1.0 + math.fmod(math.exp(x), math.sqrt(math.cos(x)))) + -1.0) / math.exp(x) else: tmp = math.exp(-x) return tmp
function code(x) tmp = 0.0 if (x <= -5e-311) tmp = 1.0; elseif (x <= 400.0) tmp = Float64(Float64(Float64(1.0 + rem(exp(x), sqrt(cos(x)))) + -1.0) / exp(x)); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := If[LessEqual[x, -5e-311], 1.0, If[LessEqual[x, 400.0], N[(N[(N[(1.0 + N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision] / N[Exp[x], $MachinePrecision]), $MachinePrecision], N[Exp[(-x)], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 400:\\
\;\;\;\;\frac{\left(1 + \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\right) + -1}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < -5.00000000000023e-311Initial program 9.8%
exp-neg9.9%
associate-*r/9.9%
*-rgt-identity9.9%
Simplified9.9%
add-exp-log9.9%
div-exp9.9%
Applied egg-rr9.9%
add-cube-cbrt9.9%
pow39.9%
Applied egg-rr9.9%
Taylor expanded in x around inf 100.0%
if -5.00000000000023e-311 < x < 400Initial program 10.2%
exp-neg10.2%
associate-*r/10.2%
*-rgt-identity10.2%
Simplified10.2%
expm1-log1p-u10.2%
expm1-udef10.3%
log1p-udef10.3%
add-exp-log10.3%
Applied egg-rr10.3%
if 400 < x Initial program 0.0%
exp-neg0.0%
associate-*r/0.0%
*-rgt-identity0.0%
Simplified0.0%
add-exp-log0.0%
div-exp0.0%
Applied egg-rr0.0%
Taylor expanded in x around inf 100.0%
neg-mul-1100.0%
Simplified100.0%
Final simplification64.6%
(FPCore (x) :precision binary64 (if (<= x -5e-311) 1.0 (if (<= x 400.0) (/ (fmod (exp x) (sqrt (cos x))) (exp x)) (exp (- x)))))
double code(double x) {
double tmp;
if (x <= -5e-311) {
tmp = 1.0;
} else if (x <= 400.0) {
tmp = fmod(exp(x), sqrt(cos(x))) / exp(x);
} else {
tmp = exp(-x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-5d-311)) then
tmp = 1.0d0
else if (x <= 400.0d0) then
tmp = mod(exp(x), sqrt(cos(x))) / exp(x)
else
tmp = exp(-x)
end if
code = tmp
end function
def code(x): tmp = 0 if x <= -5e-311: tmp = 1.0 elif x <= 400.0: tmp = math.fmod(math.exp(x), math.sqrt(math.cos(x))) / math.exp(x) else: tmp = math.exp(-x) return tmp
function code(x) tmp = 0.0 if (x <= -5e-311) tmp = 1.0; elseif (x <= 400.0) tmp = Float64(rem(exp(x), sqrt(cos(x))) / exp(x)); else tmp = exp(Float64(-x)); end return tmp end
code[x_] := If[LessEqual[x, -5e-311], 1.0, If[LessEqual[x, 400.0], 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], N[Exp[(-x)], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-311}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 400:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;e^{-x}\\
\end{array}
\end{array}
if x < -5.00000000000023e-311Initial program 9.8%
exp-neg9.9%
associate-*r/9.9%
*-rgt-identity9.9%
Simplified9.9%
add-exp-log9.9%
div-exp9.9%
Applied egg-rr9.9%
add-cube-cbrt9.9%
pow39.9%
Applied egg-rr9.9%
Taylor expanded in x around inf 100.0%
if -5.00000000000023e-311 < x < 400Initial program 10.2%
exp-neg10.2%
associate-*r/10.2%
*-rgt-identity10.2%
Simplified10.2%
if 400 < x Initial program 0.0%
exp-neg0.0%
associate-*r/0.0%
*-rgt-identity0.0%
Simplified0.0%
add-exp-log0.0%
div-exp0.0%
Applied egg-rr0.0%
Taylor expanded in x around inf 100.0%
neg-mul-1100.0%
Simplified100.0%
Final simplification64.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 7.9%
exp-neg7.9%
associate-*r/7.9%
*-rgt-identity7.9%
Simplified7.9%
add-exp-log7.9%
div-exp7.9%
Applied egg-rr7.9%
Taylor expanded in x around inf 61.4%
neg-mul-161.4%
Simplified61.4%
Final simplification61.4%
(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 7.9%
exp-neg7.9%
associate-*r/7.9%
*-rgt-identity7.9%
Simplified7.9%
add-exp-log7.9%
div-exp7.9%
Applied egg-rr7.9%
add-cube-cbrt7.9%
pow37.9%
Applied egg-rr7.9%
Taylor expanded in x around inf 42.0%
Final simplification42.0%
herbie shell --seed 2023192
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))