behaviour of setprecision(0)

J

jacek.dziedzic

Hello!

Can someone tell me what the expected output of the following
program is?

#include <fstream>
#include <iomanip>

using namespace std;

int main() {
ofstream o("o");
o << fixed << setprecision(0) << 13.0 << endl;
}

Depending on which STL implementation I use with my compiler, I
either get
"13" or "13.000000".

I don't have the Standard (at all) or the Josuttis book (with me),
so can anyone
shed some light on what setprecision(0) does? I seem to vaguely recall
that it
turns precision control off (13.000000 would be the 'right' output
then), but I'm
not sure (or is it setprecision(-1)?).

Moreover, I'm after option 1, that is, I insist on printing my
doubles with _no_
digits past the decimal point (and no decimal point), how do I achieve
this
behaviour? The obvious solution "convert to int and output" is not
available to me.
I only have the ofstream object, the output is done by some class's
operator<<,
which outputs a series of doubles and which I cannot modify. Up to now
I could
control everything with setprecision() and a method set_maths_width()
that the
class provides (as setw() works for the first output only), but when I
want a
precision of 0... I'm stumped.

TIA,
- J.
 
C

ccahoon

Hello!

Can someone tell me what the expected output of the following
program is?

#include <fstream>
#include <iomanip>

using namespace std;

int main() {
ofstream o("o");
o << fixed << setprecision(0) << 13.0 << endl;

}

Depending on which STL implementation I use with my compiler, I
either get
"13" or "13.000000".

I don't have the Standard (at all) or the Josuttis book (with me),
so can anyone
shed some light on what setprecision(0) does? I seem to vaguely recall
that it
turns precision control off (13.000000 would be the 'right' output
then), but I'm
not sure (or is it setprecision(-1)?).

Moreover, I'm after option 1, that is, I insist on printing my
doubles with _no_
digits past the decimal point (and no decimal point), how do I achieve
this
behaviour? The obvious solution "convert to int and output" is not
available to me.
I only have the ofstream object, the output is done by some class's
operator<<,
which outputs a series of doubles and which I cannot modify. Up to now
I could
control everything with setprecision() and a method set_maths_width()
that the
class provides (as setw() works for the first output only), but when I
want a
precision of 0... I'm stumped.

TIA,
- J.

If you know that there will be one decimal place (13.0), rather than
any number, (13.1324), then setprecision(-1) is fine.

Otherwise, you need to round it, floor it, or ceil it. It will remain
a double, but will only have one decimal point (13.1324->13.0).


#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
ofstream o("o");
o << setprecision(-1) << rint(13.150);

return 0;
}

Will get the output you want. If you can't round, but have a lot more
digits, I don't know how to help.

Hope I helped.

Chris
 
J

Jacek Dziedzic

ccahoon said:
Hello!

Can someone tell me what the expected output of the following
program is?

#include <fstream>
#include <iomanip>

using namespace std;

int main() {
ofstream o("o");
o << fixed << setprecision(0) << 13.0 << endl;

}

Depending on which STL implementation I use with my compiler, I
either get
"13" or "13.000000".

I don't have the Standard (at all) or the Josuttis book (with me),
so can anyone
shed some light on what setprecision(0) does? I seem to vaguely recall
that it
turns precision control off (13.000000 would be the 'right' output
then), but I'm
not sure (or is it setprecision(-1)?).

Moreover, I'm after option 1, that is, I insist on printing my
doubles with _no_
digits past the decimal point (and no decimal point), how do I achieve
this
behaviour? The obvious solution "convert to int and output" is not
available to me.
I only have the ofstream object, the output is done by some class's
operator<<,
which outputs a series of doubles and which I cannot modify. Up to now
I could
control everything with setprecision() and a method set_maths_width()
that the
class provides (as setw() works for the first output only), but when I
want a
precision of 0... I'm stumped.

TIA,
- J.

If you know that there will be one decimal place (13.0), rather than
any number, (13.1324), then setprecision(-1) is fine.

Otherwise, you need to round it, floor it, or ceil it. It will remain
a double, but will only have one decimal point (13.1324->13.0).


#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
ofstream o("o");
o << setprecision(-1) << rint(13.150);

return 0;
}

Will get the output you want. If you can't round, but have a lot more
digits, I don't know how to help.

In fact, I tried floor()ing it, but that didn't help.
I cannot allow for the trailing ".0", because the whole point
of this program is to make the text data smaller by truncating
the irrelevant digits.

I sure hope the STL gives a way to output doubles with
"0 points after the decimal point".

cheers,
- J.
 
V

Victor Bazarov

Jacek said:
ccahoon said:
Hello!

Can someone tell me what the expected output of the following
program is?

#include <fstream>
#include <iomanip>

using namespace std;

int main() {
ofstream o("o");
o << fixed << setprecision(0) << 13.0 << endl;

}

Depending on which STL implementation I use with my compiler, I
either get
"13" or "13.000000".

I don't have the Standard (at all) or the Josuttis book (with me),
so can anyone
shed some light on what setprecision(0) does? I seem to vaguely
recall that it
turns precision control off (13.000000 would be the 'right' output
then), but I'm
not sure (or is it setprecision(-1)?).

Moreover, I'm after option 1, that is, I insist on printing my
doubles with _no_
digits past the decimal point (and no decimal point), how do I
achieve this
behaviour? The obvious solution "convert to int and output" is not
available to me.
I only have the ofstream object, the output is done by some class's
operator<<,
which outputs a series of doubles and which I cannot modify. Up to
now I could
control everything with setprecision() and a method
set_maths_width() that the
class provides (as setw() works for the first output only), but
when I want a
precision of 0... I'm stumped.

TIA,
- J.

If you know that there will be one decimal place (13.0), rather than
any number, (13.1324), then setprecision(-1) is fine.

Otherwise, you need to round it, floor it, or ceil it. It will remain
a double, but will only have one decimal point (13.1324->13.0).


#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
ofstream o("o");
o << setprecision(-1) << rint(13.150);

return 0;
}

Will get the output you want. If you can't round, but have a lot more
digits, I don't know how to help.

In fact, I tried floor()ing it, but that didn't help.
I cannot allow for the trailing ".0", because the whole point
of this program is to make the text data smaller by truncating
the irrelevant digits.

I sure hope the STL gives a way to output doubles with
"0 points after the decimal point".

If 'setprecision(0)' doesn't work (for whatever reason) you can always
output an int followed by a '.', can't you? ;-)

V
 
J

Jacek Dziedzic

Victor said:
Jacek said:
ccahoon said:
On Jun 12, 8:07 am, (e-mail address removed) wrote:
Hello!

Can someone tell me what the expected output of the following
program is?

#include <fstream>
#include <iomanip>

using namespace std;

int main() {
ofstream o("o");
o << fixed << setprecision(0) << 13.0 << endl;

}

Depending on which STL implementation I use with my compiler, I
either get
"13" or "13.000000".

I don't have the Standard (at all) or the Josuttis book (with me),
so can anyone
shed some light on what setprecision(0) does? I seem to vaguely
recall that it
turns precision control off (13.000000 would be the 'right' output
then), but I'm
not sure (or is it setprecision(-1)?).

Moreover, I'm after option 1, that is, I insist on printing my
doubles with _no_
digits past the decimal point (and no decimal point), how do I
achieve this
behaviour? The obvious solution "convert to int and output" is not
available to me.
I only have the ofstream object, the output is done by some class's
operator<<,
which outputs a series of doubles and which I cannot modify. Up to
now I could
control everything with setprecision() and a method
set_maths_width() that the
class provides (as setw() works for the first output only), but
when I want a
precision of 0... I'm stumped.

TIA,
- J.
If you know that there will be one decimal place (13.0), rather than
any number, (13.1324), then setprecision(-1) is fine.

Otherwise, you need to round it, floor it, or ceil it. It will remain
a double, but will only have one decimal point (13.1324->13.0).


#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
ofstream o("o");
o << setprecision(-1) << rint(13.150);

return 0;
}

Will get the output you want. If you can't round, but have a lot more
digits, I don't know how to help.
In fact, I tried floor()ing it, but that didn't help.
I cannot allow for the trailing ".0", because the whole point
of this program is to make the text data smaller by truncating
the irrelevant digits.

I sure hope the STL gives a way to output doubles with
"0 points after the decimal point".

If 'setprecision(0)' doesn't work (for whatever reason) you can always
output an int followed by a '.', can't you? ;-)

If only.

... or is there a way around it that I don't see?

cheers,
- J.
 
V

Victor Bazarov

Jacek said:
[..]

... or is there a way around it that I don't see?

Sorry, I apparently wasn't paying attention. My bad.

What you apparently want is to output the number as it is without the
fractional part, correct? You don't have the option to round them,
nor does it help you to have control over the width - since the values
are different all the time. Use 'fixed' manipulator:

#include <iostream>
#include <ostream>
#include <iomanip>
using namespace std;

class someclass
{
};

ostream& operator <<(ostream& os, someclass const &)
{
os << 1.23456789 << ' ' << 12.34567 << ' ' << 123.45 << ' ';
return os << 1234.5 << ' ' << 123456.0;
}

int main()
{
for (int p = 5; p > -1; --p) {
cout << setprecision(p);
cout << cout.precision() << ' ' << fixed << someclass() << endl;
}
}

I believe it should do what you expect it to. Good luck!

V
 
J

James Kanze

Can someone tell me what the expected output of the following
program is?
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
ofstream o("o");
o << fixed << setprecision(0) << 13.0 << endl;
}
Depending on which STL implementation I use with my compiler, I
either get
"13" or "13.000000".

It should be "13". The standard is quite clear about this.
I don't have the Standard (at all) or the Josuttis book (with
me), so can anyone shed some light on what setprecision(0)
does?

It ensures that all future reads of the precision will return
0:).

The output format is defined in terms of equivalent printf
specifiers; if the type is a floating point type, the output
format always has the precision set, e.g. "%.*f", with the *
begin replaced by the precision.
I seem to vaguely recall that it turns precision control off
(13.000000 would be the 'right' output then), but I'm not sure
(or is it setprecision(-1)?).

At least in the standard iostream, you can't turn precision
control off. The precision is set to 6 during initialization,
and is always used for floating point (and never for any other
type).
Moreover, I'm after option 1, that is, I insist on printing my
doubles with _no_ digits past the decimal point (and no
decimal point), how do I achieve this behaviour?

By setting the precision to 0, at least with a standard
conforming library.

What implementation doesn't do this? All of those available to
me are correct here.
 
J

Jacek Dziedzic

James said:
It should be "13". The standard is quite clear about this.


It ensures that all future reads of the precision will return
0:).

The output format is defined in terms of equivalent printf
specifiers; if the type is a floating point type, the output
format always has the precision set, e.g. "%.*f", with the *
begin replaced by the precision.


At least in the standard iostream, you can't turn precision
control off. The precision is set to 6 during initialization,
and is always used for floating point (and never for any other
type).


By setting the precision to 0, at least with a standard
conforming library.

Thanks a lot for the clarification, perhaps I should file
a bug report.
What implementation doesn't do this? All of those available to
me are correct here.

This is the Intel Compiler (ICC) v9.1 20061105
for the IA64. Compiling with "icpc test.cpp" gives "13" as output,
compiling with "icpc -cxxlib-icc test.cpp" gives "13.000000"
as output. Therefore I blame the STL implementation, not the
compiler.

<OT>

Now that I take a look at Intel's site, it says:
>Previous versions of Intel C++ Compiler for Linux had an
>alternative C++ library provided by Intel with the –cxxlib-icc
>option. This didn’t provide C++ binary compatibility with gcc due
>to differences in the C++ library implementation. This option,
>–cxxlib-icc, had been deprecated and is no longer available
>as of version 10.0. The benefit is that C++ code now is always
>binary compatible with supported versions of g++.
>Intel C++ Compiler for Linux supports the C++ ABI, a convention

so perhaps I should reconsider this -cxxlib-icc option.

</OT>

Thanks a lot!

- J.
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top