divide !

May 10, 2010 at 9:37am
Hello.
I am writing a program that can do processes on huge numbers with floating point.
but I have a problem.I can't write divide function (I am overloading '/').
I don't know it's algorithm.
Can anybody help me?
Thanks :)
May 10, 2010 at 11:04am
Here is the algorithm for long division:

http://en.wikipedia.org/wiki/Long_division
May 10, 2010 at 11:05am
Here is another, more basic, example:

http://www.mathsisfun.com/long_division.html
May 11, 2010 at 12:52pm
Thanks. but I need to divide two numbers which have floating point.
what should I do?
May 11, 2010 at 1:12pm
Some of those algorithms include dealing with the floating point.

I think you will need to decide exactly how you are going to represent your large decimals.

Maybe you could define a type like this:

1
2
3
4
class decimal_t
{

};


It will need a list of numbers before the decimal point and a list of numbers after the decimal point:


1
2
3
4
5
6
7
#include <vector>

class decimal_t
{
    std::vector<int> before;
    std::vector<int> after;
};



Then you can think about overloading the operators to make it work lika a large decimal:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vector>
#include <sstream>

class decimal_t
{
    std::vector<int> before;
    std::vector<int> after;
public:
    decimal_t(double d)
    {
        // convert double into a list of ints before and after the decimal point.
    }

    decimal_t operator+(const decomal_t& rhs)
    {
        decimal_t d;

        // add rhs to this object

        return d;
    }

    decimal_t operator/(const decomal_t& rhs)
    {
        decimal_t d;

        // divide this object by rhs

        return d;
    }
};

May 11, 2010 at 11:50pm
No.
I don't do that. I show numbers like " a.bc... * 10^n " .
abc... is an array of unsigned characters and n is an integer.
this is too easier and occupys less memory.
If you want to know more I will tell you :)
May 12, 2010 at 2:15am
Ideally, you'd have a system completely in binary and that would save a lot of space. However...

Use unsigned 64-bit integers instead of what you have now? That would make your computations a lot easier, and still permit you a lot space.

Assuming your method, and assuming this problem:

1
2
 decmial_t a, b;
a/b;


In your operator, computer the resulting power of ten separate of the division of the other numbers. Remember: a^n/a^m = a^(n-m).

Of course, if you want to do this digit by digit, you're free to do so, but expect a lot of tedium.

-Albatross


May 13, 2010 at 11:48am
My compiler is almost old and my computer and windows are not 64-bit.
Can I do that (creating 64-bit integer) with my existing tools? If yes how?
Topic archived. No new replies allowed.