% !TeX TS-program = pdflatex


\documentclass[french,a4paper,11pt]{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{babel}
\usepackage{siunitx}
\sisetup{locale=FR,output-decimal-marker={,},group-minimum-digits=4}
\usepackage{xintexpr}
\usepackage{xparse}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}

\NewDocumentCommand{\calcbinomp}{ s O{3} m m m }{%*=ingénieur,1=prec,2=n,3=p,4=k
	\IfBooleanTF{#1}%
		{%
			\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(binomial(#3,#5)*#4^#5*(1-#4)^(#3-#5),#2)}}}
		}%
		{%
			\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(binomial(#3,#5)*#4^#5*(1-#4)^(#3-#5),#2)}}}
		}
}

\NewDocumentCommand{\calcbinomc}{ s O{3} m m m m }{%*=ingénieur,1=prec,2=n,3=p,4=a,5=b
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(add(binomial(#3,i)*#4^i*(1-#4)^(#3-i), i=#5..#6),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(add(binomial(#3,i)*#4^i*(1-#4)^(#3-i), i=#5..#6),#2)}}}
	}
}

\NewDocumentCommand{\calcpoissonp}{ s O{3} m m }{%*=ing,1=prec,2=lbda,3=k
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(exp(-#3)*#3^#4/factorial(#4),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(exp(-#3)*#3^#4/factorial(#4),#2)}}}
	}
}

\NewDocumentCommand{\calcpoissonc}{ s O{3} m m m }{%*=ing,1=prec,2=lbda,3=a,4=b
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(add(exp(-#3)*#3^i/factorial(i), i=#4..#5),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(add(exp(-#3)*#3^i/factorial(i), i=#4..#5),#2)}}}
	}
}

\begin{document}

\part{Test calcul en binomiale}

\medskip

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$X \hookrightarrow \mathcal{B}(5\,;\,0,4)$ :\\ \\
$P(X=3) \approx \calcbinomp{5}{0.4}{3}$.\\
$P(X\leqslant1) \approx \calcbinomc{5}{0.4}{0}{1}$.
\end{tcblisting}

\medskip

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$X \hookrightarrow \mathcal{B}(100\,;\,0,02)$ :\\ \\
$P(X=10) \approx \calcbinomp[6]{100}{0.02}{10} \approx \calcbinomp*[6]{100}{0.02}{10}$.\\
$P(15\leqslant X\leqslant25) \approx \calcbinomc[10]{100}{0.02}{15}{25} \approx \calcbinomc*[10]{100}{0.02}{15}{25}$.
\end{tcblisting}

\part{Test calcul en Poisson}

\medskip

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$Y \hookrightarrow \mathcal{P}_5$ :\\ \\
$P(Y=3) \approx \calcpoissonp[5]{5}{3}$.\\
$P(Y\leqslant2) \approx \calcpoissonc[5]{5}{0}{2}$.
\end{tcblisting}

\end{document}