In C++ float value being truncated from double

后端 未结 4 970
南旧
南旧 2021-01-27 22:59

I\'ve coded using float variables before and never had this problem.

float  a, b, subtotal, stx;
a=15.95;
b=24.95;
subtotal=a+b;
stx=subtotal*.07;

cout <         


        
4条回答
  •  Happy的楠姐
    2021-01-27 23:33

    why is the compiler interpreting the literal values as as doubles

    Because that's how literals are interpreted, unless you add modifiers to specify a different type.

    a=15.95f;
           ^ gives the literal "float" type
    

    But if variables are declared as float...

    The type of an expression never depends on how the expression is used; so 15.95 has type double whatever you do with it. The type is converted for use in a larger expression, if necessary, and that's what gives the warning in this case.

提交回复
热议问题