马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
有一个Java Applet的版本,我做了一个javascript的版本。
Applet的版本请看这里。
http://www.h-schmidt.net/FloatApplet/IEEE754.html
下面是我自己写的Javascript实现,处理的是32位Single- <!DOCTYPE html PUBLIC "-//WEC///DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Bit Test</title>
- <script type="text/javascript">
- window.onload=initPage
- function initPage()
- {
- var obj = document.getElementById('float-form');
-
- for(var x in obj.elements)
- {
- if(obj.elements[x].type == 'checkbox')
- {
- obj.elements[x].onchange = function(){
- document.getElementById('float-form').submit.click();
- }
- }
- }
-
- document.getElementById('float-form').onsubmit = function(){
- var sign = 1;
- if(this.sign.checked)
- sign = -1;
- var expoCheckValues = [ this.expo0, this.expo1,
- this.expo2, this.expo3, this.expo4,
- this.expo5, this.expo6];
-
- var mantisseCheckValues = [ this.mant0, this.mant1, this.mant2,
- this.mant3, this.mant4, this.mant5,
- this.mant6, this.mant7, this.mant8,
- this.mant9, this.mant10, this.mant11,
- this.mant12, this.mant13, this.mant14,
- this.mant15, this.mant16, this.mant17,
- this.mant18, this.mant19, this.mant20,
- this.mant21, this.mant22];
-
- var expo = 0;
- var mantisse = 1;
-
- for(var k=0; k<expoCheckValues.length; ++k)
- {
- if(expoCheckValues[k].checked)
- expo += Math.pow(2, k);
- }
-
- if(!this.expo7.checked)
- expo += -127;
- else
- expo += 1;
-
- for(var k=0; k<mantisseCheckValues.length; ++k)
- {
- if(mantisseCheckValues[k].checked)
- mantisse += Math.pow(2.0, -mantisseCheckValues.length + k );
- }
-
- document.getElementById('result').innerHTML = 'exponent = ' + expo + '
- '
- + 'mantisse = ' + mantisse + '
- '
- + 'value = ' + ( sign * Math.pow(2, expo) * mantisse );
-
- return false;
-
- }
- }
- </script>
- </head>
- <body>
- <p>
- <form name="floatform" id="float-form" action="#" method="get">
- <label>31: sign</label>: <input type="checkbox" name="sign" />
- <label>30 - 23: exponent[7 - 0]</label>:<input type="checkbox" name="expo7"/>
- <input type="checkbox" name="expo6"/>
- <input type="checkbox" name="expo5"/>
- <input type="checkbox" name="expo4"/>
- <input type="checkbox" name="expo3"/>
- <input type="checkbox" name="expo2"/>
- <input type="checkbox" name="expo1"/>
- <input type="checkbox" name="expo0"/>
- <label>22 - 0: mantisse[22 - 0]</label>:<input type="checkbox" name="mant22"/>
- <input type="checkbox" name="mant21"/>
- <input type="checkbox" name="mant20"/>
- <input type="checkbox" name="mant19"/>
- <input type="checkbox" name="mant18"/>
- <input type="checkbox" name="mant17"/>
- <input type="checkbox" name="mant16" />
- <input type="checkbox" name="mant15" />
- <input type="checkbox" name="mant14" />
- <input type="checkbox" name="mant13" />
- <input type="checkbox" name="mant12" />
- <input type="checkbox" name="mant11" />
- <input type="checkbox" name="mant10" />
- <input type="checkbox" name="mant9" />
- <input type="checkbox" name="mant8" />
- <input type="checkbox" name="mant7" />
- <input type="checkbox" name="mant6" />
- <input type="checkbox" name="mant5" />
- <input type="checkbox" name="mant4" />
- <input type="checkbox" name="mant3" />
- <input type="checkbox" name="mant2" />
- <input type="checkbox" name="mant1" />
- <input type="checkbox" name="mant0" />
- <input type="submit" name="submit" value="submit" />
- </form>
- </p>
- <p>
- <span id="result" name="result"></span>
- </body>
- </html>
复制代码 |