简体   繁体   English

用 C++ 写一个全精度浮点数

[英]Write a float with full precision in C++

In C++, can I write and read back a float (or double) in text format without losing precision?在 C++ 中,我可以在不丢失精度的情况下以文本格式写入和读取浮点数(或双精度数)吗?

Consider the following:考虑以下:

float f = ...;
{
    std::ofstream fout("file.txt");
    // Set some flags on fout
    fout << f;
 }
 float f_read;
 {
     std::ifstream fin("file.txt");
     fin >> f;
  }
  if (f != f_read) {
      std::cout << "precision lost" << std::endl;
  }

I understand why precision is lost sometimes.我理解为什么有时会丢失精度。 However, if I print the value with enough digits, I should be able to read back the exact same value.但是,如果我用足够多的数字打印值,我应该能够读回完全相同的值。

Is there a given set of flags that is guaranteed to never lose precision?是否有一组给定的标志可以保证永远不会失去精度? Would this behaviour be portable across platforms?这种行为是否可以跨平台移植?

Have a look at this article: How to Print Floating-Point Numbers Accurately and also at that one: Printing Floating-Point Numbers Quickly and Accurately .看看这篇文章: How to Print Floating-Point Numbers Accurately and the one: Printing Floating-Point Numbers Quick and Accurately

It is also mentioned on stackoverflow here , and there is some pointer to an implementation here .此处的stackoverflow 上也提到了它,并且有一些指向此处的实现的指针。

If you don't need to support platforms that lack C99 support (MSVC), your best bet is actually to use the %a format-specifier with printf , which always generates an exact (hexadecimal) representation of the number while using a bounded number of digits.如果您不需要支持缺乏 C99 支持 (MSVC) 的平台,您最好的选择实际上是将%a格式说明符与printf ,它总是在使用有界数字时生成数字的精确(十六进制)表示的数字。 If you use this method, then no rounding occurs during the conversion to a string or back, so the rounding mode has no effect on the result.如果使用此方法,则在转换为字符串或返回的过程中不会发生舍入,因此舍入模式对结果没有影响。

if I print the value with enough digits, I should be able to read back the exact same value如果我用足够多的数字打印值,我应该能够读回完全相同的值

Not if you write it in decimal - there's not an integer relationship between the number of binary digits and the number of decimal digits required to represent a number.如果你用十进制写它就不是 - 二进制数字的数量和代表一个数字所需的十进制数字的数量之间没有整数关系。 If you print your number out in binary or hexadecimal, you'll be able to read it back without losing any precision.如果您以二进制或十六进制打印您的数字,您将能够在不丢失任何精度的情况下读取它。

In general, floating point numbers are not portable between platforms in the first place, so your text representation is not going to be able to bridge that gap.通常,浮点数首先不能在平台之间移植,因此您的文本表示将无法弥合这一差距。 In practice, most machines use IEEE 754 floating point numbers, so it'll probably work reasonably well.在实践中,大多数机器使用 IEEE 754 浮点数,所以它可能会工作得相当好。

You can't necessarily print the exact value of a "power of two" float in decimal.您不一定能以十进制打印“2 的幂”浮点数的确切值。
Think of using base three to store 1/3, now try and print 1/3 in decimal perfectly.考虑使用基数 3 来存储 1/3,现在尝试以十进制完美打印 1/3。

For solutions see: How do you print the EXACT value of a floating point number?有关解决方案,请参阅: 如何打印浮点数的 EXACT 值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM