3 solutions

Solution 3

In my first solution to your question I took your question too literal: you ask for a floating point solution.
Reading now your expected result shows that you in fact talk about a fixed point problem.

A fix point approach places the separation between integral part and the fraction part at a fixed position within the binary format.

E.g. with 32 bit format, 22 integral bits and 10 fraction bits.

12.2 = 1100.0011001100
3.2 = 11.0011001100

In fix-point arithmetic, you can multiply as plain unsigned integer and shift the result by the proper amount of fractional bit back. To get a more accurate result, you may add two additional rounding bits, resulting in 10 + 2 fraction bit, where only the first 10 bits are taken and the last two are used for rounding.

E.g. with 32 bit format, 20 integral bits and 10+2 fraction bits.

12.2 = 1100.0011001100[11]
3.2 = 11.0011001100[11]

It can be viewed as scaling: in this example you have 10+2 fractional bits, hence, scaled by 2(10+2) = 4096.

Algorithm:
1) scale decimal numbers by 4096 and store as integral bit pattern
2) multiply the integral bit patterns as integer
3) divide result by 4096 and take the resulting bit pattern as fixpoint number
4) for printing: leave the two rounding bits away

Thanks for your 5!
He did not accept explicitly. It looks like if there is a certain number of votes for a solution, then that solution gets this light green color. An explicitly accepted solution is plain green.
Anyways, I could not resist to re-visit and get an alternative answer ;-)
Cheers
Andi

Solution 2

By "floating point", I assume you are talking about an arbitrary number of digits in "xxx.xxx" format and not the floating point values stored in float and double values, which are handled by machine hardware.

The first thing you need to do is break your number down into binary; after that do the math on the result. Digits ("bits") to the left of the decimal point (sic - actually binary point) follow the same rules as for translating integers to binary. Bits to the right of the binary point are what will make your head hurt, and correspond to 0.5, 0.25, 0.125... etc.

Understanding the previous paragraph should solve the problem for you.