[comp.std.c] strtoul

gafter@m2.csc.ti.com (Neal M Gafter) (07/25/90)

I have implemented the ANSI library function strtoul() [4.10.1.6] and
had a little problem interpreting the standard.  To quote:

	If the subject sequence begins with a minus sign, the value
	resulting from the conversion is negated.
and
	If the correct value is outside the range of representable
	values, ULONG_MAX is returned, and the value of the macro
	ERANGE is stored in errno.

My question is: what is the `range of representable values'?  Unsigned
longs can represent values in the range [0..ULONG_MAX].  Alternately,
one might argue that any integer is representable as an unsigned long,
since unsigneds operate in modular arithmetic.  This latter
possibility would imply that the macro errno is never set to ERANGE.

Discounting the possibility that X3J11 intended errno never to be set
to ERANGE by strtoul(), I assumed X3J11 meant the range
[0..ULONG_MAX].  I presumed the sentence about the minus sign was to
allow the string "-0" and to recognize (as errors) other negative
numbers.  Thus, I assumed any string representing a negative number
should cause errno to be set to ERANGE and strtoul() to return
ULONG_MAX.

I talked to two different X3J11 committee members to confirm my
interpretation.  To my surprise, I got two different answers, both
different from mine.  One suggested the range [-ULONG_MAX..ULONG_MAX]
be allowed, the other suggested [LONG_MIN..ULONG_MAX], in both cases
with the returned result being computed using modular arithmetic.

Can the committee please create a definitive interpretation?
--
Arpa:	gafter@csc.ti.com (Neal Gafter)
USnail:	Texas Instruments MS 238, PO BOX 655474, Dallas, TX 75265
phone:	(214) 995 - 0696 (office)  or  (214) 727 - 2082 (home)
-- 
Arpa:	gafter@csc.ti.com (Neal Gafter)
USnail:	Texas Instruments MS 238, PO BOX 655474, Dallas, TX 75265
phone:	(214) 995 - 0696 (office)  or  (214) 727 - 2082 (home)

scjones@thor.UUCP (Larry Jones) (07/26/90)

In article <GAFTER.90Jul24163928@m2.csc.ti.com>, gafter@m2.csc.ti.com (Neal M Gafter) writes:
> I have implemented the ANSI library function strtoul() [4.10.1.6] and
> had a little problem interpreting the standard.  To quote:
> 
> 	If the subject sequence begins with a minus sign, the value
> 	resulting from the conversion is negated.
> and
> 	If the correct value is outside the range of representable
> 	values, ULONG_MAX is returned, and the value of the macro
> 	ERANGE is stored in errno.
> 
> Discounting the possibility that X3J11 intended errno never to be set
> to ERANGE by strtoul(), I assumed X3J11 meant the range
> [0..ULONG_MAX].  I presumed the sentence about the minus sign was to
> allow the string "-0" and to recognize (as errors) other negative
> numbers.  Thus, I assumed any string representing a negative number
> should cause errno to be set to ERANGE and strtoul() to return
> ULONG_MAX.

This was discussed at the last meeting.  The (unanimous, as I recall)
sense of the committee was that the intended meaning of the above is
that the digit sequence is to be converted to an unsigned long.  If
the value of the digit sequence is greater than ULONG_MAX, then the
unsigned long is set to ULONG_MAX and errno is set.  If there was a
leading minus sign, the unsigned long is negated according to the
rules for negation of unsigned values.  The resulting unsigned long
value is then returned.

I don't believe that this was a formal interpretation.  If you want
one, send a request to the X3 Secretariat, CBEMA.
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones@thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"This probably just goes to show something, but I sure don't know what."
-Calvin

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/27/90)

In article <GAFTER.90Jul24163928@m2.csc.ti.com> <gafter@csc.ti.com> (Neal M Gafter) writes:
>...  I presumed the sentence about the minus sign was to
>allow the string "-0" and to recognize (as errors) other negative
>numbers.

No, in fact this was discussed at the last X3J11 interpretation meeting.
The negation is modulo longwordsize, and thus cannot overflow if the
conversion of the portion after the "-" prefix did not overflow.  (By
"overflow" I mean that the converted value value is not representable
in an unsigned long datum.)