找回密码
 FreeOZ用户注册
查看: 2879|回复: 4
打印 上一主题 下一主题

[论坛技术] IEEE 754 浮点数转换器Javascript实现

[复制链接]
跳转到指定楼层
1#
发表于 20-6-2009 17:47:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
有一个Java Applet的版本,我做了一个javascript的版本。

Applet的版本请看这里。
http://www.h-schmidt.net/FloatApplet/IEEE754.html

下面是我自己写的Javascript实现,处理的是32位Single
  1. <!DOCTYPE html PUBLIC "-//WEC///DTD XHTML 1.0 Transitional//EN"
  2.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Bit Test</title>
  6. <script type="text/javascript">
  7. window.onload=initPage

  8. function initPage()
  9. {
  10.         var obj = document.getElementById('float-form');
  11.        
  12.         for(var x in obj.elements)
  13.         {
  14.                 if(obj.elements[x].type == 'checkbox')
  15.                 {
  16.                         obj.elements[x].onchange = function(){
  17.                                 document.getElementById('float-form').submit.click();
  18.                         }
  19.                 }
  20.         }
  21.                
  22.         document.getElementById('float-form').onsubmit = function(){
  23.                 var sign = 1;
  24.                 if(this.sign.checked)
  25.                         sign = -1;
  26.                 var expoCheckValues = [ this.expo0, this.expo1,
  27.                         this.expo2, this.expo3, this.expo4,
  28.                         this.expo5, this.expo6];
  29.                        
  30.                 var mantisseCheckValues = [ this.mant0, this.mant1, this.mant2,
  31.                         this.mant3, this.mant4, this.mant5,
  32.                         this.mant6, this.mant7, this.mant8,                       
  33.                         this.mant9, this.mant10, this.mant11,
  34.                         this.mant12, this.mant13, this.mant14,
  35.                         this.mant15, this.mant16, this.mant17,
  36.                         this.mant18, this.mant19, this.mant20,
  37.                         this.mant21, this.mant22];
  38.                
  39.                 var expo = 0;
  40.                 var mantisse = 1;
  41.                
  42.                 for(var k=0; k<expoCheckValues.length; ++k)
  43.                 {
  44.                         if(expoCheckValues[k].checked)
  45.                                 expo += Math.pow(2, k);
  46.                 }
  47.                
  48.                 if(!this.expo7.checked)
  49.                         expo += -127;
  50.                 else
  51.                         expo += 1;
  52.                        
  53.                 for(var k=0; k<mantisseCheckValues.length; ++k)
  54.                 {
  55.                         if(mantisseCheckValues[k].checked)
  56.                                 mantisse += Math.pow(2.0, -mantisseCheckValues.length + k );
  57.                 }
  58.                
  59.                 document.getElementById('result').innerHTML = 'exponent = ' + expo + '
  60. '
  61.                         + 'mantisse = ' + mantisse + '
  62. '
  63.                         + 'value = ' + ( sign * Math.pow(2, expo) * mantisse );
  64.                        
  65.                 return false;
  66.                        
  67.         }
  68. }
  69. </script>
  70. </head>
  71. <body>
  72. <p>
  73. <form name="floatform" id="float-form" action="#" method="get">
  74.         <label>31: sign</label>: <input type="checkbox" name="sign" />

  75.         <label>30 - 23: exponent[7 - 0]</label>:<input type="checkbox" name="expo7"/>
  76.         <input type="checkbox"  name="expo6"/>
  77.         <input type="checkbox"  name="expo5"/>
  78.         <input type="checkbox"  name="expo4"/>
  79.         <input type="checkbox"  name="expo3"/>
  80.         <input type="checkbox"  name="expo2"/>
  81.         <input type="checkbox"  name="expo1"/>
  82.         <input type="checkbox"  name="expo0"/>

  83.         <label>22 - 0: mantisse[22 - 0]</label>:<input type="checkbox" name="mant22"/>
  84.         <input type="checkbox"  name="mant21"/>
  85.         <input type="checkbox" name="mant20"/>
  86.         <input type="checkbox" name="mant19"/>
  87.         <input type="checkbox" name="mant18"/>
  88.         <input type="checkbox" name="mant17"/>
  89.         <input type="checkbox" name="mant16" />
  90.         <input type="checkbox" name="mant15" />
  91.         <input type="checkbox" name="mant14" />
  92.         <input type="checkbox" name="mant13" />
  93.         <input type="checkbox" name="mant12" />
  94.         <input type="checkbox" name="mant11" />
  95.         <input type="checkbox" name="mant10" />
  96.         <input type="checkbox" name="mant9" />
  97.         <input type="checkbox" name="mant8" />
  98.         <input type="checkbox" name="mant7" />
  99.         <input type="checkbox" name="mant6" />
  100.         <input type="checkbox" name="mant5" />
  101.         <input type="checkbox" name="mant4" />
  102.         <input type="checkbox" name="mant3" />
  103.         <input type="checkbox" name="mant2" />
  104.         <input type="checkbox" name="mant1" />
  105.         <input type="checkbox" name="mant0" />

  106.         <input type="submit" name="submit" value="submit" />
  107. </form>
  108. </p>

  109. <p>
  110. <span id="result" name="result"></span>
  111. </body>
  112. </html>
复制代码

评分

参与人数 2威望 +60 收起 理由
ubuntuhk + 30 你太有才了!
coredump + 30 你太有才了!

查看全部评分

回复  

使用道具 举报

2#
发表于 20-6-2009 17:49:09 | 只看该作者
你最近还真是有空啊,不错不错。
回复  

使用道具 举报

3#
 楼主| 发表于 20-6-2009 17:51:57 | 只看该作者
回复  

使用道具 举报

4#
发表于 20-6-2009 17:53:23 | 只看该作者

回复 #3 key 的帖子

我知道,我知道,谁让你劝人家离婚的
回复  

使用道具 举报

5#
发表于 20-6-2009 19:25:41 | 只看该作者

回复 #3 key 的帖子

回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 29-3-2025 03:25 , Processed in 0.034424 second(s), 23 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表