浮点数如何存储在内存中? [英] How are floating point numbers stored in memory?

查看:81
本文介绍了浮点数如何存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到它们以尾数和指数形式存储

I've read that they're stored in the form of mantissa and exponent

我已阅读此文档,但是我什么都听不懂. /p>

I've read this document but I could not understand anything.

推荐答案

要了解它们的存储方式,您必须首先了解它们的含义以及它们打算处理的类型的值.

To understand how they are stored, you must first understand what they are and what kind of values they are intended to handle.

与整数不同,浮点值旨在表示非常小的值和非常大的值.对于正常的32位浮点值,它对应的范围是 1.175494351 * 10 ^ -38 3.40282347 * 10 ^ + 38 .

Unlike integers, a floating-point value is intended to represent extremely small values as well as extremely large. For normal 32-bit floating-point values, this corresponds to values in the range from 1.175494351 * 10^-38 to 3.40282347 * 10^+38.

很显然,仅使用32位,就不可能在这样的数字中存储每个数字.

Clearly, using only 32 bits, it's not possible to store every digit in such numbers.

关于表示形式,您可以看到所有正常的浮点数,其值在1.0到(几乎)2.0的范围内,并且以2的幂进行缩放.所以:

When it comes to the representation, you can see all normal floating-point numbers as a value in the range 1.0 to (almost) 2.0, scaled with a power of two. So:

  • 1.0就是 1.0 * 2 ^ 0
  • 2.0是 1.0 * 2 ^ 1 ,并且
  • -5.0是 -1.25 * 2 ^ 2 .
  • 1.0 is simply 1.0 * 2^0,
  • 2.0 is 1.0 * 2^1, and
  • -5.0 is -1.25 * 2^2.

那么,需要什么来尽可能有效地对其进行编码?我们真正需要什么?

So, what is needed to encode this, as efficiently as possible? What do we really need?

  • 表达式的符号.
  • 指数
  • 该值在1.0到(几乎)2.0的范围内.这就是所谓的尾数"或有效位数.

根据IEEE-754浮点标准,其编码如下.

This is encoded as follows, according to the IEEE-754 floating-point standard.

  • 符号是一点点.
  • 指数存储为无符号整数,对于32位浮点值,此字段为8位. 1表示最小的指数,全1-1"表示最大. (0和全为1"用于编码特殊值,请参见下文.)中间的一个值(在32位情况下为127)表示零,这也称为 bias
  • 查看尾数时(1.0到(几乎)2.0之间的值),您会看到所有可能的值都以"1"开头(以十进制和二进制表示).这意味着没有必要存储它.其余的二进制数字存储在一个整数字段中,在32位的情况下,该字段为23位.

除了正常的浮点值外,还有许多特殊值:

In addition to the normal floating-point values, there are a number of special values:

  • 将零和尾数均编码为零的零.符号位用于表示正零"和负零".当运算结果极小时,负零很有用,但知道运算来自哪个方向仍然很重要.
  • 正负无穷大-使用全1"指数和零尾数字段表示.
  • 不是数字(NaN)-使用全1"指数和非零尾数表示.
  • 非正规数-小于最小正规数的数字.用零指数字段和非零尾数表示.这些数字的特殊之处在于,精度(即一个值可以包含的位数)将随着该值变得越小而下降,这仅仅是因为尾数中没有足够的空间.

最后,以下是一些具体示例(所有值均以十六进制表示):

Finally, the following is a handful of concrete examples (all values are in hex):

  • 1.0:3f800000
  • -1234.0:c49a4000
  • 100000000000000000000000.0:65a96816

这篇关于浮点数如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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