Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without the <code>.f</code> the number gets interpreted as an integer, hence <code>1/3</code> is <code>(int)1/(int)3</code> => <code>(int)0</code> instead of the desired <code>(float)0.333333</code>. The <code>.f</code> tells the compiler to interpret the literal as a floating point number of type float. There are other such constructs such as for example <code>0UL</code> which means a <code>(unsigned long)0</code>, whereas a plain <code>0</code> would be an <code>(int)0</code>.</p> <p>The <code>.f</code> is actually two components, the <code>.</code> which indicates that the literal is a floating point number rather than an integer, and the <code>f</code> suffix which tells the compiler the literal should be of type float rather than the default double type used for floating point literals.</p> <p>Disclaimer; the "cast construct" used in the above explanation is not an actual cast, but just a way to indicate the type of the literal.</p> <p>If you want to know all about literals and the suffixes you can use in them, you can read the C++ standard, (<a href="http://open-std.org/jtc1/sc22/open/n2356/lex.html#lex.literal" rel="nofollow noreferrer">1997 draft</a>, <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3337.pdf#page=37" rel="nofollow noreferrer">C++11 draft</a>, <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3797.pdf#page=37" rel="nofollow noreferrer">C++14 draft</a>, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf#page=43" rel="nofollow noreferrer">C++17 draft</a>) or alternatively, have a look at a decent textbook, such as Stroustrup's <a href="http://www.stroustrup.com/4th.html" rel="nofollow noreferrer">The C++ Programming Language</a>.</p> <p>As an aside, in your example <code>(float)1/3</code> the literals <code>1</code> and <code>3</code> are actually integers, but the 1 is first cast to a float by your cast, then subsequently the 3 gets implicitly cast to a float because it is a righthand operand of a floating point operator. (The operator is floating point because its lefthand operand is floating point.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload