尾递归与Continuation

浮点数的二进制表示学习笔记

marz posted @ Sep 21, 2010 01:06:24 AM in algorithm with tags fpu ieee 754 float point algorithm , 2442 readers

 

基础知识:

十进制转十六进制;

十六进制转二进制;

IEEE制定的浮点数表示规则;

了解:

目前C/C++编译器标准都遵照IEEE制定的浮点数表示法来进行float,double运算。这种结构是一种科学计数法,用符号、指数和尾数来表示,底数定为2——即把一个浮点数表示为尾数乘以2的指数次方再添上符号。下面是具体的规格:
             符号位     阶码      尾数     长度
float           1          8        23      32
double          1         11        52      64

 

以下通过几个例子讲解浮点数如何转换为二进制数

例一:

已知:double类型38414.4

求:其对应的二进制表示。

分析:double类型共计64位,折合8字节。由最高到最低位分别是第636261……0位:
    最高位63位是符号位,1表示该数为负,0表示该数为正;
    62-52位,一共11位是指数位;
    51-0位,一共52位是尾数位。

     步骤:按照IEEE浮点数表示法,下面先把38414.4转换为十六进制数。
     把整数部和小数部分开处理:整数部直接化十六进制:960E。小数的处理:
0.4=0.5*0+0.25*1+0.125*1+0.0625*0+……
     
实际上这永远算不完!这就是著名的浮点数精度问题。所以直到加上前面的整数部分算够53位就行了。隐藏位技术:最高位的1不写入内存(最终保留下来的还是52位)。
    如果你够耐心,手工算到53位那么因该是:38414.4(10)=1001011000001110.
0110011001100110011001100110011001100(2)

科学记数法为:1.001011000001110 0110011001100110011001100110011001100,右移了15位,所以指数为15。或者可以如下理解:

1.001011000001110 0110011001100110011001100110011001100×2^15
     
于是来看阶码,按IEEE标准一共11位,可以表示范围是-1024 ~ 1023。因为指数可以为负,为了便于计算,规定都先加上1023(2^10-1),在这里,阶码:15+1023=1038。二进制表示为:100 00001110
    符号位:因为38414.4为正对应 0
    合在一起(注:尾数二进制最高位的1不要):
01000000 11100010 11000001 110
 01100  11001100  11001100  11001100  11001100

 

例二:

已知:整数3490593(16进制表示为0x354321)

求:其对应的浮点数3490593.0的二进制表示。 

解法如下:

先求出整数3490593的二进制表示:

 H:    3     5    4    3    2     1   (十六进制表示)

 B:   0011  0101 0100 0011 0010  0001 (二进制表示)

        │←──────21─────→│

 

即: 

               1.1010101000011001000012×221

可见,从左算起第一个121位,我们将这21为作为浮点数的小数表示,单精度浮点数float由符号位1位,指数域位k=8位,小数域位(尾数)n=23位构成,因此对上面得到的21位小数位我们还需要补上20,得到浮点数的小数域表示为:

         1 0101 0100 0011 0010 0001 00

 

float类型的偏置量Bias=2k-1-1=28-1-1=127,但还要补上刚才因为右移作为小数部分的21位,因此偏置量为127+21=148,就是IEEE浮点数表示标准:

                          V = (-1)s×M×2E

                    E = e-Bias

中的e,此前计算Bias=127,刚好验证了E=148-127=21

 

148转为二进制表示为10010100,加上符号位0,最后得到二进制浮点数表示1001010010101010000110010000100,其16进制表示为:

 H:     4        A       5          5         0         C         8        4  

 B:  0100   1010   0101    0101   0000   1100  1000   0100

                    |←────      21        ─────→   |

     1|←─8   ─→||←─────       23       ─────→ |

 

这就是浮点数3490593.0(0x4A550C84)的二进制表示。

 

例三:

0.5的二进制形式是0.1

它用浮点数的形式写出来是如下格式

 

0                01111110                 00000000000000000000000


符号位           阶码                       小数位

正数符号位为0,负数符号位为1

阶码是以2为底的指数

小数位表示小数点后面的数字


下面我们来分析一下0.5是如何写成0 01111110 00000000000000000000000


首先0.5是正数所以符号位为0

再来看阶码部分,0.5的二进制数是0.1,0.11.0*2^(-1),所以我们总结出来:

要把二进制数变成(1.f)*2^(exponent)的形式,其中exponent是指数

而由于阶码有正负之分所以阶码=127+exponent;

即阶码=127+(-1)=126  01111110

余下的小数位为二进制小数点后面的数字,00000000000000000000000


由以上分析得0.5的浮点数存储形式为0 01111110 00000000000000000000000  

注:如果只有小数部分,那么需要右移小数点比如右移3位才能放到第一个1的后面阶码就是127-3=124.

例四   (20.59375)10 =(10100.10011 2

首先分别将整数和分数部分转换成二进制数: 

20.59375
10100.10011 

然后移动小数点,使其在第12位之间 

10100.10011
1.010010011×2^4   e

于是得到: 

S
0 E4127131 M010010011 

最后得到32位浮点数的二进制存储格式为: 

0100 1001 1010 0100 1100 0000 0000 0000
(41A4C000)16

 

 

例五:
-12.5转为单精度二进制表示
12.5: 
1. 
整数部分12,二进制为1100; 小数部分0.5, 二进制是.1,先把他们连起来,从第一个1数起取24位(后面补0): 
1100.10000000000000000000 

这部分是有效数字。(把小数点前后两部分连起来再取掉头前的1,就是尾数) 
2. 
把小数点移到第一个1的后面,需要左移3位(1.10010000000000000000000*2^3加上偏移量127127+3=130,二进制是10000010,这是阶码。 
3. -12.5
是负数,所以符号位是1。把符号位,阶码和尾数连起来。注意,尾数的第一位总是1,所以规定不存这一位的1,只取后23位: 
1 10000010 10010000000000000000000 

把这32位按8位一节整理一下,得: 
11000001 01001000 00000000 00000000 

就是十六进制的 C1480000. 

例六:

2.025675 
1. 
整数部分2,二进制为10; 小数部分0.025675, 二进制是.0000011010010010101001,先把他们连起来,从第一个1数起取24位(后面补0): 
10.0000011010010010101001 

这部分是有效数字。把小数点前后两部分连起来再取掉头前的1,就是尾数: 00000011010010010101001 
2. 
把小数点移到第一个1的后面,左移了1加上偏移量127127+1=128,二进制是10000000,这是阶码。
3. 2.025675是正数,所以符号位是0。把符号位,阶码和尾数连起来: 
0 10000000 00000011010010010101001 

把这32位按8位一节整理一下,得: 
01000000 00000001 10100100 10101001 

就是十六进制的 4001A4A9.  


 

例七:
(逆向求十进制整数)一个浮点二进制数手工转换成十进制数的例子: 
假设浮点二进制数是 1011 1101 0100 0000 0000 0000 0000 0000 
1823位分成三段: 
01111010 10000000000000000000000 
最后一段是尾数。前面加上"1.", 就是 1.10000000000000000000000 
下面确定小数点位置。由
E = e-Bias阶码E01111010,加上00000101才是01111111127), 
所以他减去127的偏移量得e=-5。(或者化成十进制得122122-127=-5)。 
因此尾数1.10(后面的0不写了)是小数点右移5位的结果。要复原它就要左移5位小数点,得0.0000110, 即十进制的0.046875 
最后是符号:1代表负数,所以最后的结果是 -0.046875 

注意:其他机器的浮点数表示方法可能与此不同不能任意移植

 

再看一例(类似例七)

比如:53004d3e

二进制表示为:

01010011000000000100110100111110

按照1个符号    8个指数          23个小数位划分

0              10100110         00000000100110100111110

正确的结果转出来应该是551051722752.0

该怎么算?

好,我们根据IEEE的浮点数表示规则划分,得到这个浮点数的小数位是:

 00000000100110100111110

那么它的二进制表示就应该是:


1.000000001001101001111102 × 239

这是怎么来的呢? 别急,听我慢慢道来。
标准化公式中的M要求在规格化的情况下,取值范围1<M<(2-ε)

正因为如此,我们才需要对原始的整数二进制表示做偏移,偏移多少呢?偏移2E
这个“E”怎么算?上面的239怎么得来的呢?浮点数表示中的8位指数为就是告诉这个的。我们知道:
E = e-Bias
那么根据指数位:

101001102=>16610
e=166,由此算出E=e-Bias=166-127=39,就是说将整数二进制表示转为标准的浮点数二进制表示的时候需要将小数点左移39位,好,我们现在把它还原得到整数的二进制表示:

1 00000000100110100111110 0000000000000000

1│←───── 23─────→│←─── 16───→│

23+16=39,后面接着就是小数点了。
拿出计算器,输入二进制数1000000001001101001111100000000000000000
转为十进制数,不正是:551051722752么!

通过这例六例七,介绍了将整数二进制表示转浮点数二进制表示的逆过程,还是希望大家不但能掌握转化的方法,更要理解转化的基本原理。

 

http://blog.163.com/yql_bl/blog/static/847851692008112013117685/

  • No match
  • No match
meidir said:
Aug 20, 2022 12:21:02 AM

Thank you pertaining to giving this excellent content on your web-site. I discovered it on google. I may check back again if you publish extra aricles. 빅토리카지노주소

meidir said:
Aug 26, 2022 10:09:11 PM

Yay google is my world beater aided me to find this outstanding site! . 온라인바카라

meidir said:
Jan 16, 2023 04:18:21 PM

You must participate in a contest for among the finest blogs on the web. I will advocate this site! 룸알바

meidir said:
Jan 20, 2023 01:39:14 AM

Strong blog. I acquired various nice information. I?ve been keeping an eye fixed on this technology for some time. It?utes attention-grabbing the way it retains completely different, however a number of of the primary parts stay constant. have you ever observed plenty amendment since Search engines created their own latest purchase within the field? 홈타이서비스

 

 

========================

 

 

Hello, i think that i saw you visited my site thus i came to “return the favor”.I’m attempting to find things to enhance my web site!I suppose its ok to use a few of your ideas!! Gold99

meidir said:
Jan 24, 2023 10:25:55 PM

You really should be a part of a contest for one of the highest quality blogs over the internet. I’ll suggest this site! 성기확대수술

meidir said:
Jan 31, 2023 02:08:45 AM

After study a handful of the content on your web site now, and that i really like your way of blogging. I bookmarked it to my bookmark website list and will also be checking back soon. Pls look at my internet site too and told me what you think. Dallas security services

meidir said:
Feb 14, 2023 09:18:25 PM

Hey, I lately came to this internet site and I’ve visited it daily because that so that you can remain up to date. Maintain up the great function. 市場推廣

 

 

==============================

 

 

 

hey all, I used to be just checking out this blog and I actually admire the premise of the article, and don’t have anything to do, so if anybody wish to to have an engrossing convo about it, please contact me on AIM, my name is heather smith Room For Rent

meidir said:
Mar 11, 2023 11:00:06 PM

There’s noticeably a bundle to know about this. I assume you made certain good factors in features also. 메이저바카라

meidir said:
Mar 17, 2023 03:52:15 PM

Undeniably believe that which you stated. Your favorite justification seemed to be on the web the simplest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks 온라인카지노

meidir said:
Apr 08, 2023 02:00:44 AM I simply learned all about your internet site the other day and that i happen to be reading through it regularly. You have a a lot of extra practical information on the site and that i benefit from the particular type of the web page furthermore. Keep the good work! 바카라사이트
meidir said:
Apr 12, 2023 07:40:55 PM

I am curious to find out what blog system you’re working with? I’m having some minor security issues with my latest site and I would like to find something more safe. Do you have any suggestions? 여우알바

meidir said:
Apr 25, 2023 12:16:06 AM

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Text To Speech Free

meidir said:
Apr 27, 2023 03:42:08 PM

I feel like I’m constantly looking for interesting things to read about a variety of subjects, but I manage to include your blog among my reads every day because you have compelling entries that I look forward to. Here’s hoping there’s a lot more amazing material coming! cheapest auto transport

meidir said:
Apr 28, 2023 10:36:37 PM

I wan’t going to comment as this posts a bit old now, but just wanted to say thanks. voyance gratuite par telephone

Matthew Wade said:
Dec 07, 2024 10:34:32 PM

Hi there! I could have sworn I’ve been to this website before but after reading through some of the post I realized it’s new to me. Anyhow, I’m definitely glad I found it and I’ll be book-marking and checking back often! ECサイト セキュリティ

Matthew Wade said:
Dec 07, 2024 10:34:51 PM

I recognize there is certainly a great deal of spam on this blog. Do you want help cleansing them up? I may help in between classes! EC-CUBE カスタマイズ

Matthew Wade said:
Dec 13, 2024 11:06:08 PM

Aw, this became incredibly good post. Back in thinking I would like to make a note of that fit this description brings about : slacking plus total strength to earn a pretty decent article… on the flip side what could As i say… I really wait a large amount as well as in appear go basically finished. 大人動画

Matthew Wade said:
Dec 15, 2024 08:20:46 PM

Appreciation for this interesting writing. From the time that I commenced working on understanding much more with this topic, my entire life has better tremendously. I are looking at the choice of adding the opposite thing to my career for a year. For a nice and pleased with the sort of information I have gained from various websites, especially your site. I have a smaller budget to certainly obtain ebooks and video tutorials but your site is a good help to myself. 【フル映像】

Matthew Wade said:
Dec 17, 2024 10:16:00 PM

Very good put up, i definitely love this web site, keep it up エロ動画のフル動画

Matthew Wade said:
Dec 21, 2024 10:59:59 PM

Music began playing any time I opened this website, so frustrating! Japanese Pokemon Card


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter