473,652 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12499
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_wid th() 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_wi dth() 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_wi dth() 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_w idth() 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 objektorientier ter Datenverarbeitu ng
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
22571
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: inline std::string ftos(float f) { std::ostringstream buffer; buffer << std::setprecision(2) << f;
2
8715
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 buffer; buffer << setprecision(1) << 40.0 << "° C";
1
2670
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 desired output is: 0.555504 of an inch foreach 12.55
2
2310
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
2489
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 argc, _TCHAR* argv) { float value = 0.063397526741027832;
2
1804
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
5060
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 meaningful, because it exceed the double number's precision limit. Even if I setprecision to be 100, shall it truncate the number to be of 15(or 16) digits?
1
4871
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 how it was before and now it's not working. I keep getting the error message where setprecision is undefined. Can anyone help plz. #include <iostream> using namespace std; float convertFtoC(int temperature); int roundUpNextMultiple(int...
2
5516
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 METRIC = 'M'; // Input read as metric char const ENGLISH = 'E'; // Input read as english char const QUIT = 'Q'; // Input read to quit program char const QUIT2 = 'q'; // Input read to quit program
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.