# How can I define NaN in LaTeX3

LaTeX3 l3fp offers a way to check if a variable has a an infinite value. The following code defines a number and then sets its value to π/2. It then calculates tan(π/2) to get a large result.

\ExplSyntaxOn
\fp_new:N \mynumber
\fp_new:N \pi_half
\fp_div:Nn \pi_half{\c_pi_fp/2}
\fp_tan:Nn  \mynumber{\pi_half}
\texttt{\mynumber}\\

\fp_if_infinity:NTF \c_infinity_fp {NaN}{Do~something~with~\number}\\

\ExplSyntaxOff


In many languages the indeterminate form is normally indicated by NaN what would be a good strategy to define a macro to test for this in expl3 for all cases of NaN, such as division by zero etc?

-
As I've said in my updated answer, I think this actually needs some revision. – Joseph Wright Nov 25 '10 at 20:37

The FPU for LaTeX3 has undergone some major changes. Up to the version included in the DVD of TeX Live 2012 (mid-June 2012) the 'old' FPU worked one way. The improved FPU, available from the development repository and scheduled for release to CTAN some time in late June 2012, is expandable and features a number of improvements.

The new FPU recognises the 'not a number' concept

\fp_set:Nn \l_tmpa_fp { nan }


NaN is not equal to any value, not even another NaN: this is the standard approach in many languages. Thus

\fp_compare:nNnTF { nan } = { nan } { \TRUE } { \FALSE }


is FALSE.

There are currently special markers in expl3 for infinite and undefined results, \c_infinite_fp and \c_undefined_fp. Division by exactly zero is undefined:

\fp_div:Nn \l_my_fp { 0 }
\fp_if_undefined:NTF \l_my_fp { TRUE } { FALSE }


gives TRUE.

Thinking about this again, I notice that the tangent of π/2 should not actually be infinite as it does not have a limit (thinking about the various tests for limits of series). I suspect this should be altered: expect the next expl3 update to improve in this area.

-
@Joseph Thanks! I was just thinking of something such as fp_if_NaN, but as you say you covered the bases. Will take a look at the LaTeX-L list. – Yiannis Lazarides Nov 25 '10 at 20:40
@Yiannis: I've just checked in a change so that both division by zero and tan(π/2) will give \c_undefined_fp as result, which is tested for using \fp_if_undefined:N(TF). This will go to CTAN in a moment. – Joseph Wright Nov 25 '10 at 20:47
@Joseph Good correction as the domain of the inverse tangent function is all real numbers, but neither -infinity nor infinity is included in that set. – Yiannis Lazarides Nov 25 '10 at 20:53
@Joseph: I'd find it nice to distinguish "undefined" and "infinity" resp. "-infinity". But for the latter there are probably not many instances. For \tan in radians you don't need "undefined" at all since \tan is defined on all rational numbers, but the Cotangent is undefined at 0. – Hendrik Vogt Nov 26 '10 at 11:14
@FedericoPoloni That is the aim: Bruno Le FLoch has done a lot of work on this. At the point of writing, there are I believe some issues with subnormal numbers/underflows, but we should not be in a bad position. However, the standard leaves open some variability, which poses a few questions for TeX developers (for example in transcendental functions). – Joseph Wright Jun 2 '12 at 14:56