This question already has an answer here:
这个问题已经有了答案:
- C++ difference between 0 and 0.0 3 answers
- C+ 0和0。03的差
- C++: difference between 0. and 0.0? 5 answers
- c++:0之间的区别。和0.0吗?5个回答
I just saw this for the first time. The source code I'm looking at is in C
我是第一次看到这个。我正在查看的源代码在C语言中
if( rate < 0.){
...
}
else{
...
}
What happens if rate=0
?
如果速率为0会怎样?
3 个解决方案
#1
16
0.
is a literal of type double
(and value zero). By contrast, 0
is a literal of type int
.
0。是double类型的文字(值为0)。相比之下,0是int类型的文字。
#2
6
It is interpreting 0.
as a double (0.0
) instead of an integer (0
).
这是解释0。作为一个double(0.0)而不是整数(0)。
Check the link: of "working code", showing the different sizes of various types of zero constants:
查看“工作代码”的链接,显示各种类型的零常量的不同大小:
#3
2
0.
is a floating constant and since it does not have a suffix it is a double
, from the draft C99 standard section 6.4.4.2
Floating constants we have the following grammar:
0。是一个浮动常数,由于没有后缀,所以是双精度浮点数,从C99标准章节6.4.4.2浮动常数的草稿来看,我们有以下语法:
floating-constant: decimal-floating-constant hexadecimal-floating-constant decimal-floating-constant: fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt [...] fractional-constant: digit-sequenceopt . digit-sequence digit-sequence . < ---- This covers 0. [...]
We then have in paragraph 4:
然后,我们在第4段中:
An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.
无后缀的浮动常数类型为double。如果后缀是f或f,它有类型float。如果后缀是字母l或l,那么它的类型是long double。
#1
16
0.
is a literal of type double
(and value zero). By contrast, 0
is a literal of type int
.
0。是double类型的文字(值为0)。相比之下,0是int类型的文字。
#2
6
It is interpreting 0.
as a double (0.0
) instead of an integer (0
).
这是解释0。作为一个double(0.0)而不是整数(0)。
Check the link: of "working code", showing the different sizes of various types of zero constants:
查看“工作代码”的链接,显示各种类型的零常量的不同大小:
#3
2
0.
is a floating constant and since it does not have a suffix it is a double
, from the draft C99 standard section 6.4.4.2
Floating constants we have the following grammar:
0。是一个浮动常数,由于没有后缀,所以是双精度浮点数,从C99标准章节6.4.4.2浮动常数的草稿来看,我们有以下语法:
floating-constant: decimal-floating-constant hexadecimal-floating-constant decimal-floating-constant: fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt [...] fractional-constant: digit-sequenceopt . digit-sequence digit-sequence . < ---- This covers 0. [...]
We then have in paragraph 4:
然后,我们在第4段中:
An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.
无后缀的浮动常数类型为double。如果后缀是f或f,它有类型float。如果后缀是字母l或l,那么它的类型是long double。