By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

precision of a float

P: n/a
#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

Oct 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<wh************@yahoo.co.ukwrote in message...
#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.

--
Bob R
POVrookie
Oct 10 '07 #2

P: n/a
whatdoineed...@yahoo.co.uk wrote:
#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 <precis 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.)

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 11 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.