This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++/4835: Big = (unsigned long long)value; sets big (an unsigned long long) to zero if value is a float or double with a negative value



>Number:         4835
>Category:       c++
>Synopsis:       Big = (unsigned long long)value;  sets big (an unsigned long long) to zero if value is a float or double with a negative value
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 08 11:36:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Ted Rossin
>Release:        unknown-1.0
>Organization:
>Environment:
HP-UX poophead B.10.20 A 9000/785 2007526995 two-user license
 
gcc-core-2.95.2.tar.gz and gcc-g++-2.95.2.tar.gz
>Description:
The program below generates the following output:

The value is 0x0000000000000000
The value is 0xfffffffffffffe62
The value is 0xfffffe62

The problem is that a negative float or double cast to an unsigned long long is set to zero instead of the negative value.  If a negative float or double is cast to an unsigned long it behaves as expected.

--------------------------------------------------------

main()
{
    double value;
    unsigned long long Big;
    int hey;
    unsigned long ulong;

    value = -414.0;
    hey = 10;

    Big = (unsigned long long)value;
    printf("The value is 0x%016llx\n",Big);
    Big = (unsigned long long)((long long)value);
    printf("The value is 0x%016llx\n",Big);

    ulong = (unsigned long)value;
    printf("The value is 0x%08x\n",ulong);

}
>How-To-Repeat:
compile and run the program.
>Fix:
The workaround is to cast to an long long before casting to an unsigned long long.

Big = (unsigned long long)((long long)value);
>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]