简体   繁体   English

float32 和 float64 的真正区别

[英]The real difference between float32 and float64

I want to understand the actual difference between float16 and float32 in terms of the result precision.我想了解float16float32在结果精度方面的实际区别。 For instance, Numpy allows you to choose the range of the datatype you want (np.float16, np.float32, np.float64) .例如, Numpy允许您选择所需数据类型的范围(np.float16, np.float32, np.float64) My concern is that if I decide to go with float 16 to reserve memory and avoid possible overflow, would that create a loss of the final results comparing with float32 for instance?我担心的是,如果我决定使用 float 16 来保留内存并避免可能的溢出,那么与例如 float32 相比,这会导致最终结果的损失吗?

Thank you谢谢

a = np.array([0.123456789121212,2,3], dtype=np.float16)
print("16bit: ", a[0])

a = np.array([0.123456789121212,2,3], dtype=np.float32)
print("32bit: ", a[0])

b = np.array([0.123456789121212121212,2,3], dtype=np.float64)
print("64bit: ", b[0])
  • 16bit: 0.1235 16位:0.1235
  • 32bit: 0.12345679 32位:0.12345679
  • 64bit: 0.12345678912121212 64位:0.12345678912121212

float32 is a 32 bit number - float64 uses 64 bits. float32 是一个 32 位数字 - float64 使用 64 位。

That means that float64's take up twice as much memory - and doing operations on them may be a lot slower in some machine architectures.这意味着 float64 占用两倍的内存 - 在某些机器架构中对它们进行操作可能会慢很多。

However, float64's can represent numbers much more accurately than 32 bit floats.但是,float64 可以比 32 位浮点数更准确地表示数字。

They also allow much larger numbers to be stored.它们还允许存储更大的数字。

For your Python-Numpy project I'm sure you know the input variables and their nature.对于您的 Python-Numpy 项目,我确信您知道输入变量及其性质。

To make a decision we as programmers need to ask ourselves为了做出决定,我们作为程序员需要问自己

  1. What kind of precision does my output need?我的输出需要什么样的精度?
  2. Is speed not an issue at all?速度根本不是问题吗?
  3. what precision is needed in parts per million?百万分之几需要多少精度?

A naive example would be if I store weather data of my city as [12.3, 14.5, 11.1, 9.9, 12.2, 8.2]一个天真的例子是,如果我将我所在城市的天气数据存储为 [12.3, 14.5, 11.1, 9.9, 12.2, 8.2]

Next day Predicted Output could be of 11.5 or 11.5164374第二天的预测输出可能是 11.5 或 11.5164374

do your think storing float 32 or float 64 would be necessary?您认为有必要存储 float 32 或 float 64 吗?

float32 is less accurate but faster than float64, and flaot64 is more accurate than float32 but consumes more memory. float32 比 float64 准确度较低但速度更快,而 flot64 比 float32 更准确但消耗更多内存。 If accuracy is more important than speed , you can use float64.如果准确性比速度更重要,您可以使用 float64。 and if speed is more important than accuracy, you can use float32.如果速度比准确性更重要,您可以使用 float32。

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

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