Operating System - HP-UX
1829571 Members
1590 Online
109992 Solutions
New Discussion

Re: Double precision problems in C++ ( aCC compiler)

 
Haribaskar
Occasional Advisor

Double precision problems in C++ ( aCC compiler)

I have a C++ program, which needs to account for precision upto 16 digits.

I have aCC compiler (A. 3.33) in HP 11.0( 32 bit)
I faced the problem with compiler truncating some digits in decimals.

I wrote a sample program.

double d = 123456789.123456789123456789;
cout << setprecision(30) << d;

I got the program output as
123456789.12345679

I have read in some of the books that a double variable occypying 64 bits , can keep upto a maximum of 19 digits before decimal and 16 digits after the decimal. But, this does not seem to be happening.

Could somebody help me in solving this problem.?





4 REPLIES 4
Tomek Gryszkiewicz
Trusted Contributor

Re: Double precision problems in C++ ( aCC compiler)

i've tried the same in gcc... and the output was: 123456789.123456791
Seems like you reach some limits..

-Tomek
David Johns
Advisor

Re: Double precision problems in C++ ( aCC compiler)

Hello:

I think IEEE-754 is the standard for floating point, and you get 14 significant figures with double precision and 28 with long double. Below is an example of 28 sig. figures:

H:src:357$ cat test.C
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
long double ld = 0.9999999999999999999999999999l;


cout << setprecision(28) << 0.9999999999999999999999999999l << ' '
<< ld << endl;

return 0;
}

H:src:358$ aCC +DA2.0W -AA test.C
H:src:359$ ./a.out
0.9999999999999999999999999999 0.9999999999999999999999999999

Regards,
Dave
Antoniov.
Honored Contributor

Re: Double precision problems in C++ ( aCC compiler)

Hi Haribaskar,
IEEE standard has 3 types of floating point:
1) single precision figured on 32 bit
2) double precision figured on 64 bit
2) long double precision figured on 80 bit.
Notice IEEE doesn't refer to language but to CPU rappresentation.
The FP number has 2 section: the exponent and mantissa. Now I don't remember the the # of bits for every precision but I'm sure of limit:
1) single precision +/- 1.6 E30: you have 6 digits 1/2 of precision (for example 199999);
2) double precision +/- 1.9 E300: you have 18 digits 1/2 of precision (for example 1123456789012345678).
This means you can see any number of double precision with 18 digit plus digit 1 as most significative digit; also FP doesn't different digit before or after decimal point; so you can see 1.123456789012345678 or 11234567890.12345678 or 1123456789012345678 etc.
Also note you can't see follow 2123456789012345678 because 1st digit before 18 digits may be only 0 or 1.
IEEE can figure also NaN (invalid value) and infinite with double and long double precision; thees value are not avaiable in single precision.
At least, IEEE required compiler use long double in computate and result must be coonvert in less precision; this means avery compiler could convert any variable to long double, compute the result and reconvert to original precision.

H.T.H.
Antoniov
Antonio Maria Vigliotti
Haribaskar
Occasional Advisor

Re: Double precision problems in C++ ( aCC compiler)

Thanks for all inputs.

My special thanks to Anatov, which clarified my understanding about precision limits.

It looks like, I have to look for some other solutions to retain precision ( One solution could be , to use arbitary precision math library)

-Hari