jhjhjh
jhjhjh

Reputation: 43

What purpose does the dot( . ) fulfil in this piece of code

In an example that's in my C++ book, I've found this piece of code at the end of an example problem, which verified if 3 integers were in an arithmetic progression.

if (b==(a+c)/2.)

I don't think I've seen the dot after 2 ever used in such a way and I don't know what it's purpose is here.

Upvotes: 4

Views: 84

Answers (1)

bolov
bolov

Reputation: 75727

2. is a double literal. It's the same as 2.0.

Integer division is different than floating point division, so in some cases having a double instead of an int makes an important difference.


Although this form is perfectly valid, for readability purposes often 2.0 is preferred. In some (not all) newer languages derived from C++ like C# it is not allowed, i.e. you are forced to write 2.0.

Upvotes: 5

Related Questions