如何规范尾数 [英] How to normalize a mantissa
问题描述
我正在尝试将 int
转换为自定义浮点型,其中用户指定为exp和尾数保留的位数,但我没有了解转换的工作原理。我的函数接受一个int值和一个int exp来表示数字(值* 2 ^ exp),即value = 12,exp = 4,返回192。但是我不了解更改这些值的过程。我已经研究了好几天了,并且正在使用IEEE转换器网络应用程序,但是我只是不了解标准化过程是什么。就像我看到它的移动二进制点并调整指数一样,但是我不知道这是什么意思,有人可以举一个例子吗?另外我也不明白指数偏差是什么。我仅有的信息是,您只需在指数中添加一个数字,但我不明白为什么。我一直在Google搜索我可以理解的示例,但这对我来说没有任何意义
I'm trying to convert an int
into a custom float, in which the user specifies the amount of bits reserved for the exp and mantissa, but I don't understand how the conversion works. My function takes in an int value and and int exp to represent the number (value * 2^exp) i.e value = 12, exp = 4, returns 192. but I don't understand the process I need to do to change these. I've been looking at this for days and playing with IEEE converter web apps but I just don't understand what the normalization process is. Like I see that its "move the binary point and adjust the exponent" but I have no idea what this means, can anyone give me an example to go off of? Also I don't understand what the exponent bias is. The only info I have is that you just add a number to your exponent but I don't understand why. I've been searching Google for an example I can understand but this just isn't making any sense to me
推荐答案
当我们强制将其尾数的整数部分精确地设为 1
并允许其分数部分为我们想要的时,点号为归一化。
A floating point number is normalized when we force the integer part of its mantissa to be exactly 1
and allow its fraction part to be whatever we like.
例如,如果我们采用数字 13.25
,即 1101.01
二进制, 1101
是整数部分, 01
是小数部分。
For example, if we were to take the number 13.25
, which is 1101.01
in binary, 1101
would be the integer part and 01
would be the fraction part.
我可以将 13.25
表示为 1101.01 *(2 ^ 0)
,但这并未标准化,因为整数部分不是 1
。 但是,如果我们将指数增加1:我们可以将尾数向右移一位。
I could represent 13.25
as 1101.01*(2^0)
, but this isn't normalized because the integer part is not 1
. However, we are allowed to shift the mantissa to the right one digit if we increase the exponent by 1:
1101.01*(2^0)
= 110.101*(2^1)
= 11.0101*(2^2)
= 1.10101*(2^3)
此表示 1.10101 *(2 ^ 3)
是归一化的 13.25
的形式。
This representation 1.10101*(2^3)
is the normalized form of 13.25
.
也就是说,我们知道已归一化浮点数的格式始终为 1.fffffff *(2 ^ exp)
That said, we know that normalized floating point numbers will always come in the form 1.fffffff * (2^exp)
为了效率起见,我们不必费心在二进制表示形式本身中存储 1
整数部分,我们只是假装它在那里。因此,如果要给您的定制浮点型5个尾数位,我们知道 10100
位实际上代表 1.10100
。
For efficiency's sake, we don't bother storing the 1
integer part in the binary representation itself, we just pretend it's there. So if we were to give your custom-made float type 5 bits for the mantissa, we would know the bits 10100
would actually stand for 1.10100
.
以下是标准23位尾数的示例:
Here is an example with the standard 23-bit mantissa:
关于指数偏差,让我们看一下标准的32位 float
格式,该格式分为3部分: 1个符号位,8个指数位和23个尾数位:
As for the exponent bias, let's take a look at the standard 32-bit float
format, which is broken into 3 parts: 1 sign bit, 8 exponent bits, and 23 mantissa bits:
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
指数 00000000
和 11111111
具有特殊用途(例如表示 Inf
和 NaN
),因此使用8个指数位,我们可以表示254个不同的指数,例如说 2 ^ 1
到 2 ^ 254
。但是,如果我们想代表 2 ^ -3
怎么办?我们如何获得负指数?
The exponents 00000000
and 11111111
have special purposes (like representing Inf
and NaN
), so with 8 exponent bits, we could represent 254 different exponents, say 2^1
to 2^254
, for example. But what if we want to represent 2^-3
? How do we get negative exponents?
该格式通过自动从指数中减去127来解决此问题。因此:
The format fixes this problem by automatically subtracting 127 from the exponent. Therefore:
-
0000 0001
将是1 -127 = -126
-
0010 1101
将是45 -127 = -82
-
0111 1111
将是127-127 = 0
-
1001 0010
将是136-127 = 9
0000 0001
would be1 -127 = -126
0010 1101
would be45 -127 = -82
0111 1111
would be127-127 = 0
1001 0010
would be136-127 = 9
这会将指数范围从 2 ^ 1 ... 2 ^ 254
更改到 2 ^ -126 ... 2 ^ + 127
,所以我们可以表示负指数。
This changes the exponent range from 2^1 ... 2^254
to 2^-126 ... 2^+127
so we can represent negative exponents.
这篇关于如何规范尾数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!