Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strtoul on negative numbers

1,308 views
Skip to first unread message

Jerry Schwarz

unread,
Sep 30, 1992, 1:58:35 PM9/30/92
to
What is the result of

strtoul("-2", (char**)0, 10)

The description is clear that the minus sign is allowed.
The relevant sentence describing the result is.

"If the correct value is outside the range of representable values
ULONG_MAX is returned ..."

My reading of this is that it requires ULONG_MAX to be returned,
but others think that -2 is a representable value (equal to
ULONG_MAX-1) and so ULONG_MAX-1 should be returned.

-- Jerry Schwarz

Michael Henning

unread,
Sep 30, 1992, 7:48:08 PM9/30/92
to
j...@lucid.com (Jerry Schwarz) writes:


All implementations I have seen return ULONG_MAX - 1.
I believe that this is incorrect behaviour.
-2 is not a value representable in an unsigned long, in my opionion.

Michi.
--
-m------- Michael Henning +61 75 950255
---mmm----- Pyramid Technology +61 75 722475 FAX
-----mmmmm--- Research Park, Bond University mi...@ptcburp.ptcbu.oz.au
-------mmmmmmm- Gold Coast, Q 4229, AUSTRALIA uunet!munnari!ptcburp.oz!michi

Norman Diamond

unread,
Sep 30, 1992, 10:38:45 PM9/30/92
to
In article <1992Sep30....@lucid.com> j...@lucid.com (Jerry Schwarz) writes:
>What is the result of
> strtoul("-2", (char**)0, 10)
>The description is clear that the minus sign is allowed.

The description is schizophrenic. ANSI 4.10.1.6, page 153 line 20:
"a subject sequence resembling an unsigned integer"
(in contrast to strtol, ANSI 4.10.1.5, page 152 lines 23 to 24:
"a subject sequence resembling an integer").
But later, page 153 lines 25 and 28 and 31 allow a plus or minus sign.

Lines 22 to 23: "Then it attempts to convert the subject sequence to an
unsigned integer, and returns the result" (again in contrast to strtol,
which omits the word "unsigned"). So do negation and an implied cast to
unsigned occur during this conversion?
Lines 40 to 41: "If the subject sequence begins with a minus sign, the
value resulting from conversion is negated." So does negation occur
after conversion is finished? Is there no implied cast to unsigned?

>The relevant sentence describing the result is.
>"If the correct value is outside the range of representable values
>ULONG_MAX is returned ..."

Uh yeah, but which is the "correct" value?
I think that section 4.10.1.6 seems both self-contradictory and ambiguous,
so it is impossible to fully conform, but possible in several ways to
partially conform.
--
Norman Diamond dia...@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"Yeah -- bad wiring. That was probably it. Very bad."

T. William Wells

unread,
Oct 1, 1992, 3:51:29 AM10/1/92
to
In article <1992Sep30....@lucid.com> j...@lucid.com (Jerry Schwarz) writes:
: What is the result of

The definition for strtoul says that one converts the number (to
an unsigned long) ignoring the minus sign and then negates it.
Negation is defined for all values of an unsigned type, resulting
in ULONG_MAX+1-x, where x is 2, for this case.

---
Bill { rutgers | decwrl | telesci }!twwells!bill
bi...@twwells.com

T. William Wells

unread,
Oct 1, 1992, 3:59:37 AM10/1/92
to
In article <1992Sep30.2...@ptcburp.ptcbu.oz.au> mi...@ptcburp.ptcbu.oz.au (Michael Henning) writes:
: All implementations I have seen return ULONG_MAX - 1.

This is the correct behavior.

: I believe that this is incorrect behaviour.


: -2 is not a value representable in an unsigned long, in my opionion.

So? Who says that the value -2 is represented?

What *is* represented is the value of 2, negated. For C,
operations like add and subtract (and negate, considered as 0-x)
on unsigned types are defined as modular arithmetic where the
result is always kept in the range 0..max-1.

Phil Howard

unread,
Oct 2, 1992, 5:01:12 PM10/2/92
to
bi...@twwells.com (T. William Wells) writes:

>The definition for strtoul says that one converts the number (to
>an unsigned long) ignoring the minus sign and then negates it.
>Negation is defined for all values of an unsigned type, resulting
>in ULONG_MAX+1-x, where x is 2, for this case.

I take it that your interpretation is saying that strtoul() is supposed
to return an unsigned integer with the value ULONG_MAX+1-x whenever an
input number has the form of a "-" followed by a properly formed unsigned
quantity.

If the representation given is outside of the range of ULONG_MAX (the part
following the "-", not including it) then ULONG_MAX would be returned. But
what if such an out of range number is preceeded by a "-"? Do we return
the value ULONG_MAX+1-ULONG_MAX (which would be just 1) or do we return the
value ULONG_MAX?

0 new messages