In some C# code I have seen staments like this:
在一些C#代码中,我看过这样的staments:
float someFloat = 57f;
I want to know why we should use literals like f
in the above case?.
我想知道为什么我们应该在上面的例子中使用像f这样的文字?
7 个解决方案
#1
The "f" above is a type suffix. This tells the compiler the exact type of the literal provided. This is used so the compiler can allocate the appropriate amount of storage (precision) for the literal. By default, floating point literals are given storage for a "double." If you add "f" as a suffix, the literal will only get the storage for a float, which will have less accuracy.
上面的“f”是类型后缀。这告诉编译器提供的文字的确切类型。这是使用的,因此编译器可以为文字分配适当的存储量(精度)。默认情况下,浮点文字的存储空间为“double”。如果添加“f”作为后缀,则文字只会获得浮点数的存储空间,而精度较低。
double d = 50.1234; // given double storage
double d = 50.1234f; // given float storage, which might lose precision compared to double
#2
Mainly so the compiler knows exactly what we mean - in particular for overload resolution:
主要是因此编译器确切地知道我们的意思 - 特别是对于重载决策:
Foo(57f);
should that call Foo(int)
/ Foo(float)
/ Foo(decimal)
?
应该调用Foo(int)/ Foo(float)/ Foo(十进制)?
Actually, I don't like remembering things - an alternative is:
实际上,我不喜欢记住事物 - 另一种选择是:
float someFloat = (float)57;
this is not a runtime cast - it is identical (at the IL level) to 57f
. The only time it is subtly different is with decimal
s with extra precision:
这不是运行时强制转换 - 它与57f相同(在IL级别)。唯一不同的是带有额外精度的小数:
decimal someDecimal = (decimal)57.0; // same as 57M, not 57.0M (scale is different)
#3
By default, a real numeric literal on the right-hand side of the assignment operator is treated as double. Therefore, to initialize a float variable use the suffix f or F, for example:
默认情况下,赋值运算符右侧的实数数字文字被视为double。因此,要初始化float变量,请使用后缀f或F,例如:
float x = 3.5F;
If you don't use the suffix in the previous declaration, you will get a compilation error because you are attempting to store a double value into a float variable.
如果您不在前一个声明中使用后缀,则会出现编译错误,因为您试图将double值存储到float变量中。
From MSDN: float
来自MSDN:float
#4
Check out this article which explains a lot about Numeric Literals in C#: http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx
看看这篇文章解释了很多关于C#中的数字文字:http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx
Here is a little excerpt from the article:
以下是文章的一些摘录:
float unitPrice = 123.45; // This will not compile
float unitPrice = 123.45; //这不会编译
The problem here is that you cannot always implicitly convert a double (123.45) into a float. The C# compiler understands this and stops the code from being compiled
这里的问题是你不能总是隐式地将double(123.45)转换为float。 C#编译器理解这一点并停止编译代码
#5
why we should use literals?
为什么我们应该使用文字?
int / int = int
but int / float = float
:
int / int = int但int / float = float:
int timespanInMS = 500;
Console.WriteLine("Execution time: {0} sec.", timespanInMS / 1000);
Console.WriteLine("Execution time: {0} sec.", timespanInMS / 1000F);
// Result:
// Execution time: 0 sec.
// Execution time: 0.5 sec.
#6
I want to know why we should use literals like f in the above case?.
我想知道为什么我们应该在上面的例子中使用像f这样的文字?
You shouldn't. Well, you don't have to.
你不应该。好吧,你不必。
The above case shows a float initialization to 57f. Integers are implicitly converted to floats compile time so you can easily strip away that f suffix if that bothers you. However, for consistency in written code, I always try to add the suffix. Note that this only applies to rounded numbers (integers).
上面的例子显示浮点数初始化为57f。整数被隐式转换为浮点数编译时间,因此如果困扰你,你可以轻松地去掉那个f后缀。但是,为了保证书面代码的一致性,我总是尝试添加后缀。请注意,这仅适用于舍入数字(整数)。
See Msdn on Implicit and Explicit number conversations.
有关隐式和显式数字对话的信息,请参阅Msdn。
#7
Because 57 is an integer and 57.0 is a double. You're trying to get a float, which is a single precision number. That begs the question, why are you using a single precision float?
因为57是整数,57.0是双精度。你试图得到一个浮点数,这是一个单精度数。这引出了一个问题,你为什么要使用单精度浮子?
See http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx for some more on C# literals.
有关C#文字的更多信息,请参见http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx。
#1
The "f" above is a type suffix. This tells the compiler the exact type of the literal provided. This is used so the compiler can allocate the appropriate amount of storage (precision) for the literal. By default, floating point literals are given storage for a "double." If you add "f" as a suffix, the literal will only get the storage for a float, which will have less accuracy.
上面的“f”是类型后缀。这告诉编译器提供的文字的确切类型。这是使用的,因此编译器可以为文字分配适当的存储量(精度)。默认情况下,浮点文字的存储空间为“double”。如果添加“f”作为后缀,则文字只会获得浮点数的存储空间,而精度较低。
double d = 50.1234; // given double storage
double d = 50.1234f; // given float storage, which might lose precision compared to double
#2
Mainly so the compiler knows exactly what we mean - in particular for overload resolution:
主要是因此编译器确切地知道我们的意思 - 特别是对于重载决策:
Foo(57f);
should that call Foo(int)
/ Foo(float)
/ Foo(decimal)
?
应该调用Foo(int)/ Foo(float)/ Foo(十进制)?
Actually, I don't like remembering things - an alternative is:
实际上,我不喜欢记住事物 - 另一种选择是:
float someFloat = (float)57;
this is not a runtime cast - it is identical (at the IL level) to 57f
. The only time it is subtly different is with decimal
s with extra precision:
这不是运行时强制转换 - 它与57f相同(在IL级别)。唯一不同的是带有额外精度的小数:
decimal someDecimal = (decimal)57.0; // same as 57M, not 57.0M (scale is different)
#3
By default, a real numeric literal on the right-hand side of the assignment operator is treated as double. Therefore, to initialize a float variable use the suffix f or F, for example:
默认情况下,赋值运算符右侧的实数数字文字被视为double。因此,要初始化float变量,请使用后缀f或F,例如:
float x = 3.5F;
If you don't use the suffix in the previous declaration, you will get a compilation error because you are attempting to store a double value into a float variable.
如果您不在前一个声明中使用后缀,则会出现编译错误,因为您试图将double值存储到float变量中。
From MSDN: float
来自MSDN:float
#4
Check out this article which explains a lot about Numeric Literals in C#: http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx
看看这篇文章解释了很多关于C#中的数字文字:http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx
Here is a little excerpt from the article:
以下是文章的一些摘录:
float unitPrice = 123.45; // This will not compile
float unitPrice = 123.45; //这不会编译
The problem here is that you cannot always implicitly convert a double (123.45) into a float. The C# compiler understands this and stops the code from being compiled
这里的问题是你不能总是隐式地将double(123.45)转换为float。 C#编译器理解这一点并停止编译代码
#5
why we should use literals?
为什么我们应该使用文字?
int / int = int
but int / float = float
:
int / int = int但int / float = float:
int timespanInMS = 500;
Console.WriteLine("Execution time: {0} sec.", timespanInMS / 1000);
Console.WriteLine("Execution time: {0} sec.", timespanInMS / 1000F);
// Result:
// Execution time: 0 sec.
// Execution time: 0.5 sec.
#6
I want to know why we should use literals like f in the above case?.
我想知道为什么我们应该在上面的例子中使用像f这样的文字?
You shouldn't. Well, you don't have to.
你不应该。好吧,你不必。
The above case shows a float initialization to 57f. Integers are implicitly converted to floats compile time so you can easily strip away that f suffix if that bothers you. However, for consistency in written code, I always try to add the suffix. Note that this only applies to rounded numbers (integers).
上面的例子显示浮点数初始化为57f。整数被隐式转换为浮点数编译时间,因此如果困扰你,你可以轻松地去掉那个f后缀。但是,为了保证书面代码的一致性,我总是尝试添加后缀。请注意,这仅适用于舍入数字(整数)。
See Msdn on Implicit and Explicit number conversations.
有关隐式和显式数字对话的信息,请参阅Msdn。
#7
Because 57 is an integer and 57.0 is a double. You're trying to get a float, which is a single precision number. That begs the question, why are you using a single precision float?
因为57是整数,57.0是双精度。你试图得到一个浮点数,这是一个单精度数。这引出了一个问题,你为什么要使用单精度浮子?
See http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx for some more on C# literals.
有关C#文字的更多信息,请参见http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx。