MusiGenesis
MusiGenesis

Reputation: 75386

Looking for an easier way to use floats in C#

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?

Upvotes: 3

Views: 716

Answers (6)

supercat
supercat

Reputation: 81277

The proper behavior would not be for a compiler to interpret non-suffixed literals as single-precision floats, but rather to recognize that conversions from double to float should be regarded as widening conversions since, for every double value, there is either precisely one unambiguously-correct float representation, or (in a few rare edge cases) there will be precisely two equally-good values, neither of which will be more than a part per quadrillion from being the unambiguously-correct value. Semantically, conversions from float to double should be regarded as narrowing conversions (since they require that the compiler "guess" at information it doesn't have), but the practical difficulties that would cause might justify making conversions in that direction 'widening'.

Perhaps one should petition Microsoft to add a widening conversion from double to float? There's no good reason why code which calculates graphics coordinates as double should be cluttered with typecasts when calling drawing functions.

Upvotes: 0

Jader Dias
Jader Dias

Reputation: 90583

I would advise you to always use

var meaning = 1f;

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

Upvotes: 0

LBushkin
LBushkin

Reputation: 131806

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.

Upvotes: 1

chikak
chikak

Reputation: 1732

Float comes with an F :-)

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532655

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503280

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.

Upvotes: 14

Related Questions