Number of zero in exponent (e.g scientific)

M

mathieu

Hi,

I am looking at the following example (*). Some compile seems to
output either

3.1416 2006 1e-010
3.14159e+000 2.00600e+003 1.00000e-010

or

3.1416 2006 1e-10
3.14159e+00 2.00600e+03 1.00000e-10

How do I check how many number of zero will be printed ? Or how do I
say "only use 2 digits" when exponent is < 100.

Thanks !

(*)
#include <iostream>
using namespace std;

int main () {
double a,b,c;
a = 3.1415926534;
b = 2006.0;
c = 1.0e-10;
cout.precision(5);
cout << a << '\t' << b << '\t' << c << endl;
cout << scientific << a << '\t' << b << '\t' << c << endl;
return 0;
}
 
V

Victor Bazarov

mathieu said:
I am looking at the following example (*). Some compile seems to
output either

3.1416 2006 1e-010
3.14159e+000 2.00600e+003 1.00000e-010

or

3.1416 2006 1e-10
3.14159e+00 2.00600e+03 1.00000e-10

How do I check how many number of zero will be printed ? Or how do I
say "only use 2 digits" when exponent is < 100.

Thanks !

(*)
#include <iostream>
using namespace std;

int main () {
double a,b,c;
a = 3.1415926534;
b = 2006.0;
c = 1.0e-10;
cout.precision(5);
cout << a << '\t' << b << '\t' << c << endl;
cout << scientific << a << '\t' << b << '\t' << c << endl;
return 0;
}

It's come up before. Try searching in the archives. IIRC, there is a
defect in Visual C++ standard C library that makes it print 3 zeros
instead of only 2 (as specified by the C standard). As to your
question, no, it's not configurable. You can do it yourself by
outputting to a string and then adding (or removing) a zero to the
exponent based on your setting.

V
 
M

mathieu

It's come up before.  Try searching in the archives.  IIRC, there is a
defect in Visual C++ standard C library that makes it print 3 zeros
instead of only 2 (as specified by the C standard).  As to your
question, no, it's not configurable.  You can do it yourself by
outputting to a string and then adding (or removing) a zero to the
exponent based on your setting.

Ok sorry I found -at least- one previous post mentioning the issue.

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/624b679a4faf03d

Does anyone knows of the portable 'printf' function then ?


Thanks
 
M

Michael Doubez

Ok sorry I found -at least- one previous post mentioning the issue.

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/624...

Does anyone knows of the portable 'printf' function then ?

They are all portable. It is your responsibility to specify the format
you want:
* fix : writes values in fixed-point notation (without exponent)
* scientific : writes values with one digit before the decimal point
and an exponent
* (default): uses fix or scientific depending on the range

You can use the std::fix, std::scientific maninpulators to choose the
format or unsetf(ios_base::floatfield) to reset it.
With printf, it corresponds respectively to the %f,%e and %g format
specifiers.

Once you have chosen the format you need to choose the precision.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top