小编eld*_*jon的帖子

在 C++ 中丢失浮点数(双精度)的精度

我正在尝试为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)

c++ floating-point precision double

1
推荐指数
1
解决办法
1349
查看次数

标签 统计

c++ ×1

double ×1

floating-point ×1

precision ×1