Quote:Original post by Zipster The compiler will do the cast, and give you a warning (unless you have a low warning level). However at work we treat all warnings as errors, so if we never used the suffix we'd all be very unhappy campers.
Quote:Original post by Zipster The compiler will do the cast, and give you a warning (unless you have a low warning level). However at work we treat all warnings as errors, so if we never used the suffix we'd all be very unhappy campers.
Well, that cast is perfectly fine because the value 1.0 is exactly representable in both float and double. Try something like this:
float a = 3.14159265358979323846;float b = 1.0 / 3.0;
The compiler should warn you about truncation from double to float, because double is of a higher precision than float.
Would you mind saying which flags you are passing to the compiler(gcc or g++) which will generate warnings here? You are not using something like -Wconversion are you?
-Wpedantic and -Wpedantic-errors (I think) should scream at you every time you make even the tiniest slip.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Quote:Original post by Sc4Freak But because it's implicit, it should generate a warning (because it may not be what you intended).
I don't know about GCC, but Visual Studio 2008 issues that warning on the default warning level.
It may well do, but then I do not think MSCV is a good compiler to base the ideas of warning on. If you consider the following is incorrect I suggest you file a bug report.
Quote: dmail@x1-6-00-11-2f-24-88-81:~> cd gcc_warning dmail@x1-6-00-11-2f-24-88-81:~/gcc_warning> cat float_double.c int main() { float f = 1.0/3.0; float f1 = 56355465468487.3; return 0; } dmail@x1-6-00-11-2f-24-88-81:~/gcc_warning> gcc -Wall -pedantic -pedantic-errors -Wextra -ansi float_double.c float_double.c: In function ‘main’: float_double.c:4: warning: unused variable ‘f1’ float_double.c:3: warning: unused variable ‘f’ dmail@x1-6-00-11-2f-24-88-81:~/gcc_warning>
Neither of your tests will cause an obvious problem with coercing them to floats from doubles. Try something like 1e50 which is out of float's range, and you'll get a warning (unless you're being stupid and not runnning with a high warning level).
Quote:Original post by ApochPiQ Neither of your tests will cause an obvious problem with coercing them to floats from doubles. Try something like 1e50 which is out of float's range, and you'll get a warning (unless you're being stupid and not runnning with a high warning level).
Quote: dmail@x1-6-00-11-2f-24-88-81:~/gcc_warning> gcc -ansi -pedantic -pedantic-errors -Wall -Wextra float_double.c float_double.c: In function ‘main’: float_double.c:5: warning: unused variable ‘f2’ float_double.c:4: warning: unused variable ‘f1’ float_double.c:3: warning: unused variable ‘f’ dmail@x1-6-00-11-2f-24-88-81:~/gcc_warning> cat float_double.c int main() { float f = 1.0/3.0; float f1 = 56355465468487.3; float f2 = 1e50; return 0; } dmail@x1-6-00-11-2f-24-88-81:~/gcc_warning>
Why would I be being stupid? you can see the error and warning flags I am passing to the compiler.
Quote:Original post by dmail It may well do, but then I do not think MSCV is a good compiler to base the ideas of warning on.
If MSVC warns about truncation where gcc would silently ignore it, then I seriously beg to differ. Talk about hard-to-find bugs. Why is more warnings a bad thing?