# static const double PI = 3.1415

This topic is 4156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
well, you could try this:

const struct{  operator const double() const { return 3.1415; }} PI;

Although, PI is already defined in <cmath>

##### Share on other sites
class Math{public:static const double PI;};static const double Math::PI = 3.1415;

##### Share on other sites
Quote:
 Original post by daeridwell, you could try this:*** Source Snippet Removed ***Although, PI is already defined in

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

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

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

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.

 Beaten :P

##### Share on other sites
The correct thing to do would be to #define _USE_MATH_DEFINES before including cmath, and then use M_PI

##### Share on other sites
Quote:
Original post by ordered_disorder
Quote:
 Original post by daeridwell, you could try this:*** Source Snippet Removed ***Although, PI is already defined in

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 on other sites
Quote:
 Original post by snootyBut 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 on other sites
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>

##### Share on other sites
Quote:
 Original post by snootyBut 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 snootyWhy can't double be initialized?

Because the C++ ISO standard says so.

Quote:
 Original post by snootyAny solution?

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

##### Share on other sites
Quote:
 Original post by Evil SteveIt should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)

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 on other sites
Quote:
Quote:
 Original post by Evil SteveIt should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)

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 on other sites
Quote:
Original post by Anonymous Poster
Quote:
Quote:
 Original post by Evil SteveIt should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)

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 on other sites
Quote:
 Original post by Paradigm ShifterYeah, 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 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 on other sites
Quote:
Original post by Anonymous Poster
Quote:
Quote:
 Original post by Evil SteveIt should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)

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 on other sites
Quote:
 Original post by Xaithis 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 on other sites
Xai:
DAL (Direct Algebraic Logic) calculators have been around since 1992.

The simple single-line calculator I had 12 years ago got equations like 2 + 3 * 5 correct when entered as written.

You're a bit behind the times!

##### 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 on other sites
Quote:
 Original post by XaiAs 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