Date: Sat, 19 Nov 94 05:19:21 GMT From: dolan AT fnoc DOT navy DOT mil (Kent Dolan) To: gordon AT spot DOT colorado DOT edu Subject: Re: printf or floating point error????? Cc: djgpp AT sun DOT soe DOT clarkson DOT edu Allen, float ans,num,inv; double ansd, numd,invd; long double ansld,invld,numld; num = 824633702441.0; numd = ( double)824633702441.0; numld = (long double) 824633702441.0; I will probably get corrected on this, but I don't think casting the constant values is the correct thing to do, and I think the problem is not in your compiler or your printf() routine, but in your understanding of what a cast does. A cast does not say "convert this value on the right to this type and assign it to the recipient on the left", a cast says "for right now, the bit pattern contained in the piece of _storage_ at the location indicated by the object being cast IS this type, use it as this type and pass the appropriate value under that interpretation into the recipient on the left", not at all the same thing, it turns out. Back at least in K&R C, I would have expected the constant to be a double in each assignment, because that is what K&R makes the default for a floating point value. num = 824633702441.0; The first assignment does no harm because it has no cast; the double constant loses some precision to become the value of a float variable, num. numd = ( double)824633702441.0; The second assignment, cast and all, does no harm because the cast does not change the size of the double constant, so it just gets copied, bit by bit as it were, into the double variable, numd. numld = (long double) 824633702441.0; The third assignment, however, takes the bit pattern for a double, the next succeeding set of bytes the same size, takes your word for it that they comprise the bit pattern of a long double, and _interpretes the bits_ as a long double, almost certainly not what you intended, and the reason for the unexpectedly large exponents. The problem with Turbo C is that it tries very hard to be a Do What I Mean compiler, and C is very, very much a Do What I Say language; if there is any legal interpretation at all of some C you type in, the compiler is supposed to blunder ahead and do what you told it to do, consequences be damned. This is why "the C language" and "software engineering" should never be spoken of in the same breath except with some phrase like "makes it impossible to do" between them. Thus, what you have really discovered is a bug in Turbo C. It won't be the last. Don't feel bad, this has caught every single person who ever got past "Hello, world!" in C. Take it as an indication of your progress. Humility not spoken here. Xanthian. -- Kent, the man from xanth. Kent Paul Dolan, CSC contractor at Fleet Numerical. (408) 656-4363. (Navy Unix email: ) (Navy cc:Mail email: ) (real world email: )