PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > #define for very small numbers ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
#define for very small numbers ?

Réponse
 
LinkBack Outils de la discussion
Vieux 13/04/2008, 19h01   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut #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.
  Réponse avec citation
Vieux 13/04/2008, 19h52   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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
  Réponse avec citation
Vieux 13/04/2008, 20h57   #3
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 05h19   #4
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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 **
  Réponse avec citation
Vieux 14/04/2008, 05h40   #5
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 06h51   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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 **
  Réponse avec citation
Vieux 14/04/2008, 07h58   #7
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 10h21   #8
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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
  Réponse avec citation
Vieux 14/04/2008, 10h33   #9
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 10h35   #10
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 10h37   #11
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 13h39   #12
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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
  Réponse avec citation
Vieux 14/04/2008, 19h41   #13
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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 **
  Réponse avec citation
Vieux 14/04/2008, 20h35   #14
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 20h48   #15
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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?
  Réponse avec citation
Vieux 14/04/2008, 21h01   #16
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 21h02   #17
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 21h04   #18
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 14/04/2008, 23h46   #19
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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"
  Réponse avec citation
Vieux 15/04/2008, 00h31   #20
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 15/04/2008, 00h42   #21
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 15/04/2008, 01h10   #22
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 15/04/2008, 01h13   #23
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 15/04/2008, 01h56   #24
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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.
  Réponse avec citation
Vieux 15/04/2008, 06h12   #25
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: #define for very small numbers ?

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 **
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,37791 seconds with 33 queries