Need Help for C and C++ Avoiding Floationg Pt Usage

Discussion in 'C++' started by buffon2004, Oct 24, 2011.

  1. buffon2004

    buffon2004 New Member

    Joined:
    Oct 24, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi can anybody help me in the following question?

    u16 x, y; //u16 means unsigned integer 16 bit
    x = y * 0.728
    Solve the above equation without using the floating point library and Division method. x and y data type also cannot be changed.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    x=(728*y)/1000;
     
    buffon2004 likes this.
  3. buffon2004

    buffon2004 New Member

    Joined:
    Oct 24, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    how to think without using division for that case? any idea?
    Thanks for your reply.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Tricky. How accurate does it need to be?
    Why 0.728? Where does this question come from?
    Also, what is the possible range of y?

    It can be solved if you can use 44 bit arithmetic or more: multiply by 0x02E978D5 and shift the result right by 26 bits, which is accurate to at least 4 decimal places (checked on a spreadsheet) but needs that extra headroom.
     
    Last edited: Oct 25, 2011
    buffon2004 likes this.
  5. buffon2004

    buffon2004 New Member

    Joined:
    Oct 24, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thank you very much for ur kind help.
    it is an interview question for me to explain in second interview.
    Ur idea is very nice and i tested in my compiler it is working also
    Thanks again
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Before the interview think about how I might have found the value 0x02E978D5.
     
    buffon2004 likes this.
  7. buffon2004

    buffon2004 New Member

    Joined:
    Oct 24, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    yes that one i need to ask u that how u got this value 26 bits have to shift to get this precision?
    Is there any way to calculate that no:26?
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I can't spoon-feed you everything. I'll end up doing your job for you. Think about it and have a few guesses.
     
    buffon2004 likes this.
  9. buffon2004

    buffon2004 New Member

    Joined:
    Oct 24, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    ok anyway thanks for ur help until now.
    i will try to figure out about that:nice:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice