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

buffon2004's Avatar, Join Date: Oct 2011
Light Poster
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
x=(728*y)/1000;
buffon2004 like this
0
buffon2004's Avatar, Join Date: Oct 2011
Light Poster
how to think without using division for that case? any idea?
Thanks for your reply.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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 by xpi0t0s; 25Oct2011 at 22:36..
buffon2004 like this
0
buffon2004's Avatar, Join Date: Oct 2011
Light Poster
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
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Before the interview think about how I might have found the value 0x02E978D5.
buffon2004 like this
0
buffon2004's Avatar, Join Date: Oct 2011
Light Poster
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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 like this
0
buffon2004's Avatar, Join Date: Oct 2011
Light Poster
ok anyway thanks for ur help until now.
i will try to figure out about that