precision of a float

W

whatdoineed2do

#include <iostream>
using namespace std;

void convert(const double& d)
{
cout << "before=" << d << " after=";
cout.precision(15);
cout << d << endl;
}



int main()
{
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}



hi,
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:

before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305

can you tell me what i'm doing wrong? been using Sun's Forte6
compiler
thanks
ray
 
B

BobR

#include <iostream>
using namespace std;

void convert(const double& d){
cout << "before=" << d << " after=";
cout.precision(15);
cout << d << endl;
}

int main(){
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}

hi,
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:

before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305

can you tell me what i'm doing wrong? been using Sun's Forte6
compiler
thanks
ray

What did you expect ( what you consider "wrong" )?
Perhaps you want to do ( inside "convert()" ):

cout.setf( std::ios_base::fixed );

..... or set some of the other flags.
 
J

James Kanze

#include <iostream>
using namespace std;
void convert(const double& d)
{
cout << "before=" << d << " after=";
cout.precision(15);
}
int main()
{
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:
before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305

That's what you asked for:). C++ formatting is defined in
terms of C's printf format specifiers: by default, floating
point values are formatted as "%g" (or more correctly, as
"%.<prec>g", where <prec> is the specified precision). And
according to the C standard, the "%g" format does not output
trailing 0's, regardless. If you output in scientific or fixed
format (corresponding to "%e" or "%f"), you should see them (but
I would not recommend fixed format for numbers like the second,
above). Otherwise (and you're going to love this), set the
showpoint formatting flag, e.g.:
std::cout.setf( std::ios::showpoint ) ;
The reason (for the name) is simple: C++ formatting is defined
in terms of C's printf, and the showpoint flag corresponds to
the '#' flag in printf: use alternative format. In the case of
"%#.0f", this means to output the point, whence the name. In
the case of "%#.<prec>g", it means to output the point AND any
trailing 0's. (Try outputting something like 12.0, and you'll
see the difference immediately: "12" without showpoint,
"12.0000" with, using the default precision of 6.)
 

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

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top