How do I write an artibrary precision datatype?

I want to write my own integer data type that has arbitrary precision. Where do I start and what prerequisites might I need to know?

After integer I want to do floating point.

I also want to support operations like orders (a^2 = a*a) etc.

__________

I know nothing about how to or make datatypes. So a link for that would also be helpful.
Last edited on
Start with a very simple integer implementation: say, a vector where each element is one decimal digit.
Start by implementing basic arithmetic operations like addition, multiplication etc. exactly as you would do them using paper and pencil.

Once you have got the basics working correctly, start looking for more efficient representations and algorithms:
eg. Karatsuba multiplication: https://en.wikipedia.org/wiki/Karatsuba_algorithm
How do I define a datatype so that I can use it like a regular data type?
Just like how you can do int foo = 5; how can I make it so that I can do Arbitrary_int foo = 5;?

So I use an array of character is it? That's okay memory usage wise? Do I have to use pointers anywhere?
Last edited on
Use a vector to hold the digits. One thing that will really help is to store the least significant digit first, so for the number 123, digits[0]==3, digits[1]==2 and digits[2]==1. This makes the code much simpler when you're dealing with numbers of different lengths.
> Just like how you can do int foo = 5; how can I make it so that I can do Arbitrary_int foo = 5;?

Write converting constructors. https://en.cppreference.com/w/cpp/language/converting_constructor

For example:

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
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>

struct integer {

    // construct from string of digits (converting constructor)
    integer( std::string str ) : digits( str.begin(), str.end() ) {

        for( int& d : digits ) if( !std::isdigit(d) ) throw "invalid arg" ; else d -= '0' ;
    }

    // construct from unsigned long long (converting constructor)
    integer( unsigned long long n ) : integer( std::to_string(n) ) {}

    // construct from c-style string (converting constructor)
    integer( const char* cstr = 0 ) : integer( cstr ? std::string(cstr) : "0" ) {}

    private: std::vector<int> digits ;

    friend std::ostream& operator<< ( std::ostream& stm, const integer& i ) {

        for( int v : i.digits ) stm << char( v + '0' ) ;
        return stm ;
    }
};

int main() {

    const integer i = 123456789012345 ;
    std::cout << "i == " << i << '\n' ;

    const integer j = "99999888888888877777777777666666655555555555544444" ;
    std::cout << "j == " << j << '\n' ;

    const integer k ;
    std::cout << "k == " << k << '\n' ;
}

http://coliru.stacked-crooked.com/a/f6797b8772f17d7d


> One thing that will really help is to store the least significant digit first,

This is a good idea. Note that this has not been done in the trivial example above.
closed account (z05DSL3A)
This might be worth a read...
Bignum Challange
http://www.cplusplus.com/forum/lounge/32041/
Topic archived. No new replies allowed.