Connecting Tech Pros Worldwide Forums | Help | Site Map

converted long to double lost significant digits...

Jonny
Guest
 
Posts: n/a
#1: Oct 10 '05
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



Owen Jacobson
Guest
 
Posts: n/a
#2: Oct 10 '05

re: converted long to double lost significant digits...


On Mon, 10 Oct 2005 01:17:34 -0700, Jonny wrote:
[color=blue]
> 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?[/color]

Modified lines below.
[color=blue]
> #include<iostream>[/color]
#include <iomanip>[color=blue]
>
> using namespace std;
>
> int main () {
>
> long a = 1123456789;
>// float p = 100;[/color]
double p = 100;[color=blue]
>// double d = (double) (a / p) ;[/color]
double d = a / p;
[color=blue]
>// cout << "double = " << d << endl;[/color]
cout << "double = " << setprecision (10) << d << endl;
[color=blue]
> }[/color]

Useful info:

<http://cplusplus.com/ref/#libs>
<http://www.parashift.com/c++-faq-lite/>
Bob Hairgrove
Guest
 
Posts: n/a
#3: Oct 10 '05

re: converted long to double lost significant digits...


On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <jonyy@aol.com> wrote:
[color=blue]
>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
>[/color]

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

--
Bob Hairgrove
NoSpamPlease@Home.com
Owen Jacobson
Guest
 
Posts: n/a
#4: Oct 10 '05

re: converted long to double lost significant digits...


On Mon, 10 Oct 2005 10:36:33 +0200, Bob Hairgrove wrote:
[color=blue]
> On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <jonyy@aol.com> wrote:
>[color=green]
>>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?[/color][/color]

(Snip)
[color=blue][color=green]
>> long a = 1123456789;
>> float p = 100;
>> double d = (double) (a / p) ;[/color][/color]

(Snip)
[color=blue]
> Look for ios_base::precision, width, etc. in the STL documentation.[/color]

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
Kai-Uwe Bux
Guest
 
Posts: n/a
#5: Oct 10 '05

re: converted long to double lost significant digits...


Jonny wrote:
[color=blue]
> 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>[/color]

add:

#include <iomanip>
[color=blue]
>
> using namespace std;
>
> int main () {
>
> long a = 1123456789;
> float p = 100;
> double d = (double) (a / p) ;
>
> cout << "double = " << d << endl;[/color]

make that:

cout << "double = " << setprecision(10) << d << endl;
[color=blue]
> }[/color]

Best

Kai-Uwe Bux
Bob Hairgrove
Guest
 
Posts: n/a
#6: Oct 10 '05

re: converted long to double lost significant digits...


On Mon, 10 Oct 2005 08:46:34 GMT, Owen Jacobson
<angrybaldguy@google-email-service.example.com> wrote:
[color=blue]
>On Mon, 10 Oct 2005 10:36:33 +0200, Bob Hairgrove wrote:
>[color=green]
>> On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <jonyy@aol.com> wrote:
>>[color=darkred]
>>>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?[/color][/color]
>
>(Snip)
>[color=green][color=darkred]
>>> long a = 1123456789;
>>> float p = 100;
>>> double d = (double) (a / p) ;[/color][/color]
>
>(Snip)
>[color=green]
>> Look for ios_base::precision, width, etc. in the STL documentation.[/color]
>
>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[/color]

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

--
Bob Hairgrove
NoSpamPlease@Home.com
Walter Bright
Guest
 
Posts: n/a
#7: Oct 10 '05

re: converted long to double lost significant digits...



"Owen Jacobson" <angrybaldguy@google-email-service.example.com> wrote in
message
news:pan.2005.10.10.08.46.34.192947@google-email-service.example.com...[color=blue][color=green][color=darkred]
> >> long a = 1123456789;
> >> float p = 100;
> >> double d = (double) (a / p) ;[/color][/color]
> 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.[/color]

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


Closed Thread