strtoul of negative numbers

Fred Tydeman uunet!ibminet.awdpa.ibm.com!ibmpa!tydeman
Wed Mar 24 12:02:34 PST 1993


Thank you'll for the quick response to the question on conversion
of -1.0 to unsigned int (undefined, but many compilers implement it as
double to int to unsigned int, so end up with all bits set).
 
Now, for another point of confusion.
 
Assuming that unsigned long int is 32-bits wide, what are the return
values (in base 16) and errno for the following:
 
    strtoul( " 100000000", NULL, 16 );
    strtoul( "  FFFFFFFF", NULL, 16 );
    strtoul( "  FFFFFFFE", NULL, 16 );
    strtoul( "  80000001", NULL, 16 );
    strtoul( "  80000000", NULL, 16 );
    strtoul( "  7FFFFFFF", NULL, 16 );
    strtoul( "         1", NULL, 16 );
    strtoul( "         0", NULL, 16 );
    strtoul( "        -1", NULL, 16 );
    strtoul( " -7FFFFFFF", NULL, 16 );
    strtoul( " -80000000", NULL, 16 );
    strtoul( " -80000001", NULL, 16 );
    strtoul( " -FFFFFFFE", NULL, 16 );
    strtoul( " -FFFFFFFF", NULL, 16 );
    strtoul( "-100000000", NULL, 16 );
 
Some sections from ANSI C, X3.159-1989, that may be relevant:
 
3.1.2.5 Types
Page 24, line 5, "A computation involving unsigned operands can never
overflow, because a result that cannot be represented by the resulting
unsigned integer type is reduced modulo the number that is one greater
than the largest value that can be represented by the resulting unsigned
integer type."
 
3.2.1.1 Characters and Integers (page 35) and footnote 27.
Includes a discussion on integral promotions.
 
3.2.1.2 Signed and Unsigned Integers (page 35) and footnote 28.
Includes a discussion on signed and unsigned conversions.
 
3.3.3.3 Unary Arithmetic Operators
Page 45, line 11:  "The result of the unary - operator is the negative
of its operand."
 
4.10.1.6 The strtoul Function
Page 153, line 41:  "If the subject sequence begins with a minus sign,
the value resulting from the conversion is negated."
 
Page 154, line 3: "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."
 
---
 
 From the above, I do not see a definition of negative of an unsigned
long.  It seems to me that the unsigned must be converted to signed,
then the negation done, then converted back to unsigned.
 
What is the range of representable values for unsigned long?  Is it
[0..ULONG_MAX], [-ULONG_MAX..ULONG_MAX], everything is representable
modulo ULONG_MAX+1, or something else?
 
These are not IBM's views, these are the personal ones of:
Fred Tydeman, IBM, Austin, Texas (512) 838-3322; fax (512) 838-3484
AIX S/6000 Math library architect & IBM's rep to NCEG (X3J11.1)
Internet: tydemanaibmpa.awdpa.ibm.com    uucp: uunet!ibmsupt!tydeman



More information about the Numeric-interest mailing list