Question about printing double



 DEVELOP > c-Plus-Plus > Question about printing double

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Aman JIANG"
Date: 26 Sep 2007 02:17:59 AM
Object: Question about printing double
1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?
2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?
thanks.
.

User: "Kai-Uwe Bux"

Title: Re: Question about printing double 26 Sep 2007 02:41:36 AM
Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?

What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?

A bug?
Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.
Best
Kai-Uwe Bux
.
User: "Aman JIANG"

Title: Re: Question about printing double 26 Sep 2007 06:29:45 AM
On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...

The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?

Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.

I'll show you a simple one:
void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}
.
User: "Kai-Uwe Bux"

Title: Re: Question about printing double 26 Sep 2007 01:45:18 PM
Aman JIANG wrote:

On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect
that the numbers were too large and issue an error message? did the
program crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.

No, I can't. I don't have XP. But I have g++ on Linux. With the code you
provided and g++ version 4.1.1,I get
1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000
from:
#include <iostream>
#include <cstdio>
using namespace std;
int main ( void ) {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}
I cannot find a problem.

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?

Anyway, post some code that exhibits the problem. Maybe your test code
has undefined behavior. If not, your should contact the vendors of your
libraries.


I'll show you a simple one:

void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}

Maybe, there is a bug elsewhere in your code. Instead of giving us a
function, you should follow the FAQ on how-to-post and provide a _complete_
compilable example (e.g., there should be a main function somewhere in your
code).
Best
Kai-Uwe Bux
.
User: "duane hebert"

Title: Re: Question about printing double 26 Sep 2007 02:38:21 PM
"Kai-Uwe Bux" <jkherciueh@gmx.net> wrote in message
news:fde9cf$eo8$1@murdoch.acc.Virginia.EDU...

Aman JIANG wrote:

On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect
that the numbers were too large and issue an error message? did the
program crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


No, I can't. I don't have XP. But I have g++ on Linux. With the code you
provided and g++ version 4.1.1,I get

1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000

from:

#include <iostream>
#include <cstdio>

using namespace std;

int main ( void ) {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}


I cannot find a problem.

If the OP is trying to run his code directly from Xp he
needs to open a command prompt and run it from
there. If he runs it from the gui, it's going to close the
temp command prompt before he can see the cout.
Or after the cout/printf add:
std::system("pause");
Perhaps that's the problem.
.

User: "Victor Bazarov"

Title: Re: Question about printing double 26 Sep 2007 02:38:52 PM
Kai-Uwe Bux wrote:

Aman JIANG wrote:

On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler
detect that the numbers were too large and issue an error message?
did the program crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


No, I can't. I don't have XP. But I have g++ on Linux. With the code
you provided and g++ version 4.1.1,I get

1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000

from:

#include <iostream>
#include <cstdio>

using namespace std;

int main ( void ) {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}


I cannot find a problem.

Same code, on VC++ v8 on XP:
1234567889999999900000000000000000000000000000.000000
1234567890000000000000000000000000000000000000.000000

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?

Probably.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "Aman JIANG"

Title: Re: Question about printing double 27 Sep 2007 12:12:46 AM
On Sep 27, 3:38 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Kai-Uwe Bux wrote:

Aman JIANG wrote:


On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler
detect that the numbers were too large and issue an error message?
did the program crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


No, I can't. I don't have XP. But I have g++ on Linux. With the code
you provided and g++ version 4.1.1,I get


1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000


from:


#include <iostream>
#include <cstdio>


using namespace std;


int main ( void ) {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}


I cannot find a problem.


Same code, on VC++ v8 on XP:

1234567889999999900000000000000000000000000000.000000
1234567890000000000000000000000000000000000000.000000

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?


Probably.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Okay... what about this one, the whole program:
#include <iostream>
#include <limits>
#include <cstdio>
using namespace std;
void f() {
double value = numeric_limits<double>::max();
for (int i = 0; i < 7; ++i) {
cout << "++++++++++++++++++++" << endl;
cout << fixed << value << endl;
printf("%f\n", value);
for (int i = 0; i < 20; ++i)
value *= 0.01;
}
}
int main()
{
f();
}
My result was:
++++++++++++++++++++
17976931348623161000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623157000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
++++++++++++++++++++
17976931348623163000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000.000000
17976931348623169000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000.000000
++++++++++++++++++++
17976931348623163000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623172000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
++++++++++++++++++++
17976931348623183000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000.000000
17976931348623180000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000.000000
++++++++++++++++++++
17976931348623183000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623182000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
++++++++++++++++++++
17976931348623189000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000.000000
17976931348623190000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000.000000
++++++++++++++++++++
179769313486232020000000000000000000000000000000000000000000000000000.000000
179769313486231990000000000000000000000000000000000000000000000000000.000000
And what's yours, please ?
.
User: "Kai-Uwe Bux"

Title: Re: Question about printing double 27 Sep 2007 12:35:46 AM
Aman JIANG wrote:

On Sep 27, 3:38 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Kai-Uwe Bux wrote:

Aman JIANG wrote:


On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler
detect that the numbers were too large and issue an error message?
did the program crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


No, I can't. I don't have XP. But I have g++ on Linux. With the code
you provided and g++ version 4.1.1,I get


1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000


from:


#include <iostream>
#include <cstdio>


using namespace std;


int main ( void ) {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}


I cannot find a problem.


Same code, on VC++ v8 on XP:

1234567889999999900000000000000000000000000000.000000
1234567890000000000000000000000000000000000000.000000

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?


Probably.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Okay... what about this one, the whole program:

#include <iostream>
#include <limits>
#include <cstdio>
using namespace std;

void f() {
double value = numeric_limits<double>::max();
for (int i = 0; i < 7; ++i) {
cout << "++++++++++++++++++++" << endl;
cout << fixed << value << endl;
printf("%f\n", value);
for (int i = 0; i < 20; ++i)
value *= 0.01;
}
}

Ok. One possibility is that you are seeing artifacts from compiler
optimization. The C++ standard permits floats to be represented with higher
precision in CPU registers. Only when written to memory are those excess
digits lost. If the compiler optimizes away some writes to memory, it might
appear that the value of the variable changes magically between two points
of access.
Try declaring
double volatile value
(I don't know if that helps, but I think it might).
Also, your compiler probably has some switches that controll optimization f
floating point arithmetic.

int main()
{
f();
}


My result was:

++++++++++++++++++++

17976931348623161000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


000000000000000000000000000000000000000000000000000000000000000000000.000000


17976931348623157000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


000000000000000000000000000000000000000000000000000000000000000000000.000000

++++++++++++++++++++

17976931348623163000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000.000000

17976931348623169000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000.000000
++++++++++++++++++++

17976931348623163000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


000000000000000000000000000000000000000000000000000000000000000000000.000000


17976931348623172000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000


000000000000000000000000000000000000000000000000000000000000000000000.000000

++++++++++++++++++++

17976931348623183000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000.000000

17976931348623180000000000000000000000000000000000000000000000000000000000000000


00000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000.000000
++++++++++++++++++++

17976931348623183000000000000000000000000000000000000000000000000000000000000000


000000000000000000000000000000000000000000000000000000000000000000000.000000


17976931348623182000000000000000000000000000000000000000000000000000000000000000


000000000000000000000000000000000000000000000000000000000000000000000.000000

++++++++++++++++++++

17976931348623189000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000.000000

17976931348623190000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000.000000
++++++++++++++++++++

179769313486232020000000000000000000000000000000000000000000000000000.000000


179769313486231990000000000000000000000000000000000000000000000000000.000000



And what's yours, please ?

++++++++++++++++++++
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
++++++++++++++++++++
17976931348623168803535103715102371936956876099477027021416896665729095562501716782953112357749291337742947707592239815900387202653398199401715793096540036137152752946476437620203845856055675848407785668656604362475865354513858532014143250805428020442630978199812571136.000000
17976931348623168803535103715102371936956876099477027021416896665729095562501716782953112357749291337742947707592239815900387202653398199401715793096540036137152752946476437620203845856055675848407785668656604362475865354513858532014143250805428020442630978199812571136.000000
++++++++++++++++++++
1797693134862317184978654101495845578779892617003613451168130107163462505465704834010736734883887699948226798144570928273041617655791767616517167041227701263810207156575533830526391672376373525085234753899083930760905818275577856.000000
1797693134862317184978654101495845578779892617003613451168130107163462505465704834010736734883887699948226798144570928273041617655791767616517167041227701263810207156575533830526391672376373525085234753899083930760905818275577856.000000
++++++++++++++++++++
179769313486231803341211308005127056643265996593230959639103615616458217929317526094395659181142238779925433505180572176304271562985878751258358863773650012083645864786519533819782081019904.000000
179769313486231803341211308005127056643265996593230959639103615616458217929317526094395659181142238779925433505180572176304271562985878751258358863773650012083645864786519533819782081019904.000000
++++++++++++++++++++
17976931348623181586724979444854831637051379627365101465547414735694488826595397027683444317245857621991662199371641181642702350033510153168128835584.000000
17976931348623181586724979444854831637051379627365101465547414735694488826595397027683444317245857621991662199371641181642702350033510153168128835584.000000
++++++++++++++++++++
1797693134862319004438329439553869588098858202786180889326404644101794303230278148231539669010545381356863488.000000
1797693134862319004438329439553869588098858202786180889326404644101794303230278148231539669010545381356863488.000000
++++++++++++++++++++
179769313486231992335343347578535516032311582050772848940056867504128.000000
179769313486231992335343347578535516032311582050772848940056867504128.000000
Best
Kai-Uwe Bux
.
User: "Pete Becker"

Title: Re: Question about printing double 27 Sep 2007 06:50:23 AM
On 2007-09-27 01:35:46 -0400, Kai-Uwe Bux <jkherciueh@gmx.net> said:

179769313486231992335343347578535516032311582050772848940056867504128.000000
179769313486231992335343347578535516032311582050772848940056867504128.000000

Also known as "false precision".
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
.
User: "James Kanze"

Title: Re: Question about printing double 28 Sep 2007 02:25:01 AM
On Sep 27, 1:50 pm, Pete Becker <p...@versatilecoding.com> wrote:

On 2007-09-27 01:35:46 -0400, Kai-Uwe Bux <jkherci...@gmx.net> said:

179769313486231992335343347578535516032311582050772848940056867504128.0=

00000

179769313486231992335343347578535516032311582050772848940056867504128.0=

00000

Also known as "false precision".

Or garbage. The real problem, IMHO, is in the orginal request.
Asking for more than 300 digits of a floating point number, when
(on most machines), it only has 17 digits precision. In a very
real sense, anything beyond the first 17 digits is garbage, and
anything the compiler outputs is "correct". (Practically
speaking, of course, outputting 0's is the best solution,
because it does look like the rounding it is. But I have my
doubts about a program which requests such a thing to begin
with.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "Pete Becker"

Title: Re: Question about printing double 28 Sep 2007 07:42:30 AM
On 2007-09-28 03:25:01 -0400, James Kanze <james.kanze@gmail.com> said:

On Sep 27, 1:50 pm, Pete Becker <p...@versatilecoding.com> wrote:

On 2007-09-27 01:35:46 -0400, Kai-Uwe Bux <jkherci...@gmx.net> said:


179769313486231992335343347578535516032311582050772848940056867504128.0

00000

179769313486231992335343347578535516032311582050772848940056867504128.0

00000

Also known as "false precision".


Or garbage.

"False precision" is the polite term. <g> Like when someone converts
"about an inch" to "about 2.54 centimeters", making the value look much
more precise than it actually is.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
.






User: "Aman JIANG"

Title: Re: Question about printing double 27 Sep 2007 12:06:40 AM
On Sep 27, 2:45 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect
that the numbers were too large and issue an error message? did the
program crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


No, I can't. I don't have XP. But I have g++ on Linux. With the code you
provided and g++ version 4.1.1,I get

1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000

from:

#include <iostream>
#include <cstdio>

using namespace std;

int main ( void ) {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);

}

I cannot find a problem.



2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?


Anyway, post some code that exhibits the problem. Maybe your test code
has undefined behavior. If not, your should contact the vendors of your
libraries.


I'll show you a simple one:


void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}


Maybe, there is a bug elsewhere in your code. Instead of giving us a
function, you should follow the FAQ on how-to-post and provide a _complete_
compilable example (e.g., there should be a main function somewhere in your
code).

Best

Kai-Uwe Bux

I have no idea ... that you said you donnot have a XP system,
but this problem is just depend on OS and compiler.
.
User: "James Kanze"

Title: Re: Question about printing double 28 Sep 2007 02:30:26 AM
On Sep 27, 7:06 am, Aman JIANG <AmanJI...@gmail.com> wrote:

On Sep 27, 2:45 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

[...]

I have no idea ... that you said you donnot have a XP system,
but this problem is just depend on OS and compiler.

The problem depends on the implementation of the library, and
perhaps on the compiler (or the compiler flags) with which it
was compiled.
In many cases, different parts of the library have difference
sources. On most Unix systems, for example, g++ will use
libstdc++ (it's own implementation) for the iostream's, but will
use the implementation in libc (bundled with the system) for the
printf family---given the irrelevance of the digits you're
asking for, and the anomalies which may be associated with
floating point rounding, it's not too surprising that the
results aren't exactly the same. Rounded to 17 significant
digits, they should be the same, but beyond that, it's really
just noise. (I'm not sure what the situation is with VC++. I
know that Dinkumware provides the C++ library, but I wouldn't be
surprised if the printf stuff were some legacy Microsoft code.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



User: "John Bode"

Title: Re: Question about printing double 26 Sep 2007 06:38:48 AM
On Sep 26, 6:29 am, Aman JIANG <AmanJI...@gmail.com> wrote:

On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?


Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.


I'll show you a simple one:

void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);

}

FWIW, I had no problems. Here's my code:
#include <iostream>
#include <iomanip>
#include <cstdio>
int main(void)
{
double value = 1.23456789e45;
std::cout << std::fixed << value << std::endl;
std::cout << value << std::endl;
printf("%f\n", value);
return 0;
}
And here's the output:
1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000
This was built using g++ on Mac OS X.
There must be something else going on in your code.
.
User: "Aman JIANG"

Title: Re: Question about printing double 26 Sep 2007 07:44:30 AM
On Sep 26, 7:38 pm, John Bode <john_b...@my-deja.com> wrote:

On Sep 26, 6:29 am, Aman JIANG <AmanJI...@gmail.com> wrote:



On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:


Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?


Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.


I'll show you a simple one:


void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);


}


FWIW, I had no problems. Here's my code:

#include <iostream>
#include <iomanip>
#include <cstdio>

int main(void)
{
double value = 1.23456789e45;
std::cout << std::fixed << value << std::endl;
std::cout << value << std::endl;
printf("%f\n", value);
return 0;

}

And here's the output:

1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000

This was built using g++ on Mac OS X.

There must be something else going on in your code.

a) My example codes before was just for vc08, not g++.
b) And that my first question which about g++ was just for
XP system, not Mac OS X ... ...
.

User: "Aman JIANG"

Title: Re: Question about printing double 26 Sep 2007 07:45:09 AM
On Sep 26, 7:38 pm, John Bode <john_b...@my-deja.com> wrote:

On Sep 26, 6:29 am, Aman JIANG <AmanJI...@gmail.com> wrote:



On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:


Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.


2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?


Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.


I'll show you a simple one:


void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);


}


FWIW, I had no problems. Here's my code:

#include <iostream>
#include <iomanip>
#include <cstdio>

int main(void)
{
double value = 1.23456789e45;
std::cout << std::fixed << value << std::endl;
std::cout << value << std::endl;
printf("%f\n", value);
return 0;

}

And here's the output:

1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000
1234567889999999964160667452302023944549957632.000000

This was built using g++ on Mac OS X.

There must be something else going on in your code.

a) My example codes before was just for vc08, not g++.
b) And that my first question which about g++ was just for
XP system, not Mac OS X ... ...
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER