Connecting Tech Pros Worldwide Forums | Help | Site Map

precision of a float

whatdoineed2do@yahoo.co.uk
Guest
 
Posts: n/a
#1: Oct 10 '07
#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


BobR
Guest
 
Posts: n/a
#2: Oct 10 '07

re: precision of a float



<whatdoineed2do@yahoo.co.ukwrote in message...
Quote:
#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


James Kanze
Guest
 
Posts: n/a
#3: Oct 11 '07

re: precision of a float


whatdoineed...@yahoo.co.uk wrote:
Quote:
#include <iostream>
using namespace std;
Quote:
void convert(const double& d)
{
cout << "before=" << d << " after=";
cout.precision(15);
}
Quote:
int main()
{
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}
Quote:
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:
Quote:
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:james.kanze@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

Closed Thread