converted long to double lost significant digits...

J

Jonny

If you run the following code , you will get 1.12346e07 as the output. What
do I need to do if you want all the significant digits output, like
1.123456789e07?

#include<iostream>

using namespace std;

int main () {

long a = 1123456789;
float p = 100;
double d = (double) (a / p) ;

cout << "double = " << d << endl;

}

Thanks in advance..

-Jonny
 
O

Owen Jacobson

If you run the following code , you will get 1.12346e07 as the output. What
do I need to do if you want all the significant digits output, like
1.123456789e07?

Modified lines below.
#include<iostream>
#include said:
using namespace std;

int main () {

long a = 1123456789;
// float p = 100; double p = 100;
// double d = (double) (a / p) ;
double d = a / p;
// cout << "double = " << d << endl;
cout << "double = " << setprecision (10) << d << endl;

Useful info:

<http://cplusplus.com/ref/#libs>
<http://www.parashift.com/c++-faq-lite/>
 
B

Bob Hairgrove

If you run the following code , you will get 1.12346e07 as the output. What
do I need to do if you want all the significant digits output, like
1.123456789e07?

#include<iostream>

using namespace std;

int main () {

long a = 1123456789;
float p = 100;
double d = (double) (a / p) ;

cout << "double = " << d << endl;

}

Thanks in advance..

-Jonny

Look for ios_base::precision, width, etc. in the STL documentation.
 
K

Kai-Uwe Bux

Jonny said:
If you run the following code , you will get 1.12346e07 as the output.
What do I need to do if you want all the significant digits output, like
1.123456789e07?

#include<iostream>

add:

#include said:
using namespace std;

int main () {

long a = 1123456789;
float p = 100;
double d = (double) (a / p) ;

cout << "double = " << d << endl;

make that:

cout << "double = " << setprecision(10) << d << endl;

Best

Kai-Uwe Bux
 
O

Owen Jacobson

(Snip)

(Snip)

Look for ios_base::precision, width, etc. in the STL documentation.

The other problem is the intermediate converstion to float during the
evaluation of (a / p), prior to conversion to double... I'm not
comfortable enough with FP math to say for certain that that'll lose
digits in this case, but it may well.

-O
 
B

Bob Hairgrove

The other problem is the intermediate converstion to float during the
evaluation of (a / p), prior to conversion to double... I'm not
comfortable enough with FP math to say for certain that that'll lose
digits in this case, but it may well.

-O

Good catch ... I overlooked the fact that p is float, not double.
 
W

Walter Bright

message
The other problem is the intermediate converstion to float during the
evaluation of (a / p), prior to conversion to double... I'm not
comfortable enough with FP math to say for certain that that'll lose
digits in this case, but it may well.

A 32 bit float only has 24 bits for the mantissa, and a 32 bit long won't
fit without losing a few. However, an implementation is allowed to evaluate
(a/p) as a double internally, or even long double, so it may work anyway.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top