在C ++中全精度显示浮点数? [英] Full precision display of floating point numbers in C++?

查看:141
本文介绍了在C ++中全精度显示浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关在C ++中显示浮点数的主题,但找不到令人满意的答案.

I have read several topics about the display of floating point numbers display in C++ and I couldn't find a satisfying answer.

我的问题是: 如何以科学格式(尾数/指数)在C ++中显示浮点数的所有有效数字?

问题在于,所有数字在基数10中的有效位数都不相同.

The problem is that all numbers do not have the same number of significant digits in base 10.

例如,double具有 15至17 个有效的十进制数字精度,但是std::numeric_limits<double>::digits10返回15,因此,对于某些数字,我将失去2个额外的精度十进制数字.

For example a double has 15 to 17 significant decimal digits precision, but std::numeric_limits<double>::digits10 returns 15 and consequently, for some numbers I will loose 2 extra decimal digits of precision.

推荐答案

std::numeric_limits<double>::digits10提供可以安全地恢复的小数位数,即,可以保留往返十进制数的小数位数-> double->十进制.假设更多的十进制数字正确无济于事.如果您想简化往返double-> decimal-> double的操作,则可以使用std::numeric_limits<double>::max_digits10.

The value std::numeric_limits<double>::digits10 provides the number of decimal digits which can safely be restored, i.e., the number of decimal digits which survive a round-trip decimal->double->decimal. Assuming more decimal digits are correct isn't helpful. If you want to faciliate the round-trip double->decimal->double you'd use std::numeric_limits<double>::max_digits10.

如果要使用确切的值,可以使用std::numeric_limits<double>::digits,但是它将以有趣的方式显示从十进制值转换的数字,因为它们通常是四舍五入的值.这也是max_digits10在显示供人类消费的数字时不起作用的原因:最后两位数字通常不是人类读者期望的数字.

If you want the exact values you'd use std::numeric_limits<double>::digits but it will display numbers converted from decimal values in a funny way as they are normally rounded values. This is also the reason why max_digits10 isn't useful when presenting numbers for human consumption: the last couple of digits are normally not those expect by the human reader.

这篇关于在C ++中全精度显示浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆