Floating Point Bit Level Arithmetic

This is a discussion on Floating Point Bit Level Arithmetic within the C Programming forums, part of the General Programming Boards category; Originally Posted by DanV2 Right, like iMalc mentioned, the while loops are for normalizing the mantissa of the result. The ...

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by DanV2 View Post
    Right, like iMalc mentioned, the while loops are for normalizing the mantissa of the result. The fraction part of an IEEE Floating point number should be 23 bits with an implied 1 bit, so the while loops shift right or left as necessary to normalize the result.

    As for shifting the two FRAC values to the right, I'm not completely sure why we do, but the TA told us that it was necessary to truncate the values so that the result would fit into an integer. At least, that's what I remember from the explanation he gave.
    While it's obvious that you're trying to simulate the results of floating point division, there's no need to shift the two mantissas to the right. The thing that needs to be done tho' is right shifting the lesser of the two fractions in order to equalise them. After equalising, if numerator <= denominator, repeatedly multiply it by 10 until it becomes just greater than the denominator, specifically:
    Code:
    1 / 5
    frac1 = 1 (1.0 x 2^0 - binary)
    frac2 = 5 (1.01 x 2^2 - binary)
    frac1 >>= 2;    // equalise the magnitudes of the two fractions
    while (frac1 < frac2)
        frac1 *= 10;
    And no need to store the result of FRAC1 / FRAC2 in a float, as it can obtained thro' integer division and modulus.
    Last edited by itCbitC; 10-26-2010 at 02:53 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 08:57 PM
  2. Need help with floating point values
    By 69gto96z in forum C++ Programming
    Replies: 6
    Last Post: 07-18-2008, 08:01 AM
  3. String and Floating Point Conversion Help
    By dfghjk in forum C++ Programming
    Replies: 14
    Last Post: 05-04-2008, 01:11 PM
  4. bit level hacking?
    By CtrlAltKick in forum C Programming
    Replies: 5
    Last Post: 09-07-2002, 08:36 PM
  5. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 11:55 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21