Yel*_*aYR 0 c++ oop double warnings truncation
我之前使用浮点变量编码,从来没有遇到过这个问题.
float a, b, subtotal, stx;
a=15.95;
b=24.95;
subtotal=a+b;
stx=subtotal*.07;
cout << "Item 1: $" << a << endl;
cout << "Item 2: $" << b << endl;
cout << "\nSubtotal: $" <<subtotal<< endl;
cout << "Sales Tax: $" << stx << endl;
cout << "Total: $" << subtotal+stx << endl;
Run Code Online (Sandbox Code Playgroud)
比较严格的前瞻性代码
warning C4305: '=' : truncation from 'double' to 'float'
Run Code Online (Sandbox Code Playgroud)
我理解数据被截断的想法(我也知道你可以f在变量的末尾写入.但是如果变量被声明为float,为什么编译器将文字值解释为双精度,如果它被声明为浮点数.
我查了几张其他的门票而且他们不同于我的询问我似乎无法找到一个解决方案,为什么数据被读作双重如果它被声明为浮点数.
为什么编译器将文字值解释为双精度值
因为这是解释文字的方式,除非您添加修饰符以指定其他类型.
a=15.95f;
^ gives the literal "float" type
Run Code Online (Sandbox Code Playgroud)
但是如果变量被声明为float ...
表达式的类型决不会取决于表达式的使用方式; 所以15.95有类型double不管你用它做什么.如果需要,转换类型以用于更大的表达式,这就是在这种情况下给出警告的内容.