3

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

Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
user541747
  • 73
  • 1
  • 4
  • No semantic difference (well, there is the difference of one extra byte in your source code :p). But, slightly related to this, there IS a difference between +0.0 and -0.0. Have fun! – e.tadeu Dec 14 '10 at 16:55

5 Answers5

7

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

Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
1

No, there is not.

ltjax
  • 15,837
  • 3
  • 39
  • 62
  • Yes there is. Type cout << typeid(0).name() << " " << typeid(0.0).name(); (Removed the downvote, I will downvote later if I am correct) – the_drow Dec 14 '10 at 09:58
  • 3
    `typeid(0).name()` is using an int - note the lack of a trailing decimal place. Try `cout << typeid(0.).name() << " " << typeid(0.0).name();` and you'll see they're the same. – AgentConundrum Dec 14 '10 at 10:06
1

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

Septagram
  • 9,425
  • 13
  • 50
  • 81
1

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."

AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
-3

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
the_drow
  • 18,571
  • 25
  • 126
  • 193