views:

241

answers:

5

For a number of reasons, I have to use floats in my code instead of doubles. To use a literal in my code, I have to write something like:

float f = 0.75F;

or the compiler will barf since it treats just "0.75" as a double. Is there anything I can put in my code or set in Visual Studio that will make it treat a literal like "0.75" as a float without having to append an "F" every time?

+14  A: 

No - fortunately, IMO. Literals are treated the same way everywhere.

This is a good thing - imagine some maintenance developer comes and looks at your code in a year's time. He sees "0.75" and thinks "I know C# - that's a double! Hang on, how is it being assigned to a float variable?" Ick.

Is it really so painful to add the "F" everywhere? Do you really have that many constants? Could you extract them as constant values, so all your "F-suffixed" literals are in the same place.

Jon Skeet
As I mentioned in my comment to the original question, I agree; limit your literals and extract them as constants for maximum maintainability.
Randolpho
It's not slit-my-wrists painful, but it is annoying. What I'm doing is largely experimental and involves tweaking parameter values and testing the results. Sometimes there are lots of these in a formula, so there are lots of Fs to forget to add.
MusiGenesis
Then use doubles during your tests and switch to floats later.
Randolpho
+1: the maintenance perspective is not to be underestimated.
Fredrik Mörk
Consider reading them from a config file. That makes tweaking the algorithms easier, and you can omit as many f's as you like.
peterchen
@peterchen: I'm not only tweaking the values fed into the algorithms - I'm also tweaking and changing the algorithms themselves. Feeding them from a config file would be an unnecessary extra step at this stage.
MusiGenesis
+1  A: 

The language interprets floating point precision literals as doubles everywhere. This is not a configurable feature of the compiler - and with good reason.

Configuring how the language interprets you code would lead to problems with both compatibility and the ability of maintenance developers to understand what the code means.

While not advisable generally, you can reduce the pain a little in C# 3 by using:

var f = 0.75F;

Just be careful, because forgetting the 'F' suffix with this syntax WILL cause the compiler to create a double, not a float.

LBushkin
This doesn't reduce that pain at all. I still have to type the F.
MusiGenesis
This is one place where I **wouldn't** use the var syntax for just the reason you described. On the other hand, I would use doubles, not floats...
tvanfosson
Some languages allow redefining how the code is interpreted using macros, like C for example. It was not included in C# because it's not that very useful and it's so easy to make a mess out of it. :)
Guffa
The `var` approach feels a bit dangerous here. `float f = 0.75;` will at least cause the compiler to react; feels like a nice safety net.
Fredrik Mörk
+1  A: 

FYI -- you can find all of the compiler options for C# at http://msdn.microsoft.com/en-us/library/6ds95cz0.aspx. If you check there, you'll see that there isn't any option that allows this -- and rightly so for the reasons that @Jon Skeet noted.

tvanfosson
A: 

Float comes with an F :-)

chikak
A: 

I would advise you to always use

var meaning = 1f;

because the "var" keyword saves a lot of human interpretation and maintenance time.

Jader Dias