Samples

The first listing presents how to use ttmath::UInt<>. It creates three objects with 2 words each. On 32bit platform the max value which can be held is 2^(32*2)-1. Notice that we can initialise variables with string or if the value is small by using standard 'unsigned int' type.

#include <ttmath/ttmath.h>
#include <iostream>

int main()
{
ttmath::UInt<2> a,b,c;

    a = "1234";
    b = 3456; 
    c = a*b;

    std::cout << c << std::endl;
}

Listing nr 1

The second listing shows the ttmath::Int<> type. It is an integer type with a sign. The highest bit in the value tells us about the sign, if it is set the value is negative. The format of keeping values is the same as standard int type etc (two's complement). Values in this example can be from -2^(32*2-1) to 2^(32*2-1)-1 (on 32bit platform)

#include <ttmath/ttmath.h>
#include <iostream>

int main()
{
ttmath::Int<2> a,b,c;

    a = "-1234";
    b = 3456;
    c = a*b;

    std::cout << c << std::endl;
}

Listing nr 2

The last type in this library is ttmath::Big<>. This type holds floating point numbers.

#include <ttmath/ttmath.h>
#include <iostream>

int main()
{
// Big<exponent, mantissa>
ttmath::Big<1,2> a,b,c;

    a = "1234.3323454";
    b = "3456.1234534";
    c = a*b;

    std::cout << c << std::endl;
}

Listing nr 3

Standard manipulators such as std::setprecision(...) or std::setbase(...) etc are not supported at the moment. If you want to print a value in a different format use ttmath::Big<>::ToString(...) method instead.

With the library also goes a mathematical parser (listing nr 4). How does it work you can see on this website, the big online calculator uses it.

#include <ttmath/ttmath.h>
#include <iostream>

int main()
{
typedef ttmath::Big<1,3> MyBig;
ttmath::Parser<MyBig> parser;

    ttmath::ErrorCode err = parser.Parse(
                       " 123 - 432 * (12 / 3) ^ 54.34 - 10*pi ");

    if( err == ttmath::err_ok )
        std::cout << parser.stack[0].value << std::endl;
    else
        std::cout << "Error: "
                  << static_cast<int>(err)
                  << std::endl;
}

Listing nr 4

If you're using GCC you can compile above programs in this way:

gcc -o program -I/path/to/ttmath program.cpp

More samples you can find with the source code.