I am given three unsigned numbers of size 128 bits: a
, b
, and c
where a
and b
<= c
I want to be able to calculate (a * b) / c
with the highest possible precision.
If a
, b
, and c
were 64 bit integers, I would first calculate a * b
in a 128 bit number and then divide it by c
to obtain an accurate result. However, I am dealing with 128 bit numbers and I don't have a native 256 bit type to perform the multiplication a * b
.
Is there is a way to compute (a * b) / c
with high precision while staying in the world of 128 bits?
My (failed) attempts:
Calculating
a / (c / b)
. This looks somewhat unsymmetrical, and as expected I didn't get very accurate results.Calculating:
((((a+b)/c)^2 - ((a-b)/c)^2)*c)/4
=((a*b)/c^2) * c
=a*b/c
This also gave me pretty inaccurate results.