实数 - 如何确定是否需要浮点或双? [英] Real numbers - how to determine whether float or double is required?

查看:125
本文介绍了实数 - 如何确定是否需要浮点或双?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我们可以检查是否浮动数据类型是足够的存储号码,或双击是必需的?

Given a real value, can we check if a float data type is enough to store the number, or a double is required?

我知道precision从结构变化到架构。是否有任何C / C ++函数来确定正确的数据类型?

I know precision varies from architecture to architecture. Is there any C/C++ function to determine the right data type?

推荐答案

有关的背景,看的什么每台计算机科学家应该知道关于浮点运算

不幸的是,我不认为有什么办法来自动决定。

Unfortunately, I don't think there is any way to automate the decision.

通常,当人们在浮点的,而不是作为字符串重新present号码,目的是使用数字做算术。即使所有的输入适合与接受precision一个给定的浮点类型,你还是要考虑舍入误差和中间结果。

Generally, when people represent numbers in floating point, rather than as strings, the intent is to do arithmetic using the numbers. Even if all the inputs fit in a given floating point type with acceptable precision, you still have to consider rounding error and intermediate results.

在实践中,大多数计算将有足够的precision为有用的结果工作,使用64位的类型。只使用32位计算的许多不会得到有用的结果。

In practice, most calculations will work with enough precision for usable results, using a 64 bit type. Many calculations will not get usable results using only 32 bits.

在现代处理器,总线和算术单元是足够宽以得到32位和64位浮点类似的性能。使用32位的主要动机是存储一个非常大的阵列时,以节省空间。

In modern processors, buses and arithmetic units are wide enough to give 32 bit and 64 bit floating point similar performance. The main motivation for using 32 bit is to save space when storing a very large array.

这导致了以下策略:

如果阵列是大到足以证明开支显著努力减半它们的大小,执行分析和实验,以决定32位类型是否给出了良好的足够的结果,如果是这样使用它。否则,使用一个64位的类型。

If arrays are large enough to justify spending significant effort to halve their size, do analysis and experiments to decide whether a 32 bit type gives good enough results, and if so use it. Otherwise, use a 64 bit type.

这篇关于实数 - 如何确定是否需要浮点或双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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