Search code examples
c++syntaxnumbers

Is .5 same as 0.5 in C++?


In a C++ file, I read a statement as follows:

return cos((x-centre)*2*M_PI*recipwidth)*.5+.5;

Is the .5 in the above statement same as 0.5?


Solution

  • Here's what the standard has to say about it in §2.14.4 [lex.fcon]/1 (emphasis mine):

    A floating literal consists of an integer part, a decimal point, a fraction part, an e or E, an optionally signed integer exponent, and an optional type suffix. The integer and fraction parts both consist of a sequence of decimal (base ten) digits. Either the integer part or the fraction part (not both) can be omitted; either the decimal point or the letter e (or E ) and the exponent (not both) can be omitted.

    So yes, 0.5 and .5 are equivalent.