ntoab: Handle LONG_MIN case.
* lib/mes/ntoab.c (ntoab): Handle LONG_MIN case. * lib/tests/scaffold/60-math.c (main): Add test for INT_MIN and hex.
This commit is contained in:
parent
3b312ca983
commit
b11510f4da
|
@ -20,6 +20,10 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <mes/lib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define STR(x) #x
|
||||
#define XSTR(s) STR(s)
|
||||
|
||||
char *
|
||||
ntoab (long x, int base, int signed_p)
|
||||
|
@ -34,6 +38,25 @@ ntoab (long x, int base, int signed_p)
|
|||
if (signed_p && x < 0)
|
||||
{
|
||||
sign_p = 1;
|
||||
if (x == LONG_MIN)
|
||||
{
|
||||
/* Cannot do u = (-x), so avoid it. */
|
||||
long i;
|
||||
if (base == 10)
|
||||
return XSTR(LONG_MIN);
|
||||
/* We cause the same result as
|
||||
i = (-x) % base
|
||||
u = (-x) / base
|
||||
would in mathematics if x and base were integers.
|
||||
It will be the case that -base < x <= 0 after the loop.
|
||||
Because base > 1, the quotient will definitely fit into u.
|
||||
i will contain the last digit to print. */
|
||||
for (u = 0; x <= -base; x += base, ++u)
|
||||
;
|
||||
i = -x;
|
||||
*p-- = i > 9 ? 'a' + i - 10 : '0' + i;
|
||||
}
|
||||
else
|
||||
u = -x;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -196,6 +196,9 @@ ok1:
|
|||
if (strcmp ("-2147483648", itoa (i)))
|
||||
return 34;
|
||||
|
||||
if (strcmp ("-80000000", ntoab (i, 16, 1)))
|
||||
return 35;
|
||||
|
||||
oputs ("t: i = -2147483647\n");
|
||||
i = -2147483647;
|
||||
|
||||
|
|
Loading…
Reference in a new issue