Sign in to follow this  
snooty

static const double PI = 3.1415

Recommended Posts

Hi, I tried the following: class Math { public: static const double PI = 3.1415; }; But the compiler complains: only static const integral data members can be initialized within a class What is going on? Why can't double be initialized? Any solution? Please help.

Share this post


Link to post
Share on other sites
You can assign a static const inside the class ... what you want is this:

in your .h:

class Math
{
public:
static const double PI;
};

Then in your .cpp:

static const double Math::PI = 3.1415;




Now i'm not 100% on that sytax its off the top of my head, but its something pretty close to that.

[Edit] Beaten :P

Share this post


Link to post
Share on other sites
Quote:
Original post by ordered_disorder
Quote:
Original post by daerid
well, you could try this:

*** Source Snippet Removed ***

Although, PI is already defined in <cmath>


I suck at c++. What are you trying to accomplish with that const struct?


Something too clever for my own good, and altogether useless, really.

Share this post


Link to post
Share on other sites
Quote:
Original post by snooty
But the compiler complains:
only static const integral data members can be initialized within a class


I believe integral, in this sense, means integers. That means no floating-point values (ie. no floats or doubles). I could be wrong, though. Usually I initialize my variables in the constructor.

Share this post


Link to post
Share on other sites
Quote:
Original post by snooty
But the compiler complains:
only static const integral data members can be initialized within a class


It does what it says on the tin.

Quote:
Original post by snooty
Why can't double be initialized?


Because the C++ ISO standard says so.

Quote:
Original post by snooty
Any solution?


All static non-integral constants must be defined outside of class defintions, just as in kaysik's example.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>


Ah, Windows calculator, my favourite program ever.

Try this in it:

2+3*5

Then change from scientific view to normal view, or vice versa, and try it again.

What a piece of software!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Paradigm Shifter
Quote:
Original post by Evil Steve
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>


Ah, Windows calculator, my favourite program ever.

Try this in it:

2+3*5

Then change from scientific view to normal view, or vice versa, and try it again.

What a piece of software!


That's because in scientific mode, it emulates what would happen on a scientific calculator (2+3*5) and in the standard mode, it emulates what would happen on a basic calculator ((2+3)*5).

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Paradigm Shifter
Quote:
Original post by Evil Steve
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>


Ah, Windows calculator, my favourite program ever.

Try this in it:

2+3*5

Then change from scientific view to normal view, or vice versa, and try it again.

What a piece of software!


That's because in scientific mode, it emulates what would happen on a scientific calculator (2+3*5) and in the standard mode, it emulates what would happen on a basic calculator ((2+3)*5).



Yeah, I know why it does it (although every calculator I have ever used still had operator precedence correct, even basic calculators).

What I didn't like was being caught by surprise by it, then looking at the help for the explanation and finding nothing...

Share this post


Link to post
Share on other sites
Quote:
Original post by Paradigm Shifter
Yeah, I know why it does it (although every calculator I have ever used still had operator precedence correct, even basic calculators).

What you just said made no sense. Calculators are not telepathic, so they can't predict what the next operator you input will be. It has nothing at all to do with operator precedence.

To see why this is problematic, imagine that I show you a series of cards with numbers and operators written on them, one at a time. Then I ask you to tell me the result.

If I show you "5", "+", "7", you would say 12 at this point, and you would be correct from the calculator's point of view. But in reality, you don't know if I'm finished or not. Had my next two cards been "*", "3", the correct answer for the entire expression is 26 (5 + 7 * 3). But an equally valid answer, from your point of view, is 36 -- the first expression, multiplied by 3 ((5 + 7) * 3).

This is one reason why postfix/prefix calculators are useful, because the entire expression isn't (and can't be) evaluated until the symbols have been input. Thus, there cannot be any ambiguity about the order of operations you meant to perform.

Share this post


Link to post
Share on other sites
To the OP: Why is PI part of a class? This is mostly a rhetorical question. The answer is that the OP has programmed in Java for too long. :)

In C++ there is a notion of namespace, which is probably what you want to use instead of a class in this case. This works just fine:

namespace Math
{
const double PI = 3.1415;
}


Or, even better, this:
#include <cmath>

namespace Math
{
const double PI = 4.0*std::atan2(1.0,1.0);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Paradigm Shifter
Quote:
Original post by Evil Steve
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>


Ah, Windows calculator, my favourite program ever.

Try this in it:

2+3*5

Then change from scientific view to normal view, or vice versa, and try it again.

What a piece of software!


That's because in scientific mode, it emulates what would happen on a scientific calculator (2+3*5) and in the standard mode, it emulates what would happen on a basic calculator ((2+3)*5).


this is just not true. 2 + 3 * 5 does not = 17 on any single line scientific calculator I have ever owned. It does = 17 on calculators for which you enter the whole line and then press enter (aka TI-81), but not on any normal calculator - scientific or not - except windows calculator.

The running assumption in every calculator I've ever used is that it is at a particular value after each operation, else how would memory work:

2 + 3 M+ (better be 5 or we're in trouble) * 5 - better be 25 or we're really in a strange situation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
this is just not true. 2 + 3 * 5 does not = 17 on any single line scientific calculator I have ever owned. It does = 17 on calculators for which you enter the whole line and then press enter (aka TI-81), but not on any normal calculator - scientific or not - except windows calculator.

I have used at least one calculator that exhibited rather reasonable precedence behavior. I've used others that allowed several nested parenthesis, although I don't recall how they behaved when you entered such an expression.

CM

Share this post


Link to post
Share on other sites
I stand corrected. So apparently there are calculators that support "DAL" even when they are not the type that displays the equation before running it. A key to remember here is that this would not be "correct" behavior in an enviroment where it wasn't explicitly chosen, because it does not follow the logic of most calculators fundamental operating enviroment (that it is a system with a single primary changing value, which is acted upon in sequence as you perform operations, and is always at a "current" value (for operations like memory storage, etc).

Basically a normal calculator is always the same as - do this sequence of operations:

(v1 o1 v2) o2 v3) o3 v4) o4 v5) ....

a DAL calculator is the same as "solve this equation / expression":

v1 o1 v2 o2 v3 o3 v4 o4 v5 ....

so how do things like memory storage and recall interact in such an enviroment.

As for supporting nested parenthesis, I've used that feature plenty of times, many calculators support it - and it is not confusing since the user controls it (unless they get so deep they confuse themselves).

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
As for supporting nested parenthesis, I've used that feature plenty of times, many calculators support it - and it is not confusing since the user controls it (unless they get so deep they confuse themselves).

The main reason I brought this up was in responce to k2's assertion that getting precedence right requires telepathy. It doesn't...it just requires that the calculator remember steps it hasn't actually executed yet. It has to do the same to properly support nested parenthesis, so clearly the idea isn't so far fetched.

CM

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this