Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need Help for C and C++ Avoiding Floationg Pt Usage (http://www.go4expert.com/forums/help-c-cpp-avoiding-floationg-pt-usage-t27011/)

 buffon2004 24Oct2011 21:37

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

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.

 xpi0t0s 25Oct2011 03:09

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

x=(728*y)/1000;

 buffon2004 25Oct2011 07:57

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

how to think without using division for that case? any idea?

 xpi0t0s 25Oct2011 22:20

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

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.

 buffon2004 28Oct2011 17:32

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

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

 xpi0t0s 28Oct2011 17:41

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

Before the interview think about how I might have found the value 0x02E978D5.

 buffon2004 28Oct2011 17:52

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

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?

 xpi0t0s 28Oct2011 19:22

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

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 28Oct2011 19:35

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

ok anyway thanks for ur help until now.
i will try to figure out about that:nice:

 All times are GMT +5.5. The time now is 10:59.