Velocity Reviews > C++ > converted long to double lost significant digits...

# converted long to double lost significant digits...

Jonny
Guest
Posts: n/a

 10-10-2005
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;

}

-Jonny

Owen Jacobson
Guest
Posts: n/a

 10-10-2005
On Mon, 10 Oct 2005 01:17:34 -0700, Jonny wrote:

> 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 <iomanip>
>
> 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/>

Bob Hairgrove
Guest
Posts: n/a

 10-10-2005
On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <(E-Mail Removed)> wrote:

>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;
>
>}
>
>
>-Jonny
>

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

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)

Kai-Uwe Bux
Guest
Posts: n/a

 10-10-2005
Jonny wrote:

> 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>

#include <iomanip>

>
> 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

Owen Jacobson
Guest
Posts: n/a

 10-10-2005
On Mon, 10 Oct 2005 10:36:33 +0200, Bob Hairgrove wrote:

> On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <(E-Mail Removed)> wrote:
>
>>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?

(Snip)

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

(Snip)

> Look for ios_base:recision, 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

Bob Hairgrove
Guest
Posts: n/a

 10-10-2005
On Mon, 10 Oct 2005 08:46:34 GMT, Owen Jacobson
<(E-Mail Removed)> wrote:

>On Mon, 10 Oct 2005 10:36:33 +0200, Bob Hairgrove wrote:
>
>> On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <(E-Mail Removed)> wrote:
>>
>>>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?

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

>
>(Snip)
>
>> Look for ios_base:recision, 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

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

--
Bob Hairgrove
(E-Mail Removed)

Walter Bright
Guest
Posts: n/a

 10-10-2005

"Owen Jacobson" <(E-Mail Removed)> wrote in
message
news(E-Mail Removed)...
> >> long a = 1123456789;
> >> float p = 100;
> >> double d = (double) (a / p) ;

> 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