473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

#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 5373
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Steve | last post by:
Sorry in advance for my ignorance. Any help would sure be appreciated. I'm writing a fairly simple application with VB.Net and am obviously a bit of a newbie. This application will be used by 1,...
5
by: ddh | last post by:
Hi, I have a map type such as std::map<int, int>, which I used to manage many values in a fixed order. But I have thousands of values to be inserted into the map, and in some case, the action of...
6
by: peter_k | last post by:
Hi, Last time i'm interested in optimizing small c programs. On my studies we are sending the programs using the web interface to online judge. The person who will wrote the faster program get...
58
by: mailursubbu | last post by:
Hi How to write a program to get the factorial of 4096. I am working on a Linux m/c. Best Regards, Subra
6
by: 05l8kr | last post by:
I'm suppose to edit the function called swap_values so that it receives three numbers (not two nubmers) called first , second, and third. These numbers will not be in any special order (for example...
22
by: David T. Ashley | last post by:
I'm writing code which will compile on multiple Windows and multiple *nix platforms. Some will have 64-bit integers, I believe. What is my best option to define platform-independent types such...
10
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask...
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
7
by: Chris Saunders | last post by:
I'm not very familiar with C#. I have a static class and would like to define some constants similarily to Math.PI. Could someone show me how to go about doing this. So far no luck searching. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.