I am developing a BigNumber library which allows for creation of large numbers (Integers and Floating). You can find the public repository here
I've implemented add and subtract for both BigFloating and BigInteger, however multiply and divide only for BigInteger
The bits for a floating point number are stored in a std::vector and are in the format:
sign (1 bit)|binary integer(variable bits)|binary fraction(variable bits)
Such that the number 5.5 would have the bits 0 101 1
What are some algorithms for multiplying and dividing numbers with this format?
i.e.
(5.5) 101 1
* (5.5) 101 1
-------------
= (30.25) 11110 01
or
(5.5) 101 1
/ (5.5) 101 1
-------------
= (1) 1
The functions to be implemented are found in BigCommon.cpp and are:
std::tuple<std::vector<bool>, size_t, size_t> BigCommon::multiplyBits(const std::vector<bool> &lhsBits, const std::vector<bool> &rhsBits, const size_t &integerBitsSize, const size_t &mantissaBitsSize)
and
std::tuple<std::vector<bool>, size_t, size_t> BigCommon::divideBits(const std::vector<bool> &lhsBits, const std::vector<bool> &rhsBits, const size_t &integerBitsSize, const size_t &mantissaBitsSize)
Update
I have implemented the multiplyBits algorithm like so:
std::tuple<std::vector<bool>, size_t, size_t> BigCommon::multiplyBits(const std::vector<bool> &lhsBits, const std::vector<bool> &rhsBits, const size_t &integerBitsSize, const size_t &mantissaBitsSize)
{
std::vector<bool> result;
result.insert(result.begin(), lhsBits[0] ^ rhsBits[0]);
size_t newIntegerBitsSize = 0;
size_t newMantissaBitsSize = mantissaBitsSize + mantissaBitsSize;
std::vector<bool> lhsBinary(lhsBits.begin() + 1, lhsBits.end());
std::vector<bool> rhsBinary(rhsBits.begin() + 1, rhsBits.end());
std::vector<bool> multResult = multiplyBinaryVectors(lhsBinary, rhsBinary);
newIntegerBitsSize = multResult.size() - newMantissaBitsSize;
result.insert(result.begin() + 1, multResult.begin(), multResult.end());
return {result, newIntegerBitsSize, newMantissaBitsSize};
};
Now just for divide!
Update 2
I have successfully implemented division using the following algorithm:
Code redacted in favour of answer.
Update 3
After some testing I found the division algorithm doesn't work with some types of numbers, here are some test cases: 5 / 0.27 10 / 100. Definitely to do with divideBinaryVectors