用C

2012-02-28 61 views
0

我有在C用C

我們給出兩個16位數字與浮點加法麻煩浮點加法,我們應該增加他們不要擔心情況下,他們的症狀是不同的或指數爲0或31.
這是一項家庭作業,但我遲到了&我不明白爲什麼它不起作用。

的Algrothim排序是這樣的:

  1. 獲取每個數字的符號位,如果他們的不同返回0
  2. 獲取每個數字的指數,找到一個更大2A 。找出兩者的區別。
  3. 在放在一個隱一個到每個尾數的
  4. 權由指數的差異具有較小的指數卻將數
  5. 以較大數量的符號和指數或將其與第11位
  6. 轉移尾數。
  7. 返回結果。

這裏是我的代碼:

LC3_Word FLADD(LC3_Word A, LC3_Word B) 
{ 
    // a debug variable, so we can turn errors on (debug = 1) and off (debug = 0) 
    int debug = 1; 

    // a default return value 
    LC3_Word returnValue = 0x0000; 

    if(debug) 
    { 
     printf("-----------------------------------\nBegin debug\n-----------------------------------\n"); 
     printf("Return value: %x \n",returnValue); 
     printf("LC3 words: A %x, B %x\n",A,B); 
    } 

    // Masks to extract sign, exponent and fraction 
    LC3_Word signBitMask = 0x8000; 
    LC3_Word expMask = 0x7C000; 
    LC3_Word mantissaMask = 0x03FF; 

    // Mask to get the sign with the exponent 
    LC3_Word signExpMask = 0xFC00; 

    // A mask for the implicit 1 
    LC3_Word implicitOne = 0x0400; 

    // Getting the signs 
    LC3_Word signA = AND(signBitMask,A); 
    LC3_Word signB = AND(signBitMask,B); 

    // Getting the exponents 
    LC3_Word expA = AND(expMask,A); 
    LC3_Word expB = AND(expMask,B); 

    // Getting the mantissa's 
    LC3_Word mantA = AND(mantissaMask,A); 
    LC3_Word mantB = AND(mantissaMask,B); 

     if(debug) 
     { 
      printf("======================\n"); 
      printf("\tSignBitMask: %x\n\texpMask: %x\n\tmantissaMask: %x\n",signBitMask,expMask,mantissaMask); 
      printf("\tSign EXP Mask: %x\n",signExpMask); 
      printf("\tsignA: %x, signB: %x\n", signA, signB); 
      printf("\tImplicit One Mask: %x\n",implicitOne); 
      printf("\tExp of a: %x, Exp of b: %x\n", expA, expB); 
      printf("\tmantissa of A: %x,mantissa of B: %x\n",mantA,mantB); 
      printf("======================\n"); 
     } 

    // Getting each with it's sign bit and it's exponent 
    LC3_Word signExpA = AND(signExpMask,A); 
    LC3_Word signExpB = AND(signExpMask,B); 

     if(debug) 
     { 
      printf("signExpA of A: %i, signExpB of B: %i\n",signExpA,signExpB); 
     } 

    // if the signs are different, don't deal with this case 
    if(signA!=signB) 
    { 
     return 0; 
    } 

    // if the signs are the same, if not, just return the default value 
    if(signA==signB) 
    { 
     if(debug) 
     { 
      printf("We got into the if signs are the same block \n"); 
      printf("Sign a: %i, Sign b: %i \n",signA,signB); 
     } 

     if(expA==expB) 
     { 
      if(debug) 
      { 
       printf("We got into the if exponents are the same block \n"); 
       printf("Exp a: %x, Exp b: %x \n",expA,expB); 
      } 

      // exponents are the same 
      // Add Mantissa B to A 
      mantA = ADD(mantB,mantA); 
      if(debug) 
      { 
       printf("Addition of mantissa's %x\n",mantA); 
      } 
      // store into the return value the logical and of the mantissa with the existing exponent and sign 
      // might want to do an OR() not an AND() 
      returnValue = OR(signExpA,mantA); 
     } // end if the eponents are the same 
     else { 
      if(debug) 
      { 
       printf("The exponents are not the same block \n"); 
      } 
      // Getting the size we need to shift by 
      int sizeToShift = 0; 
      if(expA>expB) 
      { 
       // Mask the mantissa of B with a implicit 1, then right shift 
       mantB = OR(implicitOne,mantB); 

       if(debug) 
       { 
        printf("The exponent a is > b\n"); 
       } 
       // need to shift B, getting the size of how much 
       sizeToShift = expA-expB; 

       if(debug) 
       { 
        printf("size to shift: %d,\nmantissaB is: %x\n",sizeToShift,mantB); 
       } 
       // right shifting the mantissa of b 
       mantB = mantB >> sizeToShift; 

       if(debug) 
       { 
        printf("mantissa of b shifted: %x\n",mantB); 
       } 
       returnValue = OR(signExpA,ADD(mantA,mantB)); 
      }// end if A > B in the exponent 
      else 
      { 
       // Mask the mantissa of A with a implicit 1, then right shift 
       mantA = OR(implicitOne,mantA); 
       if(debug) 
       { 
        printf("The exponent B is > A\n"); 
       } 
       // need to shift A, getting the size of how much 
       sizeToShift = expB-expA; 

       if(debug) 
       { 
        printf("size to shift: %d,\nmantissaA is: %x\n",sizeToShift,mantA); 
       } 
       // right shifting the mantissa of A 
       mantA = mantA >> sizeToShift; 

       if(debug) 
       { 
        printf("mantissa of A shifted: %x\n",mantA); 
       } 
       returnValue = OR(signExpB,ADD(mantA,mantB)); 
      }// end if B > A in the exponent   
     }// end if different exponents 
    } // end if the signs are the same 

    if(debug) 
    { 
     printf("Return Value %x\n",returnValue); 
     printf("-----------------------------------\nEnd debug\n-----------------------------------\n"); 
    } 

    return returnValue; 
} 

這裏有添加或和AND,

LC3_Word AND(LC3_Word A, LC3_Word B) 
{ 
    return (A&B); 
} 

LC3_Word OR(LC3_Word A, LC3_Word B) 
{ 
    return (A|B); 
} 

LC3_Word ADD(LC3_Word A, LC3_Word B) 
{ 
    return (A+B); 
} 

當我在浮點加2 + 3,我得到3,而不是5

任何想法?

+0

這些是16位浮點值?如果是這樣,你的expMask上沒有太多的零嗎? – 2012-02-28 02:12:57

+1

經過非常簡短的審視之後,您看起來似乎沒有考慮指數相等的簡單路徑中的隱式「1」。 – 2012-02-28 02:13:06

+0

你不應該使用一個單獨的變量來進行調試。它可能會佔用內存和/或CPU。而且如果在不同地方有幾百個這樣的變量呢?你是否打算全部編輯它們?使用'#define'和'#ifdef'代替它們,它們將由預處理器管理並在需要時剝離,因爲它們從未存在於代碼中 – 2013-09-21 01:59:31

回答

1
if(expA==expB) 
    { 
     if(debug) 
     { 
      printf("We got into the if exponents are the same block \n"); 
      printf("Exp a: %x, Exp b: %x \n",expA,expB); 
     } 

     // exponents are the same 
     // Add Mantissa B to A 
     mantA = ADD(mantB,mantA); 
     if(debug) 
     { 
      printf("Addition of mantissa's %x\n",mantA); 
     } 
     // store into the return value the logical and of the mantissa with the existing exponent and sign 
     // might want to do an OR() not an AND() 
     returnValue = OR(signExpA,mantA); 
    } // end if the eponents are the same 

這是錯誤的。

你沒有考慮到兩個隱式的加法。當你添加2 + 3時,你會添加1.0 x 2^1 + 1.1 x 2^1,並且你在小數點之前忽略了所有內容......所以你最終得到0.0 + 0.1 = 0.1, 1在前面。您還需要添加兩個隱式的。

嘗試這樣:

if(expA==expB) 
    { 
     if(debug) 
     { 
      printf("We got into the if exponents are the same block \n"); 
      printf("Exp a: %x, Exp b: %x \n",expA,expB); 
     } 

     // exponents are the same 
     // Add Mantissa B to A 
     mantA = OR(implicitOne,mantA); 
     mantB = OR(implicitOne,mantB); 

     mantA = ADD(mantB,mantA); 

     // You need to normalize this now. But shifting to the right by 1 will suffice. 
     mantA >>= 1; 
     ++expA; 
     // ... add the sign and you're done... 

     if(debug) 
     { 
      printf("Addition of mantissa's %x\n",mantA); 
     } 
     // store into the return value the logical and of the mantissa with the existing exponent and sign 
     // might want to do an OR() not an AND() 
     returnValue = OR(signExpA,mantA); 
    } // end if the eponents are the same 
0

我仍然可以通過讀碼,但不應該

LC3_Word expMask = 0x7C000;

LC3_Word expMask = 0x7C00 ;?

此外,你可以粘貼你的數字的二進制表示?通過這種算法,我們清楚了什麼。如果您正在使用一個轉換代碼,該錯誤也可能存在於您的轉換代碼中...