truncation compiler warning

Started by
10 comments, last by GameDev.net 18 years, 3 months ago
Visual C++ 6.0 doesn't seem to be consistant in generating double to float truncation warnings. For example, I've always added this in headers:
#define pi 3.1415926
and commonly have lines like:
float angle = 2.0;
angle = angle - pi;
But the compiler only rarely generates this warning:
warning C4305: '=' : truncation from 'const double' to 'float'
I've compiled this project many times already but all of the sudden I've got 30+ truncation warnings cluttering my output window. Maybe this is about poor coding style, but my question is why is the compiler suddenly insisting on generating the warnings when it ignored them before? PS why don't plus signs show up in post previews(or in the forum too?)? - +
Advertisement
Quote:Original post by avocados
Visual C++ 6.0 doesn't seem to be consistant in generating double to float truncation warnings.
I think it is, actually, it's only complaining about constants that can be represented with higher precision as doubles.
You can acknowledge the warnings by adding f postfix to the floating point literals (i.e. 10.3f).

If you can't find another workaround you can always disable invidiual warnings.
#pragma warning(disable : 4305)
I just found one of my old projects that had 0 warnings and added these two lines into the main function:
float abc = 2.0f;abc += pi;

Then I recompiled with 0 warnings. I pasted the same two lines into my current project and recompiled and got a truncation warning for the line that adds pi. From what I can tell, there really is an inconsistancy here.

What about the sine functions? I've never had to add (float) in front of them before, is that standard practice?
Quote:Original post by avocados
I just found one of my old projects that had 0 warnings and added these two lines into the main function:
float abc = 2.0f;abc += pi;

Then I recompiled with 0 warnings. I pasted the same two lines into my current project and recompiled and got a truncation warning for the line that adds pi. From what I can tell, there really is an inconsistancy here.
You're probably using different warning levels for the two projects.
Anyway, the PI constant is still a double precision constant. But since you probably want to keep it at full precision I recommend casting it manually within the conversion instead (abc += (float) pi;).
Of course you can always disable to warning, but OTOH they've helped me track down precision issues on more than one occasion.

Quote:Original post by avocados
What about the sine functions? I've never had to add (float) in front of them before, is that standard practice?
C has special float versions of all floating point math functions (sinf, cosf, tanf, powf, etc..). In C++ they're also overloaded on the input type, so sin(3.0f) would return a float for instance.
You wont get warnings for modules that have been previously compiled without errors. i.e. if it only gave warnings, it wont recompile it next time. What you need to do is do a rebuild-all, then all warnings should show up.

Also, it wont give a warning about 2.0 being assigned to a float because 2.0 is exactly representable as a float. But something like 0.1 cannot be represented exactly in binary, but the larger the data type, the more accurate the approximation is. When assigning the approximation of 0.1 (as a double), to a float, the approximation is furthur approximated, making it even less accurate. This gives a warning.

The bottom line is, put an f at the end if you're assigning it to a float.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I use MSVC++ on a regular basis and it does not abide by the language completely. As you have seen it like to gripe about precision. The biggest problem is with variable scoping. for example the following code will not compile in MSVC++ 6

for(int i = 0; i < 10; i++){}// "i" should be out of scope but is not in MSVC++ 6 and will not compilefor(int i = 0; i < 5; i++){}


So basically just disable the warnings or put f's after all your float values (its not that hard to double click the warning and type f). My point is that this compiler does not abide by all the standards and theres really not much you can do about it.

Ok, I'll start keeping track of precisions. Thanks for the floating point math functions, that makes the idea more appealing.

But there is still an inconsistancy in the warnings. I didn't notice it before but of the two source files in my project, only main.cpp generates truncation warnings. Both have the float + double addition line and both source files are set to warning level 3. Oh, and I am using the "Rebuild all" menu command.
Quote:Original post by Anonymous Poster
I use MSVC++ on a regular basis and it does not abide by the language completely. As you have seen it like to gripe about precision. The biggest problem is with variable scoping. for example the following code will not compile in MSVC++ 6

*** Source Snippet Removed ***

So basically just disable the warnings or put f's after all your float values (its not that hard to double click the warning and type f). My point is that this compiler does not abide by all the standards and theres really not much you can do about it.

You're quite wrong. There are two things you can do about it. For your specific example, there are several well accepted solutions to the scoping problem, at least one of which is available in a knowledge base article on MSDN. I won't provide them due to the following point. For your generic complaint, there's no reason to continue using a decade old compiler when the newest version is available completely free. Not only does the latest version support proper scoping, it has a flag to disable it for old code.

CM
By the way,

Quote:Original post by avocados
Visual C++ 6.0 doesn't seem to be consistant in generating double to float truncation warnings. For example, I've always added this in headers:
#define pi 3.1415926


Stop that.

const double pi = 3.1415926;


Or better yet, get it from <cmath> (not directly; see here).
Quote:Original post by Anonymous Poster
I use MSVC++ on a regular basis and it does not abide by the language completely. As you have seen it like to gripe about precision. The biggest problem is with variable scoping.


One should not expect a prestandard compiler to be respectful of a standard that was written after it. Also, one should not use a prestandard compiler at all, if the goal is to write standard code.

This topic is closed to new replies.

Advertisement