NO

Author Topic: printf "long long": -9223372036854775808LL  (Read 3236 times)

albertn

  • Guest
printf "long long": -9223372036854775808LL
« on: March 27, 2007, 11:44:02 AM »
Hello!

The minimum "long long" is -2^63 = -9223372036854775808

Bug:
=================
printf("%lld \n", -9223372036854775808LL);
returns
-9223372036854775807
-> *wrong last digit*


/********************* signed_long_long.c **********************/
#include <stdio.h>

int main(void)
{
    long long longlong_v;
    /* 8 bytes = 64 bits, signed range from -9223372036854775808 to 9223372036854775807 */

    printf("%lld \n", -9223372036854775808LL); /* output : PROBLEM!! */

    longlong_v = -9223372036854775808LL;
    printf("%lld \n", longlong_v); /* output : PROBLEM!! */

    return 0;
}


Note that compiling (cc signed_long_long.c) gives warning #2072
warning #2072: Overflow in constant '9223372036854775808LL'.
...Did it miss the "-" (minus) ?


Kind regards,
Albert

cane

  • Guest
Re: printf "long long": -9223372036854775808LL
« Reply #1 on: March 27, 2007, 12:49:38 PM »
According to C99 standard LLONG_MIN is -(2^63 - 1), hence no bug (formally).
However in limits.h there's

#define LLONG_MAX   0x7fffffffffffffff      /* maximum signed long long value */
#define LLONG_MIN   (-LLONG_MAX - 1)        /* minimum signed long long value */

This may be problematic (?).
« Last Edit: March 27, 2007, 12:54:36 PM by cane »

albertn

  • Guest
Re: printf "long long": -9223372036854775808LL
« Reply #2 on: March 27, 2007, 08:42:12 PM »
Wow true!

And in addition:
All the signed values now have 1 value less for their most negative range!!

signed char: -127 to +127
signed short: -32767 to 32767
etc.

These are the minimum ranges, however the standard (see link below) says that

implementations can have larger absolute ranges:
Quote: "Their implementation-defined values shall be equal or greater in magnitude
(absolute value) to those shown, with the same sign."
(page-21 = pdf-page-33)


See it from the 2005 draft C99 Standard here:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf#page=34
(page-22 = pdf-page-34)

Hmmm... I'll be searching for a rationale for this, 'cause I think this is very strange. In
fact this means, that to guarantee that C99 code is portable, one has to assume that the minimum value is odd not even (e.g. -127 for char). Obviously there will hardly ever be a compiler that does not support a "standard 2's complement min char value" of -128.

Coding for e.g. the min. signed char of -127 will lead to unnecessary complications, as is shown in the "ultimate C99 standard-'decryption' book": "The New C Standard: A Cultural and Economic Commentary" by Derek M. Jones by (sentance-305 = pdf-page-359,360)
http://www.coding-guidelines.com/cbook/cbook1_0b.pdf#page=359&zoom=100,0,780

see also http://www.coding-guidelines.com/cbook/cbook1_0b.pdf#page=357&zoom=100,0,195
(http://www.knosof.co.uk/cbook/cbook.html)


Kind regards,
Albert
« Last Edit: March 28, 2007, 12:47:02 AM by albertn »