#define for very small numbers ?

P

pereges

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.
 
I

Ian Collins

pereges said:
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.
 
I

Ian Collins

CBFalconer said:
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.
 
R

Richard Tobin

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.
[/QUOTE]
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

Ian Collins

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?[/QUOTE]

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
 
I

Ian Collins

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?[/QUOTE]

They may not be compile time constants, but they are "read only", so the
compiler is safe to assume their value will not change.
 
B

Ben Bacarisse

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?[/QUOTE]

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.
 
I

Ian Collins

Eric said:
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.
 
U

user923005

     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?
 
H

Harald van Dijk

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.
 
I

Ian Collins

CBFalconer said:
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.
 
I

Ian Collins

user923005 said:
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.
 
K

Keith Thompson

user923005 said:
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>
 
B

Ben Bacarisse

Keith Thompson said:
<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;
 
I

Ian Collins

Ben said:
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?
 
B

Ben Bacarisse

Ian Collins said:
Ben said:
Keith Thompson said:
[...]
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

Ian Collins

Ben said:
Ian Collins said:
Ben said:
[...]
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.
 
B

Ben Bacarisse

Ian Collins said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top