Skip to main content
added 105 characters in body
Source Link
jps
  • 80.4k
  • 2
  • 89
  • 158
% Source - https://tex.stackexchange.com/q/758979
% Posted by cis, modified by community. See post 'Timeline' for change history
% Retrieved 2026-01-30, License - CC BY-SA 4.0

\documentclass[margin=10mm, varwidth]{standalone}

\ExplSyntaxOn
\fp_new_function:n { Phi }
\fp_new_function:n { PhiAux }
\fp_set_function:nnn { Phi } { x } { PhiAux(x, 1/(1 + 0.25 * sqrt(2) * x)) }
\fp_set_function:nnn { PhiAux } { x, h }
  {
    0.5 (2-h * exp(
    -0.5 x^2 -1.26551223
    +1% Horner's method for the polynomial is faster
    + h *( 1.00002368
    + h +0*( 0.37409196 h^2
    +0+ h *( 0.09678418 
 h^3   + h *(-0.18628806 h^4
    +0+ h *( 0.27886807 
 h^5   + h *(-1.13520398 h^6
    +1+ h *( 1.48851587*48851587
 h^7   + h *(-0.82215223 h^8
    +0+ h *  0.17087277 
 h^9   ))))))))))
  }
\ExplSyntaxOff

\begin{document}
\section{Test with \texttt{l3fp}}
\xdef\x{0.61}
sn(\x) = \fpeval{round(Phi(\x), 5)}
\end{document}
% Source - https://tex.stackexchange.com/q/758979
% Posted by cis, modified by community. See post 'Timeline' for change history
% Retrieved 2026-01-30, License - CC BY-SA 4.0

\documentclass[margin=10mm, varwidth]{standalone}

\ExplSyntaxOn
\fp_new_function:n { Phi }
\fp_new_function:n { PhiAux }
\fp_set_function:nnn { Phi } { x } { PhiAux(x, 1/(1 + 0.25 * sqrt(2) * x)) }
\fp_set_function:nnn { PhiAux } { x, h }
  {
    0.5 (2-h * exp(
    -0.5 x^2 -1.26551223
    +1.00002368 h +0.37409196 h^2
    +0.09678418 h^3 -0.18628806 h^4
    +0.27886807 h^5 -1.13520398 h^6
    +1.48851587* h^7 -0.82215223 h^8
    +0.17087277 h^9))
  }
\ExplSyntaxOff

\begin{document}
\section{Test with \texttt{l3fp}}
\xdef\x{0.61}
sn(\x) = \fpeval{round(Phi(\x), 5)}
\end{document}
% Source - https://tex.stackexchange.com/q/758979
% Posted by cis, modified by community. See post 'Timeline' for change history
% Retrieved 2026-01-30, License - CC BY-SA 4.0

\documentclass[margin=10mm, varwidth]{standalone}

\ExplSyntaxOn
\fp_new_function:n { Phi }
\fp_new_function:n { PhiAux }
\fp_set_function:nnn { Phi } { x } { PhiAux(x, 1/(1 + 0.25 * sqrt(2) * x)) }
\fp_set_function:nnn { PhiAux } { x, h }
  {
    0.5 (2-h * exp(
    -0.5 x^2 -1.26551223
    % Horner's method for the polynomial is faster
    + h *( 1.00002368
    + h *( 0.37409196
    + h *( 0.09678418 
    + h *(-0.18628806
    + h *( 0.27886807 
    + h *(-1.13520398
    + h *( 1.48851587
    + h *(-0.82215223
    + h *  0.17087277 
    ))))))))))
  }
\ExplSyntaxOff

\begin{document}
\section{Test with \texttt{l3fp}}
\xdef\x{0.61}
sn(\x) = \fpeval{round(Phi(\x), 5)}
\end{document}
added 926 characters in body
Source Link
jps
  • 80.4k
  • 2
  • 89
  • 158

Alternative solution: Use l3fp and define a function in it (well, two actually, to only calculate h once):

% Source - https://tex.stackexchange.com/q/758979
% Posted by cis, modified by community. See post 'Timeline' for change history
% Retrieved 2026-01-30, License - CC BY-SA 4.0

\documentclass[margin=10mm, varwidth]{standalone}

\ExplSyntaxOn
\fp_new_function:n { Phi }
\fp_new_function:n { PhiAux }
\fp_set_function:nnn { Phi } { x } { PhiAux(x, 1/(1 + 0.25 * sqrt(2) * x)) }
\fp_set_function:nnn { PhiAux } { x, h }
  {
    0.5 (2-h * exp(
    -0.5 x^2 -1.26551223
    +1.00002368 h +0.37409196 h^2
    +0.09678418 h^3 -0.18628806 h^4
    +0.27886807 h^5 -1.13520398 h^6
    +1.48851587* h^7 -0.82215223 h^8
    +0.17087277 h^9))
  }
\ExplSyntaxOff

\begin{document}
\section{Test with \texttt{l3fp}}
\xdef\x{0.61}
sn(\x) = \fpeval{round(Phi(\x), 5)}
\end{document}

Alternative solution: Use l3fp and define a function in it (well, two actually, to only calculate h once):

% Source - https://tex.stackexchange.com/q/758979
% Posted by cis, modified by community. See post 'Timeline' for change history
% Retrieved 2026-01-30, License - CC BY-SA 4.0

\documentclass[margin=10mm, varwidth]{standalone}

\ExplSyntaxOn
\fp_new_function:n { Phi }
\fp_new_function:n { PhiAux }
\fp_set_function:nnn { Phi } { x } { PhiAux(x, 1/(1 + 0.25 * sqrt(2) * x)) }
\fp_set_function:nnn { PhiAux } { x, h }
  {
    0.5 (2-h * exp(
    -0.5 x^2 -1.26551223
    +1.00002368 h +0.37409196 h^2
    +0.09678418 h^3 -0.18628806 h^4
    +0.27886807 h^5 -1.13520398 h^6
    +1.48851587* h^7 -0.82215223 h^8
    +0.17087277 h^9))
  }
\ExplSyntaxOff

\begin{document}
\section{Test with \texttt{l3fp}}
\xdef\x{0.61}
sn(\x) = \fpeval{round(Phi(\x), 5)}
\end{document}
Source Link
jps
  • 80.4k
  • 2
  • 89
  • 158

You can expand the value before you hand it to \eval*. The following does so by defining a new command that expands its mandatory argument once if the starred version was used:

% Source - https://tex.stackexchange.com/q/758979
% Posted by cis, modified by community. See post 'Timeline' for change history
% Retrieved 2026-01-30, License - CC BY-SA 4.0

\documentclass[margin=10mm, varwidth]{standalone}
\usepackage{numerica}
\NewDocumentCommand\MaybeExpandOnce{mm}
  {\IfBooleanT{#1}{\edef\ProcessedArgument{\unexpanded\expandafter{#2}}}}
\NewDocumentCommand\evalNormalDist{s >{\MaybeExpandOnce{#1}}m O{5}}
  {%
    \eval*
      {%
        0.5 (2-h \exp(
        -0.5 x^2 -1.26551223
        +1.00002368 h +0.37409196 h^2
        +0.09678418 h^3 -0.18628806 h^4
        +0.27886807 h^5 -1.13520398 h^6
        +1.48851587* h^7 -0.82215223 h^8
        +0.17087277 h^9))
      }
      [
        h=1/(1+0.25*\sqrt{2}*x),  x={#2}% cannot use \x here :(
      ]
      [#3]%
  }

\begin{document}
\section{Test with \texttt{numerica}}
\xdef\x{0.61}
sn(\x) = \evalNormalDist*{\x}
\section{Test with \texttt{xint}}
... ? ....
\end{document}