Trong một nỗ lực để xác minh (sử dụng VS2012) tuyên bố (câu 2) của một cuốn sách màchuyển đổi ngầm từ lâu lâu nổi lãi suất bất ngờ kết quả
When we assign an integral value to an object of floating-point type, the fractional part is zero.
Precision may be lost if the integer has more bits than the floating-point object can accommodate.
tôi viết prog wee sau:
#include <iostream>
#include <iomanip>
using std::cout;
using std::setprecision;
int main()
{
long long i = 4611686018427387905; // 2^62 + 2^0
float f = i;
std::streamsize prec = cout.precision();
cout << i << " " << setprecision(20) << f << setprecision(prec) << std::endl;
return 0;
}
Đầu ra là
4611686018427387905 4611686018427387900
tôi mong đợi đầu ra có dạng
4611686018427387905 4611690000000000000
Làm cách nào một phao 4 byte có thể giữ lại quá nhiều thông tin về số nguyên 8 byte? Có giá trị nào cho tôi thực sự thể hiện khiếu nại không?