计算机存储浮点数

news/2024/10/4 17:28:02

计算机存储浮点数

A computer stores floating-point numbers using a standardized format called IEEE 754. This format is designed to represent real numbers in a way that balances range and precision. Here's how it works:

Basic Structure of IEEE 754 Floating-Point Numbers

A floating-point number in a computer is typically represented by three components:

  1. Sign bit (S): This determines whether the number is positive (0) or negative (1).
  2. Exponent (E): This stores the exponent value, which determines the range of the number (i.e., how large or small it can be).
  3. Mantissa (or Significand) (M): This holds the significant digits of the number, representing its precision.

The general formula for a floating-point number is:

\[(-1)^{S} \times 1.M \times 2^{(E - \text{bias})} \]

Where:

  • S is the sign bit (0 for positive, 1 for negative).
  • M is the mantissa (or significand), typically in normalized form (starting with a leading 1).
  • E is the exponent, adjusted by a bias.

Common Floating-Point Formats

The two most common floating-point formats are single precision (32-bit) and double precision (64-bit).

1. Single Precision (32-bit Floating-Point):

  • 1 bit for sign (S)
  • 8 bits for exponent (E)
  • 23 bits for mantissa (M)

A 32-bit floating-point number has the following layout:

| S |  E (8 bits)  |       M (23 bits)         |
  • Range of exponent: The exponent is stored with a bias of 127 (i.e., ( E - 127 )), meaning the actual exponent is calculated as E - 127.
  • Mantissa: The 23 bits store the fractional part. The number is assumed to have a leading 1. (known as implicit leading 1), which is not stored explicitly. For example, a mantissa of 001 would be interpreted as 1.001.

2. Double Precision (64-bit Floating-Point):

  • 1 bit for sign (S)
  • 11 bits for exponent (E)
  • 52 bits for mantissa (M)

A 64-bit floating-point number has the following layout:

| S |      E (11 bits)     |               M (52 bits)               |
  • Range of exponent: The exponent is stored with a bias of 1023 (i.e., ( E - 1023 )).
  • Mantissa: The 52 bits store the fractional part, with an implicit leading 1.

Example of Single-Precision Float Representation

Suppose we want to store the number -6.75 as a 32-bit float:

  1. Convert to binary:

    • 6.75 in decimal is 110.11 in binary (6 = 110, and .75 = .11 in binary).
  2. Normalize the number:

    • In scientific notation, this is ( -1.1011 \times 2^2 ). This shows the sign bit is 1, the exponent is 2, and the mantissa is 1.1011.
  3. Set the components:

    • Sign bit: 1 (since the number is negative)
    • Exponent: 2 + 127 = 129 in decimal, which is 10000001 in binary.
    • Mantissa: The 1. is implicit, so we only store 1011, padded to 23 bits: 10110000000000000000000.

Thus, the 32-bit representation of -6.75 is:

1 10000001 10110000000000000000000

Precision and Limitations

  • Precision: The more bits in the mantissa, the more precise the number. Single-precision floats are accurate to about 7 decimal digits, while double-precision floats are accurate to about 15-16 decimal digits.
  • Range: The exponent allows floating-point numbers to represent a vast range, from very small numbers (close to zero) to very large ones.

Special Values

IEEE 754 also defines special cases:

  • Zero: Represented by all bits in the exponent and mantissa being zero.
  • Infinity: Represented by all bits in the exponent being 1, and the mantissa being all 0.
  • NaN (Not a Number): Represented by all bits in the exponent being 1, and the mantissa containing non-zero bits.

Summary

  • Floating-point numbers are stored in three parts: sign, exponent, and mantissa.
  • Single precision uses 32 bits, while double precision uses 64 bits.
  • The IEEE 754 standard defines how these components are laid out and how the numbers are calculated, enabling computers to store a wide range of real numbers with a trade-off between precision and range.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/67716.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

PbootCMS文章列表序号怎么写?

根据你提供的信息,我们可以进一步了解如何使用 pboot:list 标签,并结合 [list:n]、[list:i] 和 [list:id] 进行一些实用的功能实现。下面是一些具体的示例和应用场景: 1. 显示列表序号 假设我们需要显示一个列表,并且希望序号从 0 开始:html{pboot:list num=10} <li>…

PbootCMS隐藏指定 scode 的菜单各种条件判断和标签

{pboot:nav} <li {pboot:if([nav:scode] == 2 || [nav:scode] == 4 || [nav:scode] == 6)}style="display: none;"{/pboot:if}><a href="[nav:link]">{nav:name}</a> </li> {/pboot:nav}扫码添加技术【解决问题】专注中小企业网站…

PbootCMS判断导航从第几个开始各种条件判断和标签

{pboot:nav} {pboot:if([nav:i] > 2)} <li><a href="[nav:link]">{nav:name}</a></li> {/pboot:if} {/pboot:nav}扫码添加技术【解决问题】专注中小企业网站建设、网站安全12年。熟悉各种CMS,精通PHP+MYSQL、HTML5、CSS3、Javascript等。…

PbootCMS导航栏 logo 居中判断各种条件判断和标签

{pboot:nav} <a href="[nav:link]">{nav:name}</a> {pboot:if([nav:i] == 3)} <img src="{pboot:sitelogo}" /> {/pboot:if} {/pboot:nav}扫码添加技术【解决问题】专注中小企业网站建设、网站安全12年。熟悉各种CMS,精通PHP+MYSQL、HT…

PbootCMS判断列表页有无内容,无内容返回提示各种条件判断和标签

{pboot:if({page:rows} > 0)} <div class="page"><a href="{page:index}">首页</a><a href="{page:pre}">上一页</a>{page:numbar}<a href="{page:next}">下一页</a><a href="{…

检测到您模板中包含文件超过50个,请检查是否存在互相包含导致无限循环的情况!

在使用PBootCMS搭建网站时,如果遇到“检测到您模板中包含文件超过50个,请检查是否存在互相包含导致无限循环的情况”的错误,通常是因为模板文件中存在互相包含的情况。具体来说,可能是某个模板文件多次递归调用自身或其他模板文件,导致无限循环。 解决方法检查模板文件 定…

pbootcms教程—设置的会话目录创建失败!

当你在宝塔面板上部署PBootCMS模板时,可能会遇到“设置的会话目录创建失败”的问题。这通常是因为文件权限或目录权限设置不当导致的。以下是一步一步的解决方法: 解决方法登录宝塔面板打开浏览器,输入宝塔面板的地址(通常是http://你的服务器IP:8888),并登录宝塔面板。找…

自动加载类文件时发生错误,类名【core\basic\Kernel】

当你在使用PBootCMS时遇到“自动加载类文件时发生错误,类名【core\basicKernel】”的问题,通常是因为Kernel.php文件被误删除或丢失。特别是在阿里云虚拟主机上,这类文件可能会被误判为风险文件而被删除。以下是如何解决这一问题的具体步骤: 解决方法重新下载PBootCMS模板访…