469,089 Members | 1,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,089 developers. It's quick & easy.

#define for very small numbers ?

I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
Jun 27 '08 #1
27 4728
In article <6f**********************************@w8g2000prd.g ooglegroups.com>,
pereges <Br*****@gmail.comwrote:
>I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define
#define Planck 6.67e-34

--
"The beauties of conception are always superior to those of
expression." -- Walter J. Phillips
Jun 27 '08 #2
pereges wrote:
I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
You could use #define, but it would be better to use a const double.

--
Ian Collins.
Jun 27 '08 #3
Ian Collins wrote:
pereges wrote:
>I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.

You could use #define, but it would be better to use a const double.
Why? Think about it. That way you can't define compile time
constants.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #4
CBFalconer wrote:
Ian Collins wrote:
>pereges wrote:
>>I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
You could use #define, but it would be better to use a const double.

Why? Think about it. That way you can't define compile time
constants.
What use is a compile time floating point constant?

--
Ian Collins.
Jun 27 '08 #5
Ian Collins wrote:
CBFalconer wrote:
>Ian Collins wrote:
>>pereges wrote:

I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.

You could use #define, but it would be better to use a const
double.

Why? Think about it. That way you can't define compile time
constants.

What use is a compile time floating point constant?
A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #6
CBFalconer wrote:
Ian Collins wrote:
>CBFalconer wrote:
>>Ian Collins wrote:
pereges wrote:

I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
You could use #define, but it would be better to use a const
double.
Why? Think about it. That way you can't define compile time
constants.
What use is a compile time floating point constant?

A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.
Which is what any self respecting optimiser will do with a constant.

--
Ian Collins.
Jun 27 '08 #7
In article <66*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.
>Which is what any self respecting optimiser will do with a constant.
But C's const variables are not constants. Do the guarantees that
const makes actually allow the compiler to assume the variable's
value in a situation like this?
--
:wq
Jun 27 '08 #8
Richard Tobin wrote:
In article <66*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.
>Which is what any self respecting optimiser will do with a constant.

But C's const variables are not constants. Do the guarantees that
const makes actually allow the compiler to assume the variable's
value in a situation like this?
I don't see why not. The writers of at least two compilers appear to agree:

#include <stdio.h>

const double z = 42.42;

int main(void)
{
printf("%f\n", z );
}

Sun cc:

main:
subl $16,%esp
push $1078277570
push $-1889785610
push $.L16
call printf
addl $28,%esp
xorl %eax,%eax
ret

gcc:

main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
subl $20, %esp
pushl $1078277570
pushl $-1889785610
pushl $.LC1
call printf
addl $16, %esp
leave
ret
--
Ian Collins.
Jun 27 '08 #9
Richard Tobin wrote:
In article <66*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.
>Which is what any self respecting optimiser will do with a constant.

But C's const variables are not constants. Do the guarantees that
const makes actually allow the compiler to assume the variable's
value in a situation like this?
They may not be compile time constants, but they are "read only", so the
compiler is safe to assume their value will not change.

--
Ian Collins.
Jun 27 '08 #10
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <66*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.
>>Which is what any self respecting optimiser will do with a constant.

But C's const variables are not constants. Do the guarantees that
const makes actually allow the compiler to assume the variable's
value in a situation like this?
I think so -- at least, I can't see why not. Any attempt to change a
const object is UB (at least I have not seen a "hole" in the rules) so
the compiler can assume the value is unchanged by the program.
Obviously, a volatile const object can be change by something else by
that is another matter. At least two compilers I've used put const
objects in read-only memory which would be non-conforming otherwise.

--
Ben.
Jun 27 '08 #11
Ian Collins wrote:
CBFalconer wrote:
>Ian Collins wrote:
>>pereges wrote:

I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
You could use #define, but it would be better to use a const double.
Why? Think about it. That way you can't define compile time
constants.
What use is a compile time floating point constant?
Some expressions involving it can be evaluated at compile
time instead of at run time.

#define PI 3.14andsoforth
double theta = PI / 180.0 * degrees;

vs.

extern const double PI;
double theta = PI / 180.0 * degrees;

vs.

extern const double PI;
extern const double ONE_EIGHTY;
double theta = PI / ONE_EIGHTY * degrees;

One might, it's true, have a whole suite of const variables
with values derived from pi:

extern const double PI;
extern const double PI_over_180;
extern const double PI_under_180;
extern const double PI_over_2;
extern const double PI_over_3;
extern const double PI_over_4;
extern const double PI_over_6;
...

To my eye, this is not an improvement but a disimprovement.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #12
Eric Sosman wrote:
Ian Collins wrote:
>CBFalconer wrote:
>>Ian Collins wrote:
pereges wrote:

I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
You could use #define, but it would be better to use a const double.
Why? Think about it. That way you can't define compile time
constants.
What use is a compile time floating point constant?

Some expressions involving it can be evaluated at compile
time instead of at run time.

#define PI 3.14andsoforth
double theta = PI / 180.0 * degrees;

vs.

extern const double PI;
double theta = PI / 180.0 * degrees;
Fair point, I was thinking C++ where defining a const value in a header
doesn't cause multiple definition problems.

Odd that C hasn't adopted this change.

--
Ian Collins.
Jun 27 '08 #13
Ian Collins wrote:
CBFalconer wrote:
>Ian Collins wrote:
>>CBFalconer wrote:
Ian Collins wrote:
pereges wrote:
>
>I need to define the plancks constant 6.67 X 10 exp -34. Is it
>possible to do it using #define or would you suggest using a
>(static ?)const double.
>
You could use #define, but it would be better to use a const
double.

Why? Think about it. That way you can't define compile time
constants.

What use is a compile time floating point constant?

A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.

Which is what any self respecting optimiser will do with a constant.
And which it is not allowed to do with a const (which means
overridable read-only in the C language).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #14
On Apr 14, 5:39*am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Ian Collins wrote:
CBFalconer wrote:
Ian Collins wrote:
pereges wrote:
>>I need to define the plancks constant 6.67 X 10 exp -34. Is it
possible to do it using #define or would you suggest using a
(static ?)const double.
You could use #define, but it would be better to use a const double.
Why? *Think about it. *That way you can't define compile time
constants.
What use is a compile time floating point constant?

* * *Some expressions involving it can be evaluated at compile
time instead of at run time.

* * * * #define PI 3.14andsoforth
* * * * double theta = PI / 180.0 * degrees;

vs.

* * * * extern const double PI;
* * * * double theta = PI / 180.0 * degrees;

vs.

* * * * extern const double PI;
* * * * extern const double ONE_EIGHTY;
* * * * double theta = PI / ONE_EIGHTY * degrees;

* * *One might, it's true, have a whole suite of const variables
with values derived from pi:

* * * * extern const double PI;
* * * * extern const double PI_over_180;
* * * * extern const double PI_under_180;
* * * * extern const double PI_over_2;
* * * * extern const double PI_over_3;
* * * * extern const double PI_over_4;
* * * * extern const double PI_over_6;
* * * * ...

To my eye, this is not an improvement but a disimprovement.
I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?
Jun 27 '08 #15
On Mon, 14 Apr 2008 14:41:17 -0400, CBFalconer wrote:
Ian Collins wrote:
>CBFalconer wrote:
>>A definite point. However, what use is a stored const? Accessing it
requires loading an address and then dereferencing. A compile time
constant just requires loading the value.

Which is what any self respecting optimiser will do with a constant.

And which it is not allowed to do with a const
Which it is allowed to do with a const.
(which means overridable
read-only in the C language).
Which means read-only in the C language, and if you try to modify it
anyway, the behaviour is undefined, and the compiler can make the program
behave as if you didn't really change anything.
Jun 27 '08 #16
CBFalconer wrote:
Ian Collins wrote:
>CBFalconer wrote:
>>Ian Collins wrote:
CBFalconer wrote:
Ian Collins wrote:
>pereges wrote:
>>
>>I need to define the plancks constant 6.67 X 10 exp -34. Is it
>>possible to do it using #define or would you suggest using a
>>(static ?)const double.
>You could use #define, but it would be better to use a const
>double.
Why? Think about it. That way you can't define compile time
constants.
What use is a compile time floating point constant?
A definite point. However, what use is a stored const? Accessing
it requires loading an address and then dereferencing. A compile
time constant just requires loading the value.
Which is what any self respecting optimiser will do with a constant.

And which it is not allowed to do with a const (which means
overridable read-only in the C language).
If it's not allowed how comes compilers do it? There's nothing at all
wrong in using the defined value of a const in an expression.

--
Ian Collins.
Jun 27 '08 #17
user923005 wrote:
>To my eye, this is not an improvement but a disimprovement.

I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?
In C++ you can assign a const in a header.

--
Ian Collins.
Jun 27 '08 #18
user923005 <dc*****@connx.comwrites:
[...]
I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?
<OT>
Ada.

PI : constant := 3.14159265358979323846264;
Answer : constant := 42;

The lack of a type means that "PI" can be used anywhere that the
equivalent floating-point literal can be used; likewise for Answer.
They're called "named numbers". Of course the language also supports
typed constants:

X : constant Long_Float := 3.45;
N : constant Integer := 123;

which are evaluated during compilation.
</OT>

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #19
Keith Thompson <ks***@mib.orgwrites:
user923005 <dc*****@connx.comwrites:
[...]
>I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?

<OT>
Ada.

PI : constant := 3.14159265358979323846264;
Answer : constant := 42;

The lack of a type means that "PI" can be used anywhere that the
equivalent floating-point literal can be used; likewise for Answer.
They're called "named numbers".
That is a neat idea and one that, if it were considered worth while,
would allow such things into C. Since implicit int is out, all we
need is a keyword so there is no ambiguity at the start of a
declaration... Oh, we have one already:

inline pi = 3.14159265358979323846264;

--
Ben.
Jun 27 '08 #20
Ben Bacarisse wrote:
Keith Thompson <ks***@mib.orgwrites:
>user923005 <dc*****@connx.comwrites:
[...]
>>I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?
<OT>
Ada.

PI : constant := 3.14159265358979323846264;
Answer : constant := 42;

The lack of a type means that "PI" can be used anywhere that the
equivalent floating-point literal can be used; likewise for Answer.
They're called "named numbers".

That is a neat idea and one that, if it were considered worth while,
would allow such things into C.
Given that the compiler is free to substitute the constant's value, what
advantage does that give over the C++ way?

--
Ian Collins.
Jun 27 '08 #21
Ian Collins <ia******@hotmail.comwrites:
Ben Bacarisse wrote:
>Keith Thompson <ks***@mib.orgwrites:
>>user923005 <dc*****@connx.comwrites:
[...]
I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?
<OT>
Ada.

PI : constant := 3.14159265358979323846264;
Answer : constant := 42;

The lack of a type means that "PI" can be used anywhere that the
equivalent floating-point literal can be used; likewise for Answer.
They're called "named numbers".

That is a neat idea and one that, if it were considered worth while,
would allow such things into C.

Given that the compiler is free to substitute the constant's value, what
advantage does that give over the C++ way?
Not much but the C++ is not an option for the committee, I suspect
(too much to unpick and too many programs might break).

--
Ben.
Jun 27 '08 #22
Ben Bacarisse wrote:
Ian Collins <ia******@hotmail.comwrites:
>Ben Bacarisse wrote:
>>Keith Thompson <ks***@mib.orgwrites:

user923005 <dc*****@connx.comwrites:
[...]
I have always wished that there was a better way than #define and
extern to create numeric constants in the C language. I don't really
know of any language that has an excellent fascility for this. Is
there one?
<OT>
Ada.

PI : constant := 3.14159265358979323846264;
Answer : constant := 42;

The lack of a type means that "PI" can be used anywhere that the
equivalent floating-point literal can be used; likewise for Answer.
They're called "named numbers".
That is a neat idea and one that, if it were considered worth while,
would allow such things into C.
Given that the compiler is free to substitute the constant's value, what
advantage does that give over the C++ way?

Not much but the C++ is not an option for the committee, I suspect
(too much to unpick and too many programs might break).
I don't think too many programs would break, no one defines constants in
headers in C.

--
Ian Collins.
Jun 27 '08 #23
Ian Collins <ia******@hotmail.comwrites:
Ben Bacarisse wrote:
>Ian Collins <ia******@hotmail.comwrites:
<snip off-the-wall "inline variable" idea>
>>Given that the compiler is free to substitute the constant's value, what
advantage does that give over the C++ way?

Not much but the C++ is not an option for the committee, I suspect
(too much to unpick and too many programs might break).
I don't think too many programs would break, no one defines constants in
headers in C.
I'll take your word for it (no, I really will -- I am not being
sarcastic). I just assumed that, since the desire was that C and C++
should not drift too far apart, importing C++'s idea of const into C
had already been ruled out (after all C++ const in it's current form
long before C99).

--
Ben.
Jun 27 '08 #24
Ian Collins wrote:
>
.... snip ...
>
I don't think too many programs would break, no one defines
constants in headers in C.
Oh? Just for one example, how about <limits.h>?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #25
CBFalconer wrote:
Ian Collins wrote:
.... snip ...
>I don't think too many programs would break, no one defines
constants in headers in C.

Oh? Just for one example, how about <limits.h>?
Nope, they're #defines.

I was referring to "const" constants.

--
Ian Collins.
Jun 27 '08 #26
Ben Bacarisse <be********@bsb.me.ukwrote:
>
That is a neat idea and one that, if it were considered worth while,
would allow such things into C. Since implicit int is out, all we
need is a keyword so there is no ambiguity at the start of a
declaration... Oh, we have one already:

inline pi = 3.14159265358979323846264;
That should be:

inline static pi = 3.14159265358979323846264;

to satisfy the unwritten rule that any new feature has to include a new
use for "static". ;-)

-Larry Jones

Buddy, if you think I'm even going to BE here, you're crazy! -- Calvin
Jun 27 '08 #27
On Tue, 15 Apr 2008 18:09:33 +1200, Ian Collins wrote:
CBFalconer wrote:
>Ian Collins wrote:
.... snip ...
>>I don't think too many programs would break, no one defines constants
in headers in C.

Oh? Just for one example, how about <limits.h>?
Nope, they're #defines.

I was referring to "const" constants.
I would be surprised if no implementation does

extern FILE *const __stdin;
#define stdin __stdin

(I know, this is also not what you meant.)
Jun 27 '08 #28

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ddh | last post: by
6 posts views Thread by peter_k | last post: by
58 posts views Thread by mailursubbu | last post: by
7 posts views Thread by Chris Saunders | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.