428,591 Members | 650 Online
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

# behaviour of setprecision(0)

 P: n/a Hello! Can someone tell me what the expected output of the following program is? #include #include 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 Replies

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

 P: n/a ccahoon wrote: On Jun 12, 8:07 am, jacek.dzied...@gmail.com wrote: > Hello! Can someone tell me what the expected output of the followingprogram is?#include #include 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, Ieither get"13" or "13.000000". I don't have the Standard (at all) or the Josuttis book (with me),so can anyoneshed some light on what setprecision(0) does? I seem to vaguely recallthat itturns precision control off (13.000000 would be the 'right' outputthen), but I'mnot sure (or is it setprecision(-1)?). Moreover, I'm after option 1, that is, I insist on printing mydoubles with _no_digits past the decimal point (and no decimal point), how do I achievethisbehaviour? The obvious solution "convert to int and output" is notavailable to me.I only have the ofstream object, the output is done by some class'soperator<<,which outputs a series of doubles and which I cannot modify. Up to nowI couldcontrol everything with setprecision() and a method set_maths_width()that theclass provides (as setw() works for the first output only), but when Iwant aprecision 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 #include #include 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

 P: n/a 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 followingprogram is?#include #include 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, Ieither get"13" or "13.000000". I don't have the Standard (at all) or the Josuttis book (with me),so can anyoneshed some light on what setprecision(0) does? I seem to vaguelyrecall that itturns precision control off (13.000000 would be the 'right' outputthen), but I'mnot sure (or is it setprecision(-1)?). Moreover, I'm after option 1, that is, I insist on printing mydoubles with _no_digits past the decimal point (and no decimal point), how do Iachieve thisbehaviour? The obvious solution "convert to int and output" is notavailable to me.I only have the ofstream object, the output is done by some class'soperator<<,which outputs a series of doubles and which I cannot modify. Up tonow I couldcontrol everything with setprecision() and a methodset_maths_width() that theclass provides (as setw() works for the first output only), butwhen I want aprecision of 0... I'm stumped.TIA,- J. If you know that there will be one decimal place (13.0), rather thanany number, (13.1324), then setprecision(-1) is fine.Otherwise, you need to round it, floor it, or ceil it. It will remaina double, but will only have one decimal point (13.1324->13.0). #include #include #include 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 moredigits, 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

 P: n/a 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 followingprogram is?#include #include 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, Ieither get"13" or "13.000000". I don't have the Standard (at all) or the Josuttis book (with me),so can anyoneshed some light on what setprecision(0) does? I seem to vaguelyrecall that itturns precision control off (13.000000 would be the 'right' outputthen), but I'mnot sure (or is it setprecision(-1)?). Moreover, I'm after option 1, that is, I insist on printing mydoubles with _no_digits past the decimal point (and no decimal point), how do Iachieve thisbehaviour? The obvious solution "convert to int and output" is notavailable to me.I only have the ofstream object, the output is done by some class'soperator<<,which outputs a series of doubles and which I cannot modify. Up tonow I couldcontrol everything with setprecision() and a methodset_maths_width() that theclass provides (as setw() works for the first output only), butwhen I want aprecision of 0... I'm stumped.TIA,- J.If you know that there will be one decimal place (13.0), rather thanany number, (13.1324), then setprecision(-1) is fine.Otherwise, you need to round it, floor it, or ceil it. It will remaina double, but will only have one decimal point (13.1324->13.0). #include #include #include 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 moredigits, 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 pointof this program is to make the text data smaller by truncatingthe 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 notavailable to me.I only have the ofstream object, the output is done by some class'soperator<<,which outputs a series of doubles and which I cannot modify. Up tonow I couldcontrol everything with setprecision() and a methodset_maths_width() that theclass provides (as setw() works for the first output only), butwhen I want aprecision of 0... I'm stumped. ... or is there a way around it that I don't see? cheers, - J. Jun 12 '07 #5

 P: n/a Jacek Dziedzic wrote: [..] I wrote: >>>>The obvious solution "convert to int and output" is notavailable to me.I only have the ofstream object, the output is done by someclass's operator<<,which outputs a series of doubles and which I cannot modify. Up tonow I couldcontrol everything with setprecision() and a methodset_maths_width() that theclass provides (as setw() works for the first output only), butwhen I want aprecision 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 #include #include 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

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

 P: n/a James Kanze wrote: On Jun 12, 2:07 pm, jacek.dzied...@gmail.com wrote: > Can someone tell me what the expected output of the followingprogram is? >#include #include >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, Ieither 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 (withme), 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 mydoubles with _no_ digits past the decimal point (and nodecimal 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. Now that I take a look at Intel's site, it says: >Previous versions of Intel C++ Compiler for Linux had analternative C++ library provided by Intel with the –cxxlib-iccoption. This didn’t provide C++ binary compatibility with gcc dueto differences in the C++ library implementation. This option,–cxxlib-icc, had been deprecated and is no longer availableas of version 10.0. The benefit is that C++ code now is alwaysbinary 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. Thanks a lot! - J. Jun 13 '07 #8

### This discussion thread is closed

Replies have been disabled for this discussion.