Question about printing double

A

Aman JIANG

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

Kai-Uwe Bux

Aman said:
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
 
Joined
Sep 26, 2007
Messages
6
Reaction score
0
Kai-Uwe Bux said:
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

Do u mean in C++?
Kai is right in VC it will show an error message if the numbers too large.
The result in cout and printf is same, but cout will show it as a number with e.
ex : 1.234567e+011

note : I found trouble if you use too large number, printf won't work well.
Anyway why u use printf in C++ environment?
 
A

Aman JIANG

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.
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);
}
 
J

John Bode

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.




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

Aman JIANG

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

Aman JIANG

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

Kai-Uwe Bux

Aman said:
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.

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
 
D

duane hebert

Kai-Uwe Bux said:
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.
 
V

Victor Bazarov

Kai-Uwe Bux said:
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

Probably.

V
 
A

Aman JIANG

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.






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

Aman JIANG

Same code, on VC++ v8 on XP:

1234567889999999900000000000000000000000000000.000000
1234567890000000000000000000000000000000000000.000000


Probably.

V

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

Kai-Uwe Bux

Aman said:
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
17976931348623169000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
17976931348623163000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623172000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623183000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
17976931348623180000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
17976931348623183000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623182000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000.000000
17976931348623189000000000000000000000000000000000000000000000000000000000000000
17976931348623190000000000000000000000000000000000000000000000000000000000000000
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
 
P

Pete Becker

179769313486231992335343347578535516032311582050772848940056867504128.000000
179769313486231992335343347578535516032311582050772848940056867504128.000000

Also known as "false precision".
 
J

James Kanze

On 2007-09-27 01:35:46 -0400, Kai-Uwe Bux <[email protected]> said:

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.)
 
J

James Kanze

On Sep 27, 2:45 am, Kai-Uwe Bux <[email protected]> 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.)
 
P

Pete Becker

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top