"1" vs "1.0f" - makes a difference?

Started by
31 comments, last by Cromulent 15 years, 10 months ago
Quote:Original post by Sc4Freak
Quote:Original post by lexs
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.


GCC 4.1.2 doesn't warn about:
*** Source Snippet Removed ***
Using -Wall.

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.


Interesting it doesn't.
Advertisement
Quote:Original post by Sc4Freak
Quote:Original post by lexs
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.


GCC 4.1.2 doesn't warn about:
*** Source Snippet Removed ***
Using -Wall.

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 Wyrframe
Quote:Original post by MikeTacular
Actually, neither floats nor doubles can exactly represent 1.0.

IEEE 754. Read it.

I stand corrected. Thanks. Forgot you could represent 1 as 20.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by Wyrframe
-Wpedantic and -Wpedantic-errors (I think) should scream at you every time you make even the tiniest slip.
It is not a slip though, you want to truncate a double to a float.

Quote:Original post by dmail
Quote:Original post by Wyrframe
-Wpedantic and -Wpedantic-errors (I think) should scream at you every time you make even the tiniest slip.
It is not a slip though, you want to truncate a double to a float.

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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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).

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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?

This topic is closed to new replies.

Advertisement