Multiple-Precision list of mpfs

Herr Witten lingwitt at bellsouth.net
Wed Feb 25 22:17:27 CET 2004


An array could be used instead of a vector, as illustrated earlier.

#include <iostream>
#include <vector.h>
#include <stdio.h>
#include <gmp.h>

class numbers
{
     static std::vector<mpf_ptr> _numbers;

public:
     static mpf_ptr getNumber()
{
         static int precision = 0;

         mpf_ptr number = 0;

         if (_numbers.empty())
         {
             number = new __mpf_struct;
             mpf_init2(number, precision += 100000);
         }
         else
         {
             number = _numbers[0];
             _numbers.erase(_numbers.begin(), _numbers.begin() + 1);
         }

         mpf_set_d(number, 0);

         return number;
}

static void returnNumber(mpf_ptr number)
{
     _numbers.push_back(number);
}

static void cleanup()
{
     _numbers.clear();
}
};

std::vector<mpf_ptr> numbers::_numbers;

int main (int argc, char * const argv[]) {

     for (int count = 0; count < 100; count++)
     {
         mpf_ptr number1 = numbers::getNumber();
         mpf_ptr number2 = numbers::getNumber();
         mpf_ptr number3 = numbers::getNumber();

         mpf_set_d(number1, count);
         mpf_set_d(number2, 2);

         mpf_mul(number3, number1, number2);

         printf("\n");
         mpf_out_str(stdout, 10, 10, number3);

         numbers::returnNumber(number1);
         numbers::returnNumber(number2);
         numbers::returnNumber(number3);
     }

     printf("\n");

     mpf_ptr number1 = numbers::getNumber();
     mpf_ptr number2 = numbers::getNumber();
     mpf_ptr number3 = numbers::getNumber();

     cout << mpf_get_prec(number1) << endl;
     cout << mpf_get_prec(number2) << endl;
     cout << mpf_get_prec(number3) << endl;

     numbers::returnNumber(number1);
     numbers::returnNumber(number2);
     numbers::returnNumber(number3);

     numbers::cleanup();

     return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 3721 bytes
Desc: not available
Url : /list-archives/gmp-discuss/attachments/20040225/73800aaf/attachment.bin


More information about the gmp-discuss mailing list