首页 > 其他分享 >计算机存储浮点数

计算机存储浮点数

时间:2024-10-04 17:26:28浏览次数:6  
标签:存储 exponent mantissa 浮点数 number precision bit bits 计算机

计算机存储浮点数

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.

标签:存储,exponent,mantissa,浮点数,number,precision,bit,bits,计算机
From: https://www.cnblogs.com/smartljy/p/18446894

相关文章

  • 基于django+vue+Vue的高校设备信息管理系统的设计与实现【开题报告+程序+论文】-计算
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着高校教育事业的蓬勃发展,各类教学科研设备的数量急剧增加,设备信息管理成为高校管理中的重要环节。传统的人工管理方式不仅效率低下,而且......
  • 基于django+vue+Vue的高校教师多维考核评价系统设计开发与实现【开题报告+程序+论文】
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着高等教育的快速发展,高校教师的工作内容与职责日益复杂多样,传统的单一维度评价体系已难以满足当前对高校教师全面、公正评价的需求。近......
  • 基于django+vue+Vue的房屋租借系统【开题报告+程序+论文】-计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着城市化进程的加速和人口流动性的增强,房屋租借市场日益繁荣,成为满足人们居住需求的重要途径。然而,传统的房屋租借方式往往依赖于中介或......
  • java计算机毕业设计高校报修系统(开题+程序+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着高校规模的不断扩大和校园设施的日益复杂化,传统的人工报修方式已难以满足当前高效、便捷的维修需求。高校内建筑、设备种类繁多,维修需求频繁且紧......
  • java计算机毕业设计点餐平台网站(开题+程序+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展,人们的生活方式正经历着深刻的变革。在餐饮行业,点餐平台网站作为一种新兴的餐饮服务形式,已经逐渐渗透到人们的日常生活中。......
  • 基于Node.js+vue个人博客系统(开题+程序+论文) 计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展和普及,个人博客作为一种重要的网络日志形式,已经成为许多人分享生活感悟、技术心得、艺术创作的平台。个人博客不仅能够记录个人的......
  • C/C++ 判断计算机存储器字节序(端序)的几种方式
    字节序分为存储器字节序和网络字节序(通常采用大端),这里主要讨论的是主存储器字节序。主存是存储器中的一种,为什么只讨论主存?因为编写运行在现代主流操作系统上的程序,是没有I/O权限的。主存字节序所谓字节序就是字节排列的顺序,拿主存来说就是如果低字节存放在低地址处,就是低端......
  • 用Python实现运筹学——Day 10: 线性规划的计算机求解
    一、学习内容1.使用Python的scipy.optimize.linprog进行线性规划求解scipy.optimize.linprog是Python中用于求解线性规划问题的函数。它实现了单纯形法、内点法等算法,能够处理求解最大化或最小化问题,同时满足线性约束条件。线性规划问题的形式:线性规划问题可以描......
  • 【开题报告】基于Springboot+vue农村住宅房屋信息管理应用系统(程序+源码+论文) 计算机
    本系统(程序+源码)带文档lw万字以上文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着农村经济的快速发展和城乡一体化进程的加速推进,农村住宅房屋作为农村居民生活与生产的重要载体,其管理效率与信息化水平日益成为影响农村现代化建......
  • python+flask计算机毕业设计门诊管理系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着医疗技术的飞速发展和患者需求的日益增长,传统的手工门诊管理模式已难以满足现代医院高效、精准、便捷的服务要求。门诊作为医院面向患......