473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

behaviour of setprecision(0)


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.

Jun 12 '07 #1
7 12477
On Jun 12, 8:07 am, jacek.dzied...@gmail.com 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.

Hope I helped.

Chris

Jun 12 '07 #2
ccahoon wrote:
On Jun 12, 8:07 am, jacek.dzied...@gmail.com 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".

cheers,
- J.
Jun 12 '07 #3
Jacek Dziedzic wrote:
ccahoon wrote:
>On Jun 12, 8:07 am, jacek.dzied...@gmail.com 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? ;-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 12 '07 #4
Victor Bazarov wrote:
Jacek Dziedzic wrote:
>ccahoon wrote:
>>On Jun 12, 8:07 am, jacek.dzied...@gmail.com 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.

I wrote:
>>>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.
... or is there a way around it that I don't see?

cheers,
- J.
Jun 12 '07 #5
Jacek Dziedzic wrote:
[..]
I wrote:
>>>>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.

... 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 12 '07 #6
On Jun 12, 2:07 pm, jacek.dzied...@gmail.com wrote:
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.

--
James Kanze (GABI Software, from CAI) email: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

Jun 13 '07 #7
James Kanze wrote:
On Jun 12, 2:07 pm, jacek.dzied...@gmail.com wrote:
> 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.
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.
Jun 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: tarmat | last post by:
I'm trying to create a function that will turn a float into a std::string that always shows the number to two decimal places. I have the following but it doesn't give the desired output: ...
2
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in scientific notation. For example std::stringstream...
1
by: Gary Wessle | last post by:
hi the code below is giving me what I want but it is very ugly. and will not work for a long list of different length numbers. could you please look at it and comment. thank you the...
2
by: Wing | last post by:
Hello everyone, I have the following code: /////////////////////////////////////// double num; num=1234567890; num+=(1.0/3.0); cout.setf(ios::fixed);
3
by: Anjo Gasa | last post by:
I'm having some cases where setprecision in combination with iostreams gives some unepected behavior. Consider the following program: #include <iostream> #include <iomanip> int _tmain(int...
2
by: mahesh.kanakaraj | last post by:
Hi All, I am new to C++ programming. I encounter an unexpected behavior of 'setprecision'. The code snippet is : #include <iostream.h> #include <stdio.h> #include <iomanip.h>
3
by: PengYu.UT | last post by:
Hi, I setprecision to be 100 for both cases. I'm wondering why the number of digits are different. Also, for a double number, I think any digits that are longer than 15 (or 16) are not...
1
by: jthep | last post by:
Hi, I'm converting codes that I worked with in C to C++ and having a problem with setprecision. At first the code worked, then I made some changes. When the changes didn't work I wrote it back to...
2
by: victoryusami | last post by:
I'm not 100% sure whats even going on, but I'm working on a C++ assignment for school, and its a road trip program #include <iostream> #include <iomanip> using namespace std; char const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.