Mail Archives: djgpp/1998/08/28/12:03:37
Kevin Ashley wrote:
> >> double dvar;
> >> dvar = 25834242042;
> > This line WILL work, I've already uploaded example code that shows this working.
>
> I hadn't seen your example when I posted. However, I think there is
> still some room for both to be correct. Your example was C++; I don't
> program in that, but use C exclusively. I'm familiar with the C standard
> but not with C++. As far as C is concerned, that constant is an integer
> constant, which is then effectively cast to a double at compilation time.
> If it's too big to fit in an int, the results are undefined.
It'll work in C as well:
#include <stdio.h>
int main()
{
double Double = 25834242042;
printf("%15f\n", Double);
}
Output:
25834242042.000000
> GCC produces warnings at compilation time that the constant is too big
> for an integer, and that it will end up being unsigned. I suspect it
> must internally store the evaluation in a long long int, since it
> does produce correct results despite the warnings, but this is definitely
> compiler-specific behaviour and cannot be relied upon in general. I've
> tried it on another compiler (ANSI-compatible but not GCC) and both
> the double and the int end up equal to 64438266 (with a similar slew
> of warnings.)
>
> So, it may work, but if the target language is C it isn't right and
> it isn't portable and sooner or later it may cause you problems. If
> we're talking C++, I confess ignorance.
Well, if people are mentioning compiler specific stuff like 'long long', then
I thought it would be ok if I mentioned that you could initialized a double
with a literal int. Even if it isn't portable.
--
(\/) Endlisnis (\/)
s257m AT unb DOT ca
Endlisnis AT GeoCities DOT com
Endlis AT nbnet DOT nb DOT ca
- Raw text -