我正在尝试为double变量分配一个大值并将其打印在控制台上。我提供的数字与显示为输出的数字不同。是否有可能在double不损失精度的情况下正确分配和输出值?这是C++代码:
#include <iostream>
#include <limits>
int main( int argc, char *argv[] ) {
// turn off scientific notation on floating point numbers
std::cout << std::fixed << std::setprecision( 3 );
// maximum double value on my machine
std::cout << std::numeric_limits<double>::max() << std::endl;
// string representation of the double value I want to get
std::cout << "123456789123456789123456789123456789.01" << std::endl;
// value I supplied
double d = 123456789123456789123456789123456789.01;
// it's printing 123456789123456784102659645885120512.000 instead of 123456789123456789123456789123456789.01
std::cout << d << …Run Code Online (Sandbox Code Playgroud)