strtoul and negative values

Fred Tydeman uunet!ibminet.awdpa.ibm.com!ibmpa!tydeman
Thu Mar 25 06:57:42 PST 1993


Dave,
 
Thanks for the quick response.
 
One interpretation:
 
The representable values of unsigned long int are [0...ULONG_MAX].
-3 is outside the range of representable values, therefore,
strtoul("-3",NULL,16) returns ULONG_MAX and sets errno to ERANGE.
 
By this interpretation, my first test sets errno to ERANGE (since
0x100000000 is larger than ULONG_MAX), all the tests with a '-' as part
of the number set errno to ERANGE (since negative numbers are not
representable), and the others convert OK.
 
As I read your response, the above is wrong.
 
The following is what I read as the correct interpretation:
 
For the time being, ignore the sign on the input string.  Convert the
string of digits, as if they were a positive number.  If that value
cannot be represented (because its value is larger than ULONG_MAX),
return ULONG_MAX and set errno to ERANGE.  If the converted (positive)
value can be represented, then if there was a leading '-' sign, perform
  uli = ULONG_MAX-uli+1;  /* uli = -uli; */
Return that (possibly negated) value.
 
By this interpretation, my first and last tests set errno to ERANGE and
the others leave errno alone and convert OK.
 
If all of the above is correct, there is no need to respond.  Thanks.
 
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