What is the "char -sequence" argument for NaN generation functions?

In addition to the NAN macro, C99 has two ways to create a NaN value for a floating point number, the nanf(const char *tagp) and strtof("NAN(char-sequence)") functions.

Both of these NaN creation methods accept an optional string argument (* tagp in nanf() and a char sequence in the strtof method). What exactly does this string argument do? I could not find specific examples of how you use it. From cppreference.com we have:

A call to nan ("string") is equivalent to a call to strtod ("NAN (string)", (char **) NULL);

Call nan (") is equivalent to calling strtod (" NAN () ", (char **) NULL);

Call nan (NULL) is equivalent to calling strtod ("NAN", (char **) NULL);

And nan (3) says:

These functions return a view (defined by tagp) of a quiet NaN. [Notch] The tagp argument is used indefinitely. There are many NaN representations on IEEE 754 systems and tagp selects one.

This doesn’t really tell me what I can use for the tagp string or why I ever wanted to use it. Is there a list of available valid parameters for this tag line, and what would be the reason to use nanf(NULL) by default?

+6
source share
2 answers

Tl; Dr: tagp argument gives you the ability to have different NAN values.

This is from the man page for nan (3), which gives a bit more info on tagp .

Mostly:

The nan () functions return a quiet NaN whose final fractional field contains the result of the tagp conversion for an unsigned integer.

This gives you the ability to have different NAN values.

In particular, from the C99 Justification doc :

Other uses of NaN may be useful. Available portions of NaNs were used to encode supporting information, for example, about NaNs. Signal NaNs may be candidates for filling an uninitialized repository; and their accessible parts could distinguish between uninitialized floating objects. IEC 60559 NaNs signaling and trap handlers potentially provide hooks to support diagnostic information or to implement special arithmetic.

There is an implementation here . Keep in mind that this may or may not be standard, as indicated in the comments. However, this should give you an idea of ​​what tagp used tagp .

As on the manual page, you can see the replacement mentioned above:

 nan("1") = nan (7ff8000000000001) nan("2") = nan (7ff8000000000002) 

Full user page here:

NAN (3) BSD Library Feature Guide
NAN (3)

NAME nan - generate silent NaN

SYNTAXIS #include

  double nan(const char *tagp); long double nanl(const char *tagp); float nanf(const char *tagp); 

DESCRIPTION The nan () functions return a quiet NaN whose final fractional field contains the result of the tagp conversion for an unsigned integer. If tagp is too large to be included in the field of the final NaN fraction, then the least significant bits of the integer represented by tagp are used.

SPECIAL VALUES If tagp contains any non-numeric characters, the function returns the NaN field, zero.

If tagp is empty, the function returns a field in which the final fraction of NaN whos is zero.

STANDARDS The nan () functions comply with ISO / IEC 9899: 2011.

BSD July 1, 2008

+7
source

This does not tell me what I can use for tagp string or why I would ever want to use it.

Some floating point standards, such as IEEE-754, have slightly different NaN values. This specification of the nan function allows the implementation to select a specific NaN representation depending on the string in a method that can be determined by the implementation.

+3
source

Source: https://habr.com/ru/post/986690/


All Articles