C++: difference between 0. and 0.0?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 12:53:13

问题


I am well aware of the difference between 0 and 0.0 (int and double).

But is there any difference between 0. and 0.0 ( please note the . )?

Thanks a lot in advance,

Axel


回答1:


There is no difference. Both literals are double. From the C++-Grammar:

fractional-constant:
    digit-sequenceopt . digit-sequence
    digit-sequence .

See: Hyperlinked C++ BNF Grammar




回答2:


No, there is not.




回答3:


No. You can also write .0 as far as I know.




回答4:


Just having the . as part of the number identifies it as a floating point type.

This:

 cout << (5 / 2) << endl;
 cout << (5. / 2) << endl;
 cout << (5.0 / 2) << endl;

Prints this:

 2
 2.5
 2.5

You can see that the first line uses integer division (because both values are integers), whereas 5. and 5.0 both get identified as floating point types, and so they trigger "normal division."




回答5:


0 is of type int but can be casted to double and 0.0 is of type double but can be casted to int.
Both casts are implicit.



来源:https://stackoverflow.com/questions/4437852/c-difference-between-0-and-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!