⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 airconditioner.txt

📁 本源码为用于测试空调监控通讯代码是否正确的页面代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD>
  <TITLE>
    IEEE-754 Floating-Point Conversion from 32-bit Hexadecimal to
    Floating-Point
  </TITLE>

<SCRIPT = "JavaScript">
/*
  Copyright (c) 2003, City University of New York
  All rights reserved.

  Redistribution and use in source and binary forms, with or
  without modification, are permitted provided that the following
  conditions are met:

      * Redistributions of source code must retain the above
      * copyright notice, this list of conditions and the
      * following disclaimer.  Redistributions in binary form
      * must reproduce the above copyright notice, this list of
      * conditions and the following disclaimer in the
      * documentation and/or other materials provided with the
      * distribution.  Neither the name of Queens College of CUNY
      * nor the names of its contributors may be used to endorse
      * or promote products derived from this software without
      * specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  Original version by Quanfei Wen, 1997
  Modifications by Kevin J. Brewer, 1998/09/20 to 1998/09/30

   - Reordered Hex2Bin and Convert2Bin (with Convert2Bin first) so that Unix
     'diff' has some chance at comparing this file with IEEE-754.html .
   - General clean-ups.
   - Found and corrected bug in exponent calculation (and most significant bit
     placement) when converting from a 64-bit normalized value to one which
     must be denormalized in the 32-bit format (normalized to normalized
     conversion was already fine).

   1998/10/06 to 1998/10/07

   - Added removal of input leading and trailing blanks by adding RemoveBlanks
     routine.
   - Greatly improved the efficiency of the Convert2Hex routine.

   1998/10/20

   - Allow power of 10 indicator in numStrClipOff to be "E" as well as "e" in
     case not all browsers use "e".
   - Found and corrected bug in 32-bit binary output created by some unknown
     JavaScript scoping problem for the symbol 'output' by introduction of the
     RemoveBlanks routine.

   1998/10/23

   - All settings of 'this.StatCond' to "normal" were removed in order to match
     IEEE-754.html, "normal" rather than "error" is now the default.

   1998/10/28

   - Redundant code in Convert2Bin "already special values" section to that in
     Hex2Bin removed.

   1999/03/04

   - Corrected displaying error in numStrClipOff when the number of digits of
     precision for a particular IEEE-754 format is less than the number of
     digits returned by the system (before the value is large enough that the
     system returns it in "E" notation).
     Error found by Bill Maxwell (billmax@compuserve.com).

   1999/03/05

   - The system returns values such as 1.0E21 simply as 1E21.  In numStrClipOff,
     made adjustments to correct the display output when the system returns such
     values.
     Due to the idealized nature of the input to numStrClipOff vs. that of
     Canonical, many simplifications to the code in numStrClipOff were made.

   - Added a Clear button next to the input field which clears the input field
     and all result fields.

   1999/05/17

   - Removed <FONT FACE="Arial"> which is not displayed the same by all browser
     versions.

   - Balanced all <FONT> tags with </FONT> tags.

   - Removed all value layout comments since that information is much better
     presented in the IEEE-754references.html file.

   - Aligned all "Bit xx" and "Bits xx - xx" headings.

   - Replaced all occurrences of the term "unnormalized" with the term
     "denormalized" preferred by the standard.

   - Changed the "Decimal value of the exponent" display from "b + [e] = [f]"
     to "[f] - b = [e]" where b = 127 or b = 1023

   - Headings "Exponent" changed to "Exponent Field" and headings
     "Decimal value of the exponent" changed to
     "Decimal value of exponent field and exponent".

   1999/05/28

   - Rounded the 32-bit decimal significand just like the 32-bit full decimal
     value.

   - Fixed displaying problem in numStrClipOff in which values of
     0.000000099... and smaller (in 32-bit) are displayed as 0.0000000 because
     the values are not small enough for the system to return them in "E"
     notation and similarly for 0.000000000000000099... and smaller in 64-bit.

*/

function Convert2Bin(outstring, statstring, signBit, power, rounding)
{
  output = new String()                 //Output

  var binexpnt, index1, index2, cnst, bias, lastbit, rounded, index3, binexpnt2
  var moreBits

  cnst = 2102		// 1 (carry bit) + 1023 + 1 + 1022 + 53 + 2 (round bits)
  bias = 1024

  //init
  for (index1 = 0; index1 < this.Size; index1++)  this.Result[index1] = 0     

  with (Math) 					
  {
    //sign bit
    this.Result[0] = signBit

    //obtain exponent value
    index1 = 0

    if (this.Size == 32) index2 = 9
    else index2 = 12

    if (rounding && (outstring == ""))
    {
      //find most significant bit of significand
      while ((index1 < cnst) && (this.BinVal[index1] != 1)) index1++

      binexpnt = bias - index1

      //regular normalized numbers
      if (binexpnt >= this.MinExp)
      {
				//the value is shifted until the most
        index1++		//significant 1 is to the left of the binary
				//point and that bit is implicit in the encoding
      }//if normalized numbers

      //support for zero and denormalized numbers
      //exponent underflow for this precision
      else
      {
        binexpnt = this.MinExp - 1
        index1 = bias - binexpnt
      }//if zero or denormalized (else section)


      //use round to nearest value mode

      //compute least significant (low-order) bit of significand
      lastbit = this.Size - 1 - index2 + index1

      //the bits folllowing the low-order bit have a value of (at least) 1/2
      if (this.BinVal[lastbit + 1] == 1)
      {
        rounded = 0

        //odd low-order bit
        if (this.BinVal[lastbit] == 1)
        {
          //exactly 1/2 the way between odd and even rounds up to the even,
          //so the rest of the bits don't need to be checked to see if the value
          //is more than 1/2 since the round up to the even number will occur
          //anyway due to the 1/2
          rounded = 1
        }//if odd low-order bit

        //even low-order bit
        else  //this.BinVal[lastbit] == 0
        {
          //exactly 1/2 the way between even and odd rounds down to the even,
          //so the rest of the bits need to be checked to see if the value
          //is more than 1/2 in order to round up to the odd number
          index3 = lastbit + 2
          while ((rounded == 0) && (index3 < cnst))
          {
            rounded = this.BinVal[index3]
            index3++
          }//while checking for more than 1/2

        }//if even low-order bit (else section)

        //do rounding "additions"
        index3 = lastbit
        while ((rounded == 1) && (index3 >= 0))
        {
          // 0 + 1 -> 1 result with 0 carry
          if (this.BinVal[index3] == 0)
          {
            // 1 result
            this.BinVal[index3] = 1

            // 0 carry
            rounded = 0

          }//if bit is a 0

          // 1 + 1 -> 0 result with 1 carry
          else  //this.BinVal[index3] == 1
          {
            // 0 result
            this.BinVal[index3] = 0

            // 1 carry
//          rounded = 1
          }//if bit is a 1 (else section)

          index3--
        }//while "adding" carries from right to left in bits

      }//if at least 1/2

      //obtain exponent value
      index1 = index1 - 2
      if (index1 < 0) index1 = 0

    }//if rounding

    //find most significant bit of significand
    while ((index1 < cnst) && (this.BinVal[index1] != 1)) index1++

    binexpnt2 = bias - index1

    if (outstring == "")
    {
      binexpnt = binexpnt2

      //regular normalized numbers
      if ((binexpnt >= this.MinExp) && (binexpnt <= this.MaxExp))
      {
                                //the value is shifted until the most
        index1++                //significant 1 is to the left of the binary
                                //point and that bit is implicit in the encoding
      }//if normalized numbers

      //support for zero and denormalized numbers
      //exponent underflow for this precision
      else if (binexpnt < this.MinExp)
      {
        if (binexpnt2 == bias - cnst)
          //value is truely zero
          this.StatCond = "normal"
        else if (binexpnt2 < this.MinUnnormExp)
          this.StatCond = "underflow"
        else
          this.StatCond = "denormalized"

        binexpnt = this.MinExp - 1
        index1 = bias - binexpnt
      }//if zero or denormalized (else if section)
    }

    else //already special values
    {
      binexpnt = power
      index1 = bias - binexpnt

      //compute least significant (low-order) bit of significand
      lastbit = this.Size - 1 - index2 + index1

      moreBits = this.BinVal[lastbit]

      index3 = lastbit + 1
      while ((moreBits == 0) && (index3 < cnst))
      {
        moreBits = this.BinVal[index3]
        index3++
      }//while checking for more bits from other precision

      this.BinVal[lastbit] = moreBits

    }//if already special (else section)

    //copy the result
    while ((index2 < this.Size) && (index1 < cnst))
    {
      this.Result[index2] = this.BinVal[index1]
      index2++
      index1++
    }//while

    //max exponent for this precision
    if ((binexpnt > this.MaxExp) || (outstring != ""))
    {
      binexpnt = this.MaxExp + 1

      //overflow of this precision, set infinity
      if (outstring == "")
      {
        this.StatCond = "overflow"
        this.DispStr = "Infinity"

        if (this.Result[0] == 1)
          this.DispStr = "-" + this.DispStr

        if (this.Size == 32) index2 = 9
        else index2 = 12

        //zero the significand
        while (index2 < this.Size)
        {
          this.Result[index2] = 0
          index2++
        }//while

      }//if overflowed

      else //already special values
      {
        this.StatCond = statstring
        this.DispStr = outstring
      }//if already special (else section)

    }//if max exponent

    //convert exponent value to binary representation
    if (this.Size == 32) index1 = 8
    else index1 = 11
    this.BinaryPower = binexpnt
    binexpnt += this.ExpBias		//bias
    while ((binexpnt / 2) != 0)
    {
      this.Result[index1] = binexpnt % 2
      if (binexpnt % 2 == 0) binexpnt = binexpnt / 2
        else binexpnt = binexpnt / 2 - 0.5
      index1 -= 1
    }

    //output binary result
    output = ""
    for (index1 = 0; index1 < this.Size; index1++) 
      output = output + this.Result[index1]  
    return output

  }//with Math
}

function Hex2Bin(input)
{
  output = new String()                 //Output
  numerals = new String()

  var index1, nibble, i, s, binexpnt, cnst, bias, index2, zeroFirst, zeroRest
  var binexpnt2, index3

  cnst = 2102           // 1 (carry bit) + 1023 + 1 + 1022 + 53 + 2 (round bits)
  bias = 1024

  //init
  numerals = "0123456789ABCDEF"

  for (index1 = 0; index1 < cnst; index1++)  this.BinVal[index1] = 0

  for (index1 = 0; index1 < this.Size; index1++)  this.Result[index1] = 0

  with (Math)
  {

    input = RemoveBlanks(input)

    if (input.length > (this.Size / 4))
    {
      alert("too many hex digits")
      output = "exit"
      return output
    }

    else if (input.length < (this.Size / 4))
    {
      alert("too few hex digits")
      output = "exit"
      return output
    }

    else
    {
      input = input.toUpperCase()

      for (index1 = 0; index1 < this.Size; index1 +=4)
      {
        nibble = numerals.indexOf(input.charAt(index1 / 4))

        if (nibble == -1)
        {
          alert("invalid hex digit")
          output = "exit"
          return output
        }

        temp = nibble / 16

        for (i = 0; i < 4; i++)
        {
          temp *= 2
          if (temp >= 1)
          {
            this.Result[index1 + i] = 1
            temp --
          }
          else
            this.Result[index1 + i] = 0
        }
      }
    }

    //obtain exponent value
    binexpnt = 0

    if (this.Size == 32) index2 = 9
    else index2 = 12

    for (index1 = 1; index1 < index2; index1++)
      binexpnt += parseInt(this.Result[index1])*pow(2, index2 - index1 - 1)

    binexpnt -= this.ExpBias            //bias
    this.BinaryPower = binexpnt

    index1 = bias - binexpnt

    //regular normalized numbers
    if ((binexpnt >= this.MinExp) && (binexpnt <= this.MaxExp))
    {
      //the encoding's hidden 1 is inserted
      this.BinVal[index1] = 1
      index1++
    }//if normalized numbers

    index3 = index1

    //copy the input
    if (this.Result[index2] == 0)
      zeroFirst = true
    this.BinVal[index1] = this.Result[index2]
    index2++
    index1++

    zeroRest = true
    while ((index2 < this.Size) && (index1 < cnst))
    {
      if (this.Result[index2] == 1)
        zeroRest = false
      this.BinVal[index1] = this.Result[index2]
      index2++
      index1++
    }//while

    //find most significant bit of significand
    //for the actual denormalized exponent test for zero
    while ((index3 < cnst) && (this.BinVal[index3] != 1)) index3++
    binexpnt2 = bias - index3

    //zero and denormalized numbers
    if (binexpnt < this.MinExp)
    {
      if (binexpnt2 == bias - cnst)
        //value is truely zero
        this.StatCond = "normal"
      else if (binexpnt2 < this.MinUnnormExp)
        this.StatCond = "underflow"
      else
        this.StatCond = "denormalized"
    }//if zero or denormalized

    //max exponent for this precision
    else if (binexpnt > this.MaxExp)
    {
      if (zeroFirst && zeroRest)
      {
        //Infinity
        this.StatCond = "overflow"
        this.DispStr = "Infinity"
      }//if Infinity
      else if (!zeroFirst && zeroRest && (this.Result[0] == 1))
      {
        //Indeterminate quiet NaN
        this.StatCond = "quiet"
        this.DispStr = "Indeterminate"
      }//if Indeterminate quiet NaN (else if section)
      else if (!zeroFirst)
      {
        //quiet NaN
        this.StatCond = "quiet"
        this.DispStr = "NaN"
      }//if quiet NaN (else if section)
      else
      {
        //signaling NaN
        this.StatCond = "signaling"
        this.DispStr = "NaN"
      }//if signaling NaN (else section)

      if ((this.Result[0] == 1) && (this.DispStr != "Indeterminate"))
        this.DispStr = "-" + this.DispStr

    }//if max exponent (else if section)

    //output binary result
    output = ""
    for (index1 = 0; index1 < this.Size; index1++)
      output = output + this.Result[index1]
    return output

  }//with Math
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -