Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,850 people online right now. Registration is fast and FREE... Join Now!




Defined constants

 
Reply to this topicStart new topic

Defined constants

pietra
13 Mar, 2007 - 09:02 AM
Post #1

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
When constants are defined with #define, how do you know which type are they (int, long)? And do they have a type?

I have this code

CODE

#include <iostream>
#define PI 3.14159
int main ()
{
    
std::cout << PI;
getchar();
return 0;
}


output is

3.14159

And when I add more numbers for PI

CODE

#define PI 3.14159265


output is still

3.14159

Why is that?

This post has been edited by pietra: 13 Mar, 2007 - 09:08 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Defined Constants
13 Mar, 2007 - 09:09 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
It is important to remember that when using the define directive, it is actually implementing a macro that replaces any occurrences of the specified define (PI i this case) with the definition. It is a straight replacement in the code by the preprocessor. As such, there really are no data types for defines.
User is online!Profile CardPM
+Quote Post

pietra
RE: Defined Constants
13 Mar, 2007 - 09:17 AM
Post #3

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
Ok, no data types.

But why it won't display the whole number? Shouldn't it be a defined constant, how you -define- it it's up to you and how many digits there are is up to you as well? What if you need to use very precise calculation with let's say PI?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Defined Constants
13 Mar, 2007 - 09:23 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Is it truncating or rounding? I'm afraid I'm not next to a compiler ATM...can you change #define PI 3.14159265 to #define PI 3.14159665 and see if it cuts or rounds?
User is online!Profile CardPM
+Quote Post

pietra
RE: Defined Constants
13 Mar, 2007 - 09:29 AM
Post #5

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
QUOTE(Amadeus @ 13 Mar, 2007 - 10:23 AM) *

Is it truncating or rounding? I'm afraid I'm not next to a compiler ATM...can you change #define PI 3.14159265 to #define PI 3.14159665 and see if it cuts or rounds?


I've change it and output is 3.1416

Sorry I don't get it
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Defined Constants
13 Mar, 2007 - 09:31 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
That means that the number is being rounded somehow, not actually cut off.
User is online!Profile CardPM
+Quote Post

pietra
RE: Defined Constants
13 Mar, 2007 - 09:33 AM
Post #7

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
QUOTE(Amadeus @ 13 Mar, 2007 - 10:31 AM) *

That means that the number is being rounded somehow, not actually cut off.

Aha, I see. And that's suppose to be correct? Is this because of compiler or that's just the way it is? I use Dev C++ now.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Defined Constants
14 Mar, 2007 - 03:01 AM
Post #8

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Include <iomanip> and add std::cout<<std::setprecision(10); before std::cout << PI;

This post has been edited by Xing: 14 Mar, 2007 - 03:02 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Defined Constants
14 Mar, 2007 - 05:25 AM
Post #9

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(Xing @ 14 Mar, 2007 - 04:31 PM) *

Include <iomanip> and add std::cout<<std::setprecision(10); before std::cout << PI;


yes xing is correct icon_up.gif . it's rounding it because it's precision for console out is 4 by default, so it round up the number if it does not fit into the format.[somewhat similar to C formating concepts]

so to get more precision or different format of output [hex, binary or only whole numbers etc.] we need to do formatting first using "iomanip" header file.
User is offlineProfile CardPM
+Quote Post

pietra
RE: Defined Constants
15 Mar, 2007 - 01:26 AM
Post #10

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
And... it's working smile.gif

QUOTE(AmitTheInfinity @ 14 Mar, 2007 - 06:25 AM) *

it's rounding it because it's precision for console out is 4 by default

This was the missing fact.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Defined Constants
15 Mar, 2007 - 05:42 AM
Post #11

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(pietra @ 15 Mar, 2007 - 02:56 PM) *

And... it's working smile.gif

QUOTE(AmitTheInfinity @ 14 Mar, 2007 - 06:25 AM) *

it's rounding it because it's precision for console out is 4 by default

This was the missing fact.

I guess it's 6 instead of 4 not sure though.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month