如何将 float 128 的所有小数打印到控制台

问题描述 投票:0回答:1

我想打印常量 e,但我很快意识到,cout 无法处理 __float128。由于该函数无法打印 __float 128 。我还尝试了一些可以在网上找到的解决方案,它们都告诉我将其转换为双精度。但这会失去计算的精度。

我使用的是 GCC x64。

c++ floating-point
1个回答
0
投票

C++23 将 to_chars API 更改为:

std::to_chars - cppreference.com

std::to_chars_result  
    to_chars( char* first, char* last, /* floating-point-type */ value,  
              std::chars_format fmt, int precision );

(C++23 起)

看来有办法。

    __float128 x = 1.123456789012345678901l;
    char buffer[256];
    std::to_chars(std::begin(buffer), std::end(buffer), x);
    std::cout << buffer;

https://godbolt.org/z/sKdKxYKdG

但首先我会在使用之前对其进行公平的测试。

© www.soinside.com 2019 - 2024. All rights reserved.