计算机存储浮点数

news/2024/12/21 21:20:10/文章来源:https://www.cnblogs.com/smartljy/p/18446894

计算机存储浮点数

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.hqwc.cn/news/807989.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

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个,请检查是否存在互相包含导致无限循环的情况”的错误,通常是因为模板文件中存在互相包含的情况。具体来说,可能是某个模板文件多次递归调用自身或其他模板文件,导致无限循环。 解决方法检查模板文件 定…

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

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

pbootcms模板文章列表没有缩略图时也不显示默认图片

通义灵码为了在PBootCMS模板中实现只有上传了缩略图才显示图片的功能,可以使用[list:isico]标签来判断是否存在缩略图。下面是具体实现的代码示例: 完整代码示例{pboot:list scode={sort:scode}}<a href="[list:link]" rel="external nofollow" title…

程序运行异常: Modulo by zero,位置

在PBOOTCMS中,确实存在一些特殊字符可能导致模板解析或数据存储的问题。特别是百分号 %,可能会被误认为SQL查询的一部分或其他特殊用途。为了避免这类问题,可以采取以下几种解决方案: 解决方案直接去掉百分号:直接去掉百分号,避免潜在的问题。使用中文表示:使用中文“百…

GIS发展趋势与国产GIS现状

地理信息系统(GIS)作为获取、管理、分析和可视化地理空间数据的重要工具,在多个领域发挥着至关重要的作用。随着技术的不断进步,GIS正朝着更高效、更智能的方向发展。GIS发展趋势1. 3D GIS与虚拟现实(VR)3D GIS技术通过增加高度信息,为地理空间数据提供了更丰富的展示和…

Pbootcms字段为空调用另一个字段标签代码

Pbootcms模板,自定义的字段为空时,调用另一个字段标签代码,方法如下:{pboot:if([list:ext_adminbuycn]!=)}[list:ext_adminbuycn]{else}[list:ico]{/pboot:if}扫码添加技术【解决问题】专注中小企业网站建设、网站安全12年。熟悉各种CMS,精通PHP+MYSQL、HTML5、CSS3、Java…

Gentoo 之 DHCP 设置静态IP

DHCP设置静态IP当我们使用virtualbox虚拟机,增加一个host-only模式的虚拟网卡时,往往因为系统内默认使用dhcp而导致ssh不能连接上一次的网卡IP地址。DHCP如何设置静态IP地址?如上图所示,显然DHCP也有配置文件可以做到设置静态IP的结果。 设置静态IP 增加/etc/dhcpcd.conf的…