Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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.

like image 762
jhjhjh Avatar asked Oct 18 '25 13:10

jhjhjh


1 Answers

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.

like image 51
bolov Avatar answered Oct 21 '25 03:10

bolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!