Author: Sune Fischer
Date: 09:27:23 02/08/04
Go up one level in this thread
On February 08, 2004 at 10:29:19, Uri Blass wrote:
>On February 08, 2004 at 09:59:37, Sune Fischer wrote:
>
>>>
>>>Under MSVC, test the following:
>>>
>>> unsigned __int64 a = (1 << 50);
>>> printf("%I64u\n", a);
>>>
>>>What will the result be?... 0 ! No errors, no warnings.
>>
>>I think the compiler sees the code as:
>>
>>int one = 1;
>>__int64 a = one<<50;
>>
>>so the behavior is expected, no warning should be given IMO, there are plenty of
>>cases where you deliberately want to over shift something to produce a zero.
>
>I disagree.
>You could claim that the compiler should never give a warning by this logic.
>
>The behaviour is clearly not expected for the programmer who writes that
>code(otherwise the programmer could write simply __int64 a=0;
Perhaps a warning wouldn't hurt, although I'm not sure the compiler in general
will analyze mathematical expressions to see if they produce something
illogical.
For instance if you have x=a*c*0*d/2; do you expect the compiler to give a
warning here or should it just compute x=0?
Suppose you have a table of constants, some of which enter into the complex
expression and are zero. In principle the compiler should be able to unroll the
loops and give the same warnings.
>>>After debugging for several hours I finally found the problem and fixed it
>>>using:
>>>
>>> unsigned __int64 a = (1ui64 << 50);
>>>
>>>Yes, 1 should be written as 1ui64, or another solution:
>>>
>>> unsigned __int64 a = (((unsigned __int64) 1) << 50);
>>>
>>>Casting 1 to 64 bit.
>>
>>You must do the cast before the operation, ie. if you do something like:
>>
>>int x=...,y=...;
>>__int64 f = x*y;
>>
>>it will not produce a __int64 result, similarly for a shift operation.
>
>Yes and I think that it is a mistake in defining the standard C behaviour.
I don't think it is a mistake.
If you take two floats and multiply them the result is a float and not a double.
If you wanted a double result you should have used double oprands (or casted to
double).
Multiply a float with a float you get a float, divide an integer with an integer
you get an integer, an appealing consitancy which is easy to remember.
It needs explicit defining only in cases like a float times an integer, I can
never remember which takes precedence so I always do the cast first on one of
them.
-S.
>Uri
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.