【发布时间】:2015-02-20 17:55:08
【问题描述】:
我以前使用浮点变量进行编码,但从未遇到过这个问题。
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;
相对严格的前向代码
warning C4305: '=' : truncation from 'double' to 'float'
我理解数据被截断的想法(而且我也知道你可以在变量末尾写f。但是如果变量被声明为浮点数,为什么编译器会将文字值解释为双精度值它被声明为浮点数。
我查找了其他几张票,它们与我的查询不同,我似乎无法找到解决方案,说明如果数据声明为浮点数,为什么数据被读取为双精度数。
【问题讨论】:
-
15.95始终是double。总是。使用方式不会影响其类型。 -
为什么要标记为 OOP?
标签: c++ oop double warnings truncation