In <
[email protected]>
(e-mail address removed) (David Resnick) writes: [...]
Perhaps your program is not running in the "C" locale. In which case the
character that represents the decimal point depends on the locale.
Nope:
6 In other than the "C" locale, additional locale-specific subject
^^^^^^^^^^
sequence forms may be accepted.
This means that '.' is accepted in ALL locales, but other locales can also
accept other characters as the "decimal point".
No, I don't think it does.
The standard defines the atof function as (nearly) equivalent to a
call to the strtod function.
The description of the strtod function (C99 7.20.1.3) says:
The expected form of the subject sequence is an optional plus or
minus sign, then one of the following:
-- a nonempty sequence of decimal digits optionally containing
a decimal-point character, then an optional exponent part
as defined in 6.4.4.2;
It uses the term "decimal-point character", not "period". A few
paragraphs later, it says:
If the subject sequence has the expected form for a floating-point
number, the sequence of characters starting with the first digit
or the decimal-point character (whichever occurs first) is
interpreted as a floating constant according to the rules of
6.4.4.2, except that the decimal-point character is used in place
of a period, [...]
(The wording in the C90 standard is similar; it's easier to
cut-and-paste from my copy of the C99 standard.)
Your library is broken in multiple places. The correct output of your
program consists of three *identical* lines because "12.345" is valid
input for strtod in *any* locale and because the current locale does
NOT affect the output of the %g conversion specifier (for reasons
that I do not understand, the Single Unix Specification is at odds
with the C standard here).
The description of the %e and %f conversion specifiers also uses the
term "decimal-point character", not "period". ("%g" is defined in
terms of "%e" and "%f".)
C99 7.1.1p2 says (underscores indicate italics):
The _decimal-point character_ is the character used by functions
that convert floating-point numbers to or from character sequences
to denote the beginning of the fractional part of such character
sequences. It is represented in the text and examples by a
period, but may be changed by the setlocale function.
Again, the wording in the C90 standard is similar.
For locales other than "C", the numeric conversion functions and
formatted I/O functions are not required to recognize '.' as a
decimal-point character.