look, I didn't #include <ios>, and this program also works, and produces answer.
1 2 3 4 5 6
#include <iostream>
int main() {
double pi = 3.141592653589792;
std::cout << std::scientific << pi << std::endl;
return 0;
}
3.141593e+000
I use eclipse+MinGW;
why??? I found this too much.
ps. I think it's cumbersome to writed things with std::, if we want to set the output format, there's too many std:: prefix to use, why someone prefer ust std:: each time needed to just use using namespace std; ?
I think iostream includes ios.h. There is a class hierachy for ios stuff.
As far as std:: goes there a couple of things you can do:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
//same for other std things
int main() {
double pi = 3.141592653589792;
cout << std::scientific << pi << endl;
return 0;
}
I tend to do that for things that are used a lot, then put std:: for things that aren't (like scientific in this example)
HTH
Edit: If you include cmath you can use M_PI directly without having to define your own constant. There are other math constants also. Look at the math.h file
#include <ios> is listed in the Header <iostream> synopsis in the standard §27.4.1. Does that mean <ios> has to be included by <iostream> or what? I can't see it saying that explicitly.
TheIdeasMan wrote:
If you include cmath you can use M_PI directly without having to define your own constant.
Well I am surprised by that. It may not be part of the standard library, but on all the systems I have ever used since 1987, it is in math.h which is included in cmath. Is it that I am lucky that this is the case for gcc 4.7 ?
It seems strange to me that math constants are not part of standard system, they are used frequently by coders wanting to do math type applications - why should the coder have to define their own version?
I was reading that the boost library has math constants built in. Is it true that some things are not in the STL because they are so readily available in boost?
Well how hard is it to declare your own constants ? Also the FPU has an instruction to load PI into the top register but I haven't seen anything take advantage of that yet.
It seems strange to me that math constants are not part of standard system, they are used frequently by coders wanting to do math type applications - why should the coder have to define their own version?
I don't know why. Maybe they think it's not needed because it could easily be calculated as std::acos(-1). Defining a global constant/macro is more of the C way of doing it. If C++ adds it to the standard I guess it would have been something similar to std::numeric_limits but for math constants, to be able to handle all the floating point types correctly, which would be more verbose than just using M_PI.
TheIdeasMan wrote:
I was reading that the boost library has math constants built in. Is it true that some things are not in the STL because they are so readily available in boost?
I don't think so. A lot of things in C++11 was taken from boost.
Well how hard is it to declare your own constants ?
Not hard, but why should I? There is potential for error, if one types it in wrong. Something I would personally be very careful about - if I had to do it.
An suspecting coder might define the constant with only 6 dp, then wonder why they have errors in some situations. You could get the value from your calculator - but what if only does 8dp?
Much easier to use M_PI or M_E say and not worry about it.
I do mathematical apps, all the time - I would find it a real pain to define my own constants all the time - that is part of what math headers are for IMO.
This is about on par with when I learnt that main does not have explicitly return an int.
I guess I do still think with a "C" mentality, because that is what I learnt first.
If C++ adds it to the standard I guess it would have been something similar so std::numeric_limits but for math constants, to be able to handle all the floating point types correctly, which would be more verbose than just using M_PI.
Yes, I think boost does something similar.
BTW, I have a really geeky T-shirt that has pi to 8,000 dp !! I haven't been game to wear it anywhere though :)
For those who want to make their own constants - from math.h
There is still potential for error, your just assuming that whoever wrote it in the standard didn't make an error. Not sure what the specifications were of M_PI when it was still defined but who's to say it met your needs ? What if I am on a computer that has a 128 bit floating point precision number. M_PI is now invalid for my computer and by your assumption I would never solve the problem cause I assume M_PI is what I need and would never have a second thought.
Why would you go to a calculator to get PI ? There's your problem.
http://www.gnu.org/software/libc/manual/html_node/Mathematical-Constants.html
These constants come from the Unix98 standard and were also available in 4.4BSD; therefore they are only defined if _BSD_SOURCE or _XOPEN_SOURCE=500, or a more general feature select macro, is defined. The default set of features includes these constants.
What if I am on a computer that has a 128 bit floating point precision number.
All values are of type double. As an extension, the GNU C Library also defines these constants with type long double. The long double macros have a lowercase ‘l’ appended to their names: M_El, M_PIl, and so forth. These are only available if _GNU_SOURCE is defined.
Why would you go to a calculator to get PI ? There's your problem.
That is not what I would do - I meant a newbie might do that.
The values in math.h (shown above) are to 20 dp and would be sufficient for use with doubles, and I am guessing that was the intention back in the days of ANSI C. Also I would have thought that the double type would be sufficient for most applications IMO. Problems with precision are often avoided by changing the units - astrophysicists use light years instead of metres as an example. That is not say that there are applications that do need extended precision.
Sure if you are using a 128 bit FP then you would need something else. I was not proposing it was a solution for everything, although it is my fault for making it sound like that. What I really meant was there should be some sort of built in value.
This brings me to the boost library again. I was reading that their value of pi is good to 100dp!! Hopefully that is good enough for everyone.
Now a case in point. Peter87 has pointed out that the OP had the last digit incorrect. If M_PI (or some other even more precise value) was available to the OP - then that would not have happened.
Any way thanks for your counterpoint - some debate is good, one never knows what one might learn.
To give a bit more info on the availability of M_PI: it likely started in BSD (as the GNU doc above says) and after defeating its rival PI (due to less likely naming collisions) it became part of the Single Unix Specification, which later merged with POSIX where M_PI became a part of the XSI extension to the POSIX standard.
It has never been part of C or C++, but boost introduced it in its math.constants library in 2006, which now defines both function templates such as boost::math::constants::pi<double>(); and constants such as boost::math::double_constants::pi