JoelMiller
09-09-2002, 09:54 AM
First, if anyone knows a clean way to add quadruple precision to numerical recipes in C++ please tell me (or I suppose I could even learn Fortran if that's easier).
I'm trying to change some calculations to quaduple precision. I expected it to be straightforward to redefine DP to be quadruple as opposed to double. So I downloaded a package from http://www.nersc.gov/~dhb/mpdist/mpdist.html and have successfully implemented simple calculations (e.g., 4*4 etc).
Here is a sample program, so you can see how the code works:
CODE:
**************************************
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "qd.h"
#include <cmath>
#include "x86.h"
using std::cout;
using std::cin;
using std::endl;
int main() {
unsigned short old_cw;
x86_fix_start(&old_cw);
qd_real a, b, c;
cout << "Enter a:";
cin >> a;
cout << "Enter b:";
cin >> b;
c = a + b;
qd_real asquared = a*a;
qd_real bsquared = sqr(b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "2ab = " << 2*a*b << endl;
cout << "a*a = " << asquared << endl;
cout << "b*b = " << bsquared << endl;
cout << "a*a-b*b = " << asquared -bsquared << endl;
cout << "a + b = " << c << endl;
cout << "sin(c) = " << sin(c) << endl;
x86_fix_end(&old_cw);
return 0;
}
********************************************
OUTPUT:
Enter a:5
Enter b:3
a = 5.000000000000000000000000000000000000000000000000 000000000000000E0
b = 3.000000000000000000000000000000000000000000000000 000000000000000E0
2ab = 3.000000000000000000000000000000000000000000000000 000000000000000E1
a*a = 2.500000000000000000000000000000000000000000000000 000000000000000E1
b*b = 9.000000000000000000000000000000000000000000000000 000000000000000E0
a*a-b*b = 1.600000000000000000000000000000000000000000000000 000000000000000E1
a + b = 8.000000000000000000000000000000000000000000000000 000000000000000E0
sin(c) = 9.893582466233817778081235982452886721164190808857 612628177152199E-1
*********************************************
I'm now trying to redefine 'DP' to be quadruple precision by changing the lines in nrtypes.h and nrutil.h mentioned on page 17 of the text to 'typedef qd_real DP;'
However, it is getting errors in compilation:
**********************************************
In file included from nrutil.h:1,
from nr.h:5,
from bsstep.cpp:2:
nrutil_nr.h:10: syntax error before `;'
nrutil_nr.h:416: `DP' was not declared in this scope
nrutil_nr.h:416: template argument 1 is invalid
nrutil_nr.h:416: ISO C++ forbids declaration of `cc_p' with no type
nrutil_nr.h:416: ISO C++ forbids declaration of `cr_p' with no type
etc.
********************************************
Line 10 of nrutil_nr.h was the line that was changed to 'typedef qd_real DP;'
I suspect that it has something to do with the fact that x86_fix_start and _end are in main. Somehow it doesn't know about qd_real when it goes through nr.h
Any ideas/suggestions would be greatly appreciated.
I'm trying to change some calculations to quaduple precision. I expected it to be straightforward to redefine DP to be quadruple as opposed to double. So I downloaded a package from http://www.nersc.gov/~dhb/mpdist/mpdist.html and have successfully implemented simple calculations (e.g., 4*4 etc).
Here is a sample program, so you can see how the code works:
CODE:
**************************************
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "qd.h"
#include <cmath>
#include "x86.h"
using std::cout;
using std::cin;
using std::endl;
int main() {
unsigned short old_cw;
x86_fix_start(&old_cw);
qd_real a, b, c;
cout << "Enter a:";
cin >> a;
cout << "Enter b:";
cin >> b;
c = a + b;
qd_real asquared = a*a;
qd_real bsquared = sqr(b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "2ab = " << 2*a*b << endl;
cout << "a*a = " << asquared << endl;
cout << "b*b = " << bsquared << endl;
cout << "a*a-b*b = " << asquared -bsquared << endl;
cout << "a + b = " << c << endl;
cout << "sin(c) = " << sin(c) << endl;
x86_fix_end(&old_cw);
return 0;
}
********************************************
OUTPUT:
Enter a:5
Enter b:3
a = 5.000000000000000000000000000000000000000000000000 000000000000000E0
b = 3.000000000000000000000000000000000000000000000000 000000000000000E0
2ab = 3.000000000000000000000000000000000000000000000000 000000000000000E1
a*a = 2.500000000000000000000000000000000000000000000000 000000000000000E1
b*b = 9.000000000000000000000000000000000000000000000000 000000000000000E0
a*a-b*b = 1.600000000000000000000000000000000000000000000000 000000000000000E1
a + b = 8.000000000000000000000000000000000000000000000000 000000000000000E0
sin(c) = 9.893582466233817778081235982452886721164190808857 612628177152199E-1
*********************************************
I'm now trying to redefine 'DP' to be quadruple precision by changing the lines in nrtypes.h and nrutil.h mentioned on page 17 of the text to 'typedef qd_real DP;'
However, it is getting errors in compilation:
**********************************************
In file included from nrutil.h:1,
from nr.h:5,
from bsstep.cpp:2:
nrutil_nr.h:10: syntax error before `;'
nrutil_nr.h:416: `DP' was not declared in this scope
nrutil_nr.h:416: template argument 1 is invalid
nrutil_nr.h:416: ISO C++ forbids declaration of `cc_p' with no type
nrutil_nr.h:416: ISO C++ forbids declaration of `cr_p' with no type
etc.
********************************************
Line 10 of nrutil_nr.h was the line that was changed to 'typedef qd_real DP;'
I suspect that it has something to do with the fact that x86_fix_start and _end are in main. Somehow it doesn't know about qd_real when it goes through nr.h
Any ideas/suggestions would be greatly appreciated.