|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In article <6f6cd5eb-2f61-4a57-b597-901127c97f41@w8g2000prd.googlegroups.com>,
pereges <Broli00@gmail.com> wrote: >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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 ** |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 ** |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
In article <66gdk8F2kg6m6U9@mid.individual.net>,
Ian Collins <ian-news@hotmail.com> 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. 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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Richard Tobin wrote:
> In article <66gdk8F2kg6m6U9@mid.individual.net>, > Ian Collins <ian-news@hotmail.com> 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. > > 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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Richard Tobin wrote:
> In article <66gdk8F2kg6m6U9@mid.individual.net>, > Ian Collins <ian-news@hotmail.com> 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. > > 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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> In article <66gdk8F2kg6m6U9@mid.individual.net>, > Ian Collins <ian-news@hotmail.com> 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. > > 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. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 esosman@ieee-dot-org.invalid |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 ** |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
On Apr 14, 5:39am, Eric Sosman <esos...@ieee-dot-org.invalid> 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; > > 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? |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
user923005 <dcorbit@connx.com> writes:
[...] > 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) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
Keith Thompson <kst-u@mib.org> writes:
> user923005 <dcorbit@connx.com> writes: > [...] >> 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. |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
Ben Bacarisse wrote:
> Keith Thompson <kst-u@mib.org> writes: > >> user923005 <dcorbit@connx.com> writes: >> [...] >>> 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. |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
Ian Collins <ian-news@hotmail.com> writes:
> Ben Bacarisse wrote: >> Keith Thompson <kst-u@mib.org> writes: >> >>> user923005 <dcorbit@connx.com> writes: >>> [...] >>>> 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. |
|
|
|
#23 |
|
Messages: n/a
Hébergeur: |
Ben Bacarisse wrote:
> Ian Collins <ian-news@hotmail.com> writes: > >> Ben Bacarisse wrote: >>> Keith Thompson <kst-u@mib.org> writes: >>> >>>> user923005 <dcorbit@connx.com> writes: >>>> [...] >>>>> 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. |
|
|
|
#24 |
|
Messages: n/a
Hébergeur: |
Ian Collins <ian-news@hotmail.com> writes:
> Ben Bacarisse wrote: >> Ian Collins <ian-news@hotmail.com> writes: <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. |
|
|
|
#25 |
|
Messages: n/a
Hébergeur: |
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 ** |
|
![]() |
| Outils de la discussion | |
|
|