问题
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