I was just hoping to get some advice before I start writing my program, I need to put two columns of data into some sort of data structure, and each number has about 25 decimal places. Can anyone recommend the best one? Would storing them as a vector work?

Thanks in advance!

A vector probably would not be the best choice unless you create 2 parallel vectors and manage them accordingly.

A multi-dimensional array could work, but you would have to be careful about your capacity. You would have to use a high-precision dataType like a double . I don't know the precision of doubles right off hand though, I don't think it's high enough. You'd definitely be able to hold the information, but probably not with the needed precision. You may need to get a special library or define a custom type for that high precision.

Do you know anything about classes or structures? If so, you might be able to use a vector or a list that contains objects of a custom class or structure.

class myDataType {
  private:
    long double dataField1;
    long double dataField2;

    /* ... other datums and/or private methods... */

  public:
    myDataType(); /* default constructor */

    /* ... other constructors and public methods ... */
};

Check out the GMP for the level of precision that you need.

Thank you both for the replies.

I need use the table on http://www.holoborodko.com/pavel/?page_id=679

for values up to n = 10.

I have tried to put this into an array as below - is this OK or will this cause memory problems?

Thanks again!

int main()
{
    double nodesandweights [28][2];
    // n = 2
    nodesandweights [0][0] = 0.5773502691896257645091488;
    nodesandweights [0][1] = 1.0000000000000000000000000
    // n = 3
    nodesandweights [1][0] = 0;
    nodesandweights [1][1] = 0.8888888888888888888888889;
    nodesandweights [2][0] = 0.7745966692414833770358531;
    nodesandweights [2][1] = 0.5555555555555555555555556;
    // n = 4
    nodesandweights [3][0] = 0.3399810435848562648026658;
    nodesandweights [3][1] = 0.6521451548625461426269361;
    nodesandweights [4][0] = 0.8611363115940525752239465;
    nodesandweights [4][1] = 0.3478548451374538573730639;
    // n = 5
    nodesandweights [5][0] = 0;
    nodesandweights [5][1] = 0.5688888888888888888888889;
    nodesandweights [6][0] = 0.5384693101056830910363144;
    nodesandweights [6][1] = 0.4786286704993664680412915;
    nodesandweights [7][0] = 0.9061798459386639927976269;
    nodesandweights [7][1] = 0.2369268850561890875142640;
    // n = 6
    nodesandweights [8][0] = 0.2386191860831969086305017;
    nodesandweights [8][1] = 0.4679139345726910473898703;
    nodesandweights [9][0] = 0.6612093864662645136613996;
    nodesandweights [9][1] = 0.3607615730481386075698335;
    nodesandweights [10][0] = 0.9324695142031520278123016;
    nodesandweights [10][1] = 0.1713244923791703450402961;
    // n = 7
    nodesandweights [11][0] = 0;
    nodesandweights [11][1] = 0.4179591836734693877551020;
    nodesandweights [12][0] = 0.4058451513773971669066064;
    nodesandweights [12][1] = 0.3818300505051189449503698;
    nodesandweights [13][0] = 0.7415311855993944398638648;
    nodesandweights [13][1] = 0.2797053914892766679014678;
    nodesandweights [14][0] = 0.9491079123427585245261897;
    nodesandweights [14][1] = 0.1294849661688696932706114;
    // n = 8
    nodesandweights [15][0] = 0.1834346424956498049394761;
    nodesandweights [15][1] = 0.3626837833783619829651504;
    nodesandweights [16][0] = 0.5255324099163289858177390;
    nodesandweights [16][1] = 0.3137066458778872873379622;
    nodesandweights [17][0] = 0.7966664774136267395915539;
    nodesandweights [17][1] = 0.2223810344533744705443560;
    nodesandweights [18][0] = 0.9602898564975362316835609;
    nodesandweights [18][1] = 0.1012285362903762591525314;
    // n = 9
    nodesandweights [19][0] = 0;
    nodesandweights [19][1] = 0.3302393550012597631645251;
    nodesandweights [20][0] = 0.3242534234038089290385380;
    nodesandweights [20][1] = 0.3123470770400028400686304;
    nodesandweights [21][0] = 0.6133714327005903973087020;
    nodesandweights [21][1] = 0.2606106964029354623187429;
    nodesandweights [22][0] = 0.8360311073266357942994298;
    nodesandweights [22][1] = 0.1806481606948574040584720;
    nodesandweights [23][0] = 0.9681602395076260898355762;
    nodesandweights [23][1] = 0.0812743883615744119718922;
     // n = 10
    nodesandweights [24][0] = 0.1488743389816312108848260;
    nodesandweights [24][1] = 0.2955242247147528701738930;
    nodesandweights [25][0] = 0.4333953941292471907992659;
    nodesandweights [25][1] = 0.2692667193099963550912269;
    nodesandweights [26][0] = 0.6794095682990244062343274;
    nodesandweights [26][1] = 0.2190863625159820439955349;
    nodesandweights [27][0] = 0.8650633666889845107320967;
    nodesandweights [27][1] = 0.1494513491505805931457763;
    nodesandweights [28][0] = 0.9739065285171717200779640;
    nodesandweights [28][1] = 0.0666713443086881375935688;


    return 0;
}

Did you read the website's author's response to the first comment? He essentially answered your question about the precision. The step size (epsilon) from one double to another is on the order of 10^-16. Any decimal digits you have left over smaller than that are useless. He recommended GMP or another library for which he's written a wrapper So if you want the level of precision reflected in your current numbers that's what it will take.

I'm not very confident with classes, as I have only seen them once before, so I wanted to try the array method.

I have tried to use a class below, and tried to output a value from the table to see if it's working, but I'm getting NaN. Can someone please explain what I have done wrong?

Thanks.

#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>

using namespace std;

class tabledata {
    private:
        long double abscisass[28];
        long double weights[28];
    public:
        void set_values();
    } table;

void tabledata::set_values () {

    // n = 2
    abscisass[0] = 0.5773502691896257645091488;
    weights[0] = 1.0000000000000000000000000;
    // n = 3
    abscisass[1] = 0.0;
    weights[1] = 0.8888888888888888888888889;
    abscisass[2] = 0.7745966692414833770358531;
    weights[2] = 0.5555555555555555555555556;
    // n = 4
    abscisass[3] = 0.3399810435848562648026658;
    weights[3] = 0.6521451548625461426269361;
    abscisass[4] = 0.8611363115940525752239465;
    weights[4] = 0.3478548451374538573730639;
    // n = 5
    abscisass[5] = 0.0;
    weights[5] = 0.5688888888888888888888889;
    abscisass[6] = 0.5384693101056830910363144;
    weights[6] = 0.4786286704993664680412915;
    abscisass[7] = 0.9061798459386639927976269;
    weights[7] = 0.2369268850561890875142640;
    // n = 6
    abscisass[8] = 0.2386191860831969086305017;
    weights[8] = 0.4679139345726910473898703;
    abscisass[9] = 0.6612093864662645136613996;
    weights[9] = 0.3607615730481386075698335;
    abscisass[10] = 0.9324695142031520278123016;
    weights[10] = 0.1713244923791703450402961;
    // n = 7
    abscisass[11] = 0.0;
    weights[11] = 0.4179591836734693877551020;
    abscisass[12] = 0.4058451513773971669066064;
    weights[12] = 0.3818300505051189449503698;
    abscisass[13] = 0.7415311855993944398638648;
    weights[13] = 0.2797053914892766679014678;
    abscisass[14] = 0.9491079123427585245261897;
    weights[14] = 0.1294849661688696932706114;
    // n = 8
    abscisass[15] = 0.1834346424956498049394761;
    weights[15] = 0.3626837833783619829651504;
    abscisass[16] = 0.5255324099163289858177390;
    weights[16] = 0.3137066458778872873379622;
    abscisass[17] = 0.7966664774136267395915539;
    weights[17] = 0.2223810344533744705443560;
    abscisass[18] = 0.9602898564975362316835609;
    weights[18] = 0.1012285362903762591525314;
    // n = 9
    abscisass[19] = 0.0;
    weights[19] = 0.3302393550012597631645251;
    abscisass[20] = 0.3242534234038089290385380;
    weights[20] = 0.3123470770400028400686304;
    abscisass[21] = 0.6133714327005903973087020;
    weights[21] = 0.2606106964029354623187429;
    abscisass[22] = 0.8360311073266357942994298;
    weights[22] = 0.1806481606948574040584720;
    abscisass[23] = 0.9681602395076260898355762;
    weights[23] = 0.0812743883615744119718922;
     // n = 10
    abscisass[24] = 0.1488743389816312108848260;
    weights[24] = 0.2955242247147528701738930;
    abscisass[25] = 0.4333953941292471907992659;
    weights[25] = 0.2692667193099963550912269;
    abscisass[26] = 0.6794095682990244062343274;
    weights[26] = 0.2190863625159820439955349;
    abscisass[27] = 0.8650633666889845107320967;
    weights[27] = 0.1494513491505805931457763;
    abscisass[28] = 0.9739065285171717200779640;
    weights[28] = 0.0666713443086881375935688;

}

int main()
{
    long double abscisass[28], weights[28];

    tabledata table;
    table.set_values();

    cout << "the answer is " << setprecision(25) << weights[28];

    return 0;
}
Member Avatar for iamthwee

She reads but clearly it doesn't go in.

You can't store that big a number. You need to use a library.

DO YOU UNDERSTAND?

what I have done wrong?

To point out basic errors, see the comments below

#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>

using namespace std;

class tabledata {
    private:
        long double abscisass[28];
        long double weights[28];
    public:
        void set_values();
}  

// a global variable here, will you be using it? If not, delete it.
table;   

void tabledata::set_values () {

    // Both arrays are of size 28 -> the last index
    // you are allowed to use is 27, NOT 28

    abscisass[28] = 0.9739065285171717200779640;
    weights[28] = 0.0666713443086881375935688;
}

int main()
{
    // Here you have two uninitialized arrays
    long double abscisass[28], weights[28];

    // The variable name 'table' clashes with the one
    // you already have as a global variable, 
    // do something about it.
    tabledata table;
    table.set_values();

    // Here you illegally (again out-of-bounds) access the 
    // still-uninitialized 'weights' array local to this function.
    // Shouldn't you be using the 'table' instead?
    cout << "the answer is " << setprecision(25) << weights[28];

    return 0;
}

She reads but clearly it doesn't go in.

You can't store that big a number. You need to use a library.

DO YOU UNDERSTAND?

Sorry - but is there a need to be so rude?

Also I don't remember stating that I am female.

I am new to C++ and I'm trying my best, I'm not familiar with all the jargon so please excuse me if I am slow in understanding.

Thank you mitrmkar for you're constructive help.

Sorry - but is there a need to be so rude?

Also I don't remember stating that I am female.

I am new to C++ and I'm trying my best, I'm not familiar with all the jargon so please excuse me if I am slow in understanding.

Thank you mitrmkar for you're constructive help.

Being new, are you actually required to work with all 25 points of precision on those values? I did a little more research, the precision on a double , per the ANSI standard, is 10 points (Herbert Schildt The Complete C++ Reference p.15). However, 8-bytes is a common size for a modern double which allows them to store about 15 places of precision. If you need to work with all 25 points of precision provided in the table, you will have to use a high-precision library.

A library is an add-on that you can add to your program to expand your compiler's default capabilities. There is a link to one on the site you linked to and jonsca also gave you a link to a different one.

Thank you Fbody, I think I have a better understanding of what a library is now.

I have downloaded the library on the website I cited, although I'm a little unsure about how this should be used.

The website says:
"MPFR C++ consists of two files: mpreal.h and mpreal.cpp. In order to use MPFR C++ header file mpreal.h should be #included and mpreal.cpp should be compiled along with other source modules of the program."

So I have used #include "mpreal.h" in my original program, and copied and pasted the mpreal.cpp code into my program as well, is this correct?

Also to use this library, where I have used 'double' would I now replace this with 'mpreal'?

Thanks in advance!


Being new, are you actually required to work with all 25 points of precision on those values? I did a little more research, the precision on a double , per the ANSI standard, is 10 points (Herbert Schildt The Complete C++ Reference p.15). However, 8-bytes is a common size for a modern double which allows them to store about 15 places of precision. If you need to work with all 25 points of precision provided in the table, you will have to use a high-precision library.

A library is an add-on that you can add to your program to expand your compiler's default capabilities. There is a link to one on the site you linked to and jonsca also gave you a link to a different one.

Actually Fbody you are right, I just checked and I only need to use double precision, but thank you for all the help anyway.

Great, glad to hear it.

It's probably just as well. I started looking at the setup instructions for MPFR, what a nightmare...

I know haha, I've been looking into this stuff all day and it turns out it was all a waste of time! Really appreciate your efforts in helping me though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.