Will converting a string to a double equal the literal double?

This is not guaranteed by the C standard. The semantics of converting a floating-point literal in the source code are specified in C 2011 [draft N1570] 6.4.4.2. This says the recommended, but not required, practice is that the translation-time conversion of floating-point constants should match the execution-time conversion of character strings by library functions, such as strtod.

More than that, the standard does not even require that two different literals with the same mathematical value, such as 1.23 and 1.230, convert to the same value. These examples come from footnote 75 in the standard, which is a footnote to a paragraph stating that all floating-point constants of the same source form shall convert to the same value. Thus, 1.23 always converts to the same value wherever it appears in the source, but 1.230 does not necessarily convert to the same value as 1.23 or 123e-2. Even 123e-2 and 123e-02 may be different.

atof(p) is specified in 7.22.1.2 to be equivalent to strtod(p, (char **) NULL) except for how they behave with errors. strtod is specified in 7.22.1.3. That clause has some recommended practices for accuracy but is silent about matching the translation-time conversion of literals.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)