0

How to convert a Bignum value into a double value and so can do all the ordinary arthiemetic operations on it. I'm trying the following code but it seems to be foolish to do it in this way. So, I'm asking about if a Todouble() operator exists which can do this work.
Furthermore, I need an additional operator for division for these Bignums, and if one gives more operators for the other basic(+-*) operations it will be better. Thanks in advance.

#include <assert.h>
#include <iomanip>
#include <iostream.h>
#include <stdint.h>
#include <vector>
class Bignum
{
public:
Bignum(int value)
{
    assert(value >= 0 && value <= 999999999);
    parts.push_back(value);
}

Bignum& operator*=(int rhs)
{
    assert(rhs >= 0 && rhs <= 999999999);
    uint32_t carry = 0;
    for (size_t i = 0; i < parts.size(); i++)
    {
        uint64_t product = (uint64_t)parts[i] * (uint64_t)rhs + carry;
        parts[i] = (uint32_t)(product % 1000000000LL);
        carry = (uint32_t)(product / 1000000000LL);
    }
    if (carry != 0)
        parts.push_back(carry);
    return *this;
 }

friend std::ostream & operator<<(std::ostream& stream, const Bignum& num);
private:
std::vector<uint32_t> parts;
};
inline std::ostream& operator<<(std::ostream& stream, const Bignum& num)
{
char oldfill = stream.fill('0');
for (std::vector<uint32_t>::const_reverse_iterator it = num.parts.rbegin();
it != num.parts.rend(); it++)         
    stream << *it << std::setw(9);
stream.fill(oldfill);
stream.width(0);
return stream;
}
Bignum factorial(int n)
{
Bignum fac = 1;
for (int i = 2; i <= n; i++)
    fac *= i;
return fac;
}
int main()
{
double t = factorial (30);
cout<<t<<endl;
}
agua from mars
  • 16,428
  • 4
  • 61
  • 70
Esraa
  • 1
  • 2
  • This? http://stackoverflow.com/questions/12323668/conversion-big-integer-double-in-c – Khalil Khalaf Apr 07 '16 at 12:09
  • Presumably your `Bignum` is trying to represent arbitrarily large values. The range of values a `double` can represent is finite. How do you expect the conversion to work if the `Bignum` is representing a value larger than a `double` can store? How do you expect multiplication of the values to then work? – Peter Apr 07 '16 at 12:12
  • For the operators I assume he means to have someone provide him with the operators for `Bignum` rather then double? Why not use gmp - `get_d()` is quite easy and well documented. – Floris Velleman Apr 07 '16 at 12:14
  • @peter, for my project double or say long double will be a good choice. for the operators, yes as Floris say i need them to do operations with two or more Bignums – Esraa Apr 07 '16 at 12:27
  • @Floris i can't install gmp as many as i try – Esraa Apr 07 '16 at 12:28
  • @Esraa Try searching google, there are a lot of people who have done this before ([I for a fact, although very crappy (personal use)](https://github.com/Futsy/Euler/blob/master/LargeInt/LargeInt.hpp)) - I just put all the numbers in a string and called `stod` while returning 0.0 if it fails. – Floris Velleman Apr 07 '16 at 12:30
  • Converting to `double` is going to lose all the extra precision that Bignum gives you. If you take this approach, you might as well just use doubles for everything. – pcarter Apr 07 '16 at 12:40
  • Have you tried `double operator()(const Bignum& num){return /*convert to double as you like*/; }`? – Teivaz Apr 07 '16 at 12:46

0 Answers0