IEEE754 单精度浮点数

关于IEEE754 单精度浮点数的定义,转换与数值范围

IEEE 754

IEEE754 将数字表示成形如

$$Z=(-1)^s \times M \times 2^E$$

的形式.

其中,

  • 符号位s由一位二进制表示,1表示负数,0表示正数
  • 有效数(尾数)M 由23位二进制表示
  • 指数E由8位二进制移码表示

数据格式

31          22                           0
|________________________________________|
|s|   c    |          f                  |
------------------------------------------
   |      |
   30    23

尾数的正规化(规约形式):
IEEE754 规定位数应该化为 1.f 的形式, 第一位由于必定为1, 略去不计。

栗子-3125.97

符号位
负数符号位取1

二进制转换精确度
$log_210 = 3.322$ , 十进制转换2进制时小数应当记录7位。

正规化尾数
$3125.97_{(10)} = 110000110101.1111100_{(2)} = 1.100001101011111100_{(2)} \times 2^{11}$
最左侧的1省略不记,不足28位右侧补0, 那么 -3125.97 的规约形式尾数部分为
100 0011 0101 1111 1000 0000

指数:
11 转化为8位2进制移码。
$C = c + B = 11_{(10)} + 127_{(10)} = 138 = 1000 1010_{(2)}$

单精度浮点数表示

Z = 1100 0101 0100 0011 0101 1111 1000 0000

其中
s = 1
c = 1000 1010
f = 100 0011 0101 1111 1000 0000

表示值

Name c f Z
规约形式 $0<c<2B+1$ f 任意 $Z=(-1)^s\cdot (1.f)\cdot 2^{c-B}$
非规约形式 $c=0$ f 任意 $Z=(-1)^s\cdot (0.f)\cdot 2^{1-B}$
无穷 $c=2B+1$ $f=0$ $Z=(-1)^s\cdot \infty$
不存在 $c=2B+1$ $f\ne 0$ $Z=NaN$

Notes:

  • 8位移码表示的指数中,偏移B=127
  • c = e+B , 其中c为二进制移码表示的指数,e为二进制原码表示的指数
  • 后两种特殊情况中,c=2B+1 即c=255, 取全1
  • 非规约形式是为了表示绝对值非常接近0的数,非规约形式由于实际指数固定,在其表示范围内精度保持不变。

范围

尾数及其表示的有效数
23位2进制尾数表示范围是 $[0, 2^{23}]$
规约尾数表示的的有效数范围是 $[1, 2-10^{-23}]$

指数

实际指数e 偏移指数c
[-126,127] c = e+127
e = -126 c = 0

规约的实际指数范围:
[-126,127]

规约的偏移指数范围:
c = e + 127, 即 [1,254]

非规约的指数:
实际指数为 -126, 偏移指数记0

绝对值最大的归约数:
$\pm (2-2^{-23}) \times 2^{127}$

绝对值最小的规约数:
$\pm 2^{-126}$

绝对值最大的非规约数:
$\pm (1-2^{-23} \times 2^{-126})$

绝对值最小的非规约数:
$\pm 2^{-23} \times 2^{-126}$

Nov 25, 2020


[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<




Fun with Image Maps and SVGs via benji February 20, 2024

Over the past few weeks I've been playing around with making some images on my website interactive. My "informatics" class in high school taught us some basic HTML. By basic I mean in notepad and writing everything by hand, saving to a...

Goblin Week 2024 via Helvetica Blanc January 26, 2024

It snuck up on me, but I managed to draw my little goblins to celebrate the week! I love my children - they're like awful Pikmin. I've uploaded the whole parade as a print, as well as individual prints for each goblin. There's something very fu…

How to trust gpg keys via Travis Shears Personal Site October 27, 2023

After moving some GPG keys to a new computer I kept getting these trust warnings. It is NOT certain that the key belongs to the person named in the user ID.If you * really * know what you are doing, you may answer the next question with yes. Use this key a…

Generated by openring from webring