浮点值后缀“f”有什么用

发布于 2024-10-18 05:17:43 字数 169 浏览 0 评论 0原文

我想知道 C: 中这两个变量之间有什么区别

float price = 3.00;

float price = 3.00f;

在这种情况下后缀 f 有什么用?

I am wondering what the difference is between these two variables in C:

float price = 3.00;

and

float price = 3.00f;

What is the use of suffix f in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

絕版丫頭 2024-10-25 05:17:43

3.00 被解释为 double,而 3.00f 被编译器视为 float

f 后缀只是告诉编译器哪个是 float 哪个是 double

请参阅 MSDN (C++)

3.00 is interpreted as a double, as opposed to 3.00f which is seen by the compiler as a float.

The f suffix simply tells the compiler which is a float and which is a double.

See MSDN (C++)

花伊自在美 2024-10-25 05:17:43

除了已经说过的内容之外,跟踪 1.0 与 1.0f 比许多人意识到的更重要。如果你编写这样的代码:

float x;
...
float y = x * 2.0;

那么 x 将被提升为双精度型,因为 2.0 是双精度型。不允许编译器优化该提升,否则会违反 C 标准。计算以双精度进行,然后结果被隐式截断为浮点型。这意味着计算将比您编写 2.0f 或 2 时慢(尽管更准确)。

如果您编写 2,常量将是 int 类型,它将被提升为 float,并且计算将以“浮点精度”完成。一个好的编译器会警告您有关此促销的信息。

在此处阅读有关“常用算术转换”规则的更多信息:

http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx

In addition to what has already been said, keeping track of 1.0 versus 1.0f is more important than many people realize. If you write code like this:

float x;
...
float y = x * 2.0;

Then x will be promoted to a double, because 2.0 is a double. The compiler is not allowed to optimize that promotion away or it would violate the C standard. The calculation takes place with double precision, and then the result is then implicitly truncated into a float. This means that the calculation will be slower (though more accurate) than it would have been if you had written 2.0f or 2.

Had you written 2, the constant would be of int type, which would be promoted to a float, and the calculation would have been done with "float precision". A good compiler would warn you about this promotion.

Read more about the "usual arithmetic conversion" rules here:

http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx

话少情深 2024-10-25 05:17:43

因为无后缀的浮点文字是双精度数,而舍入意味着即使很小的文字在舍入为浮点数和双精度数时也可以采用不同的值。这可以在以下示例中观察到:

float f=0.67;
if(f == 0.67) 
  printf("yes");
else 
  printf("no");  

这将输出 no,因为 0.67 在舍入为 float 时的值与舍入为 double 时的值不同。另一方面:

float f=0.67;
if(f == 0.67f) 
  printf("yes");
else 
  printf("no"); 

输出yes

可以使用大写或小写字母指定后缀。

也试试这个:

printf(" %u %u\n", sizeof(.67f), sizeof(.67));

检查@codepade

Because by unsuffixed floating-point literals are doubles, and rounding means that even small literals can take on different values when rounded to float and double. This can be observed in the following example:

float f=0.67;
if(f == 0.67) 
  printf("yes");
else 
  printf("no");  

This will output no, because 0.67 has a different value when rounded to float than it does when rounded to double. On the other hand:

float f=0.67;
if(f == 0.67f) 
  printf("yes");
else 
  printf("no"); 

outputs yes.

The suffix can be specified using either upper or lowercase letters.

Try this also:

printf(" %u %u\n", sizeof(.67f), sizeof(.67));

Check @codepade

我不是你的备胎 2024-10-25 05:17:43

3.00 是双精度型,3.00f 是浮点数。

3.00 is a double, 3.00f is a float.

ぺ禁宫浮华殁 2024-10-25 05:17:43

添加了一些浮点和双精度数据类型之间的比较组合。

int main()
{
    // Double type constant(3.14) converts to Float type by 
    // truncating it's bits representation 
    float a = 3.14; 
    // Problem: float type 'a' promotes to double type and the value 
    // of 'a'  depends on how many bits added to represent it.
    if(a == 3.14)   
        std::cout<<"a: Equal"<<std::endl;
    else
        std::cout<<"a: Not Equal"<<std::endl; 

    float b = 3.14f; // No type conversion
    if(b == 3.14)    // Problem: Float to Double conversion
        std::cout<<"b: Equal"<<std::endl;
    else
        std::cout<<"b: Not Equal"<<std::endl;

    float c = 3.14; // Double to Float conversion (OK even though is not a good practice )
    if(c == 3.14f)  // No type conversion 
        std::cout<<"c: Equal"<<std::endl;  // OK
    else
        std::cout<<"c: Not Equal"<<std::endl;

    float d = 3.14f;
    if(d == 3.14f)
        std::cout<<"d: Equal"<<std::endl; // OK
    else
        std::cout<<"d: Not Equal"<<std::endl;

    return 0;
}    

输出:

 a: Not Equal
 b: Not Equal
 c: Equal
 d: Equal

Adding few more combination of comparisons between float and double data types.

int main()
{
    // Double type constant(3.14) converts to Float type by 
    // truncating it's bits representation 
    float a = 3.14; 
    // Problem: float type 'a' promotes to double type and the value 
    // of 'a'  depends on how many bits added to represent it.
    if(a == 3.14)   
        std::cout<<"a: Equal"<<std::endl;
    else
        std::cout<<"a: Not Equal"<<std::endl; 

    float b = 3.14f; // No type conversion
    if(b == 3.14)    // Problem: Float to Double conversion
        std::cout<<"b: Equal"<<std::endl;
    else
        std::cout<<"b: Not Equal"<<std::endl;

    float c = 3.14; // Double to Float conversion (OK even though is not a good practice )
    if(c == 3.14f)  // No type conversion 
        std::cout<<"c: Equal"<<std::endl;  // OK
    else
        std::cout<<"c: Not Equal"<<std::endl;

    float d = 3.14f;
    if(d == 3.14f)
        std::cout<<"d: Equal"<<std::endl; // OK
    else
        std::cout<<"d: Not Equal"<<std::endl;

    return 0;
}    

Output:

 a: Not Equal
 b: Not Equal
 c: Equal
 d: Equal
此岸叶落 2024-10-25 05:17:43

这是因为 a 的默认类型
浮点数字文字 -
字符 3.00 是双精度而不是浮点数。
要进行编译,您必须添加
后缀 f(或 F)。

That's because the default type of a
floating point numeric literal - the
characters 3.00 is double not float.
To make this compile you have to add
the suffix f (or F).

心如荒岛 2024-10-25 05:17:43

通常差异并不重要,因为编译器无论如何都会将双精度常量转换为浮点型。但是,请考虑一下:

template<class T> T min(T a, T b)
{
  return (a < b) ? a : b;
}

float x = min(3.0f, 2.0f); // will compile
x = min(3.0f, 2);   // compiler cannot deduce T type
x = min(3.0f, 2.0); // compiler cannot deduce T type

Often the difference isn't important, as the compiler will convert the double constant into a float anyway. However, consider this:

template<class T> T min(T a, T b)
{
  return (a < b) ? a : b;
}

float x = min(3.0f, 2.0f); // will compile
x = min(3.0f, 2);   // compiler cannot deduce T type
x = min(3.0f, 2.0); // compiler cannot deduce T type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文