In C++ float value being truncated from double

后端 未结 4 982
南旧
南旧 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条回答
  •  梦谈多话
    2021-01-27 23:20

    The processes the string tokens that make up your code - it sees is "2.7" or 15.95, which is numeric (starts with a digit) and floating point (has a .) The default container for floating point numbers is a double (otherwise you'd get really erroneous outcomes for most of the hard coded numbers you enter. After the value of the expression is evaluated (in this case, just the value itself), it's assigned to a float value, which isn't precise enough to store the result, hence the warning.

提交回复
热议问题