Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Good C programming style

Reply
Thread Tools

Good C programming style

 
 
Gregory Pietsch
Guest
Posts: n/a
 
      10-10-2005
(Concerning the accuracy of pi)

With 42 digits of pi, you could find the circumference of the known
universe to within the width of a proton, but for practical matters, an
accuracy of one or two decimal places is ample.

LOL, Gregory Pietsch

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      10-10-2005
Gregory Pietsch wrote:
> (Concerning the accuracy of pi)
>
> With 42 digits of pi, you could find the circumference of the known
> universe to within the width of a proton, but for practical matters, an
> accuracy of one or two decimal places is ample.


It all depends on what you are using PI for. The 1% or 0.1% error may
only be one error factor within the calculation, and it may be scaled up
by other things. I have certainly used PI in calculations where I have
also used other approximations (ones which actually saved a significant
amount of processing time). So if you are trying to reduce the error the
place to start is with the accuracy of the constants, because you can
improve those up to what your implementation can cope with without
impacting on other aspects of performance. Of course, a far easier
approach is to never introduce error in constants for the sake of a
little typing.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
 
 
 
Sensei
Guest
Posts: n/a
 
      10-10-2005
On 2005-10-09 21:34:29 +0200, cs <> said:
>
> for me it is not a good idea for debug puorpose and ... because it is
> more easy to write errors
>
> the only macros that i like are
> #define P printf
> #define F for
> #defile W while
> #define U unsigned
> #define R return
>
> but no one seems agree in this with me ...



Ok. I see.

So why I see so often something like:

#define CEXCERPT do { \
some(); \
C_code(); \
here(); \
} while(0)

Is it just some person thinking he would achieve more speed?

--
Sensei <>

The difference between stupidity and genius is that genius has its
limits. (A. Einstein)

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-10-2005
Sensei wrote:

> So why I see so often something like:
>
> #define CEXCERPT do { \
> some(); \
> C_code(); \
> here(); \
> } while(0)
>
> Is it just some person thinking he would achieve more speed?


No, it's some person arranging that their macro can be expanded
as
CEXCERPT;

without confusion. (I would have thought, even more likely
with a parameterised macro.)

--
Chris "electric hedgehog" Dollin
"I know three kinds: hot, cool, and what-time-does-the-tune-start?"
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      10-10-2005
In article <didqfs$8ct$>,
Chris Dollin <> wrote:

>> #define CEXCERPT do { \
>> some(); \
>> C_code(); \
>> here(); \
>> } while(0)


>No, it's some person arranging that their macro can be expanded
>as
> CEXCERPT;
>
>without confusion.


In particular, as the body of an if. If the macro were defined as just
the bracketed part, then

if(test)
CEXCERPT;
else
...

would be an error, and

if(test1)
if(test2)
CEXCERPT;
else
...

would not behave as suggested by the indentation.

-- Richard

 
Reply With Quote
 
cs
Guest
Posts: n/a
 
      10-10-2005
On Mon, 10 Oct 2005 14:29:18 +0200, Sensei <> wrote:
>On 2005-10-09 21:34:29 +0200, cs <> said:
>>
>> for me it is not a good idea for debug puorpose and ... because it is
>> more easy to write errors
>>
>> the only macros that i like are
>> #define P printf
>> #define F for
>> #defile W while
>> #define U unsigned
>> #define R return
>>
>> but no one seems agree in this with me ...

>
>
>Ok. I see.
>
>So why I see so often something like:
>
>#define CEXCERPT do { \
> some(); \
> C_code(); \
> here(); \
> } while(0)
>
>Is it just some person thinking he would achieve more speed?


it is a trick
because some people want a more than one instruction macro and
they want save a call (in some loop i think)
a=1;
CEXCERPT;
b=1;

is expanded in
a=1;
{ some(); C_code(); here(); } while(0);
b=1;

so in this way you can use ";" for end the instruction (CEXCERPT

note that in this way
if(a==rand()%9)
CEXCERPT;
else b=1;
is also ok
is expanded in

if(a==rand()%9)
{ some(); C_code(); here(); } while(0);
else b=1;

the same seem ok
if(a==rand()%9)
{ CEXCERPT; }
while(a==rand()%9)
CEXCERPT;
while(a==rand()%9)
{ CEXCERPT; }
etc
 
Reply With Quote
 
cs
Guest
Posts: n/a
 
      10-10-2005
On Mon, 10 Oct 2005 17:16:33 +0200, cs <> wrote:
>On Mon, 10 Oct 2005 14:29:18 +0200, Sensei <> wrote:
>>On 2005-10-09 21:34:29 +0200, cs <> said:
>>>
>>> for me it is not a good idea for debug puorpose and ... because it is
>>> more easy to write errors
>>>
>>> the only macros that i like are
>>> #define P printf
>>> #define F for
>>> #defile W while
>>> #define U unsigned
>>> #define R return
>>>
>>> but no one seems agree in this with me ...

>>
>>
>>Ok. I see.
>>
>>So why I see so often something like:
>>
>>#define CEXCERPT do { \
>> some(); \
>> C_code(); \
>> here(); \
>> } while(0)
>>
>>Is it just some person thinking he would achieve more speed?

>
>it is a trick
>because some people want a more than one instruction macro and
>they want save a call (in some loop i think)
>a=1;
>CEXCERPT;
>b=1;


i would like to say this

>is expanded in
>a=1;

do { some(); C_code(); here(); } while(0);
>b=1;
>
>so in this way you can use ";" for end the instruction (CEXCERPT
>
>note that in this way
>if(a==rand()%9)
> CEXCERPT;
>else b=1;
>is also ok
>is expanded in
>
>if(a==rand()%9)

do { some(); C_code(); here(); } while(0);
>else b=1;
>
>the same seem ok
>if(a==rand()%9)
> { CEXCERPT; }
>while(a==rand()%9)
> CEXCERPT;
>while(a==rand()%9)
> { CEXCERPT; }
>etc

 
Reply With Quote
 
Sensei
Guest
Posts: n/a
 
      10-10-2005
On 2005-10-10 15:31:40 +0200, Chris Dollin <> said:

> Sensei wrote:
>
>> So why I see so often something like:
>>
>> #define CEXCERPT do { \
>> some(); \
>> C_code(); \
>> here(); \
>> } while(0)
>>
>> Is it just some person thinking he would achieve more speed?

>
> No, it's some person arranging that their macro can be expanded
> as CEXCERPT;
>
> without confusion. (I would have thought, even more likely
> with a parameterised macro.)



I understood why he does not use a ; at the end, but why someone would
have such a big macro... is beyond my knowledge, something like the
xge_whatever stuff I found...

--
Sensei <>

The difference between stupidity and genius is that genius has its
limits. (A. Einstein)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-10-2005
Sensei <> writes:
> On 2005-10-10 15:31:40 +0200, Chris Dollin <> said:
>> Sensei wrote:
>>> So why I see so often something like:
>>> #define CEXCERPT do { \
>>> some(); \
>>> C_code(); \
>>> here(); \
>>> } while(0)
>>> Is it just some person thinking he would achieve more speed?

>> No, it's some person arranging that their macro can be expanded
>> as CEXCERPT;
>> without confusion. (I would have thought, even more likely
>> with a parameterised macro.)

>
> I understood why he does not use a ; at the end, but why someone would
> have such a big macro... is beyond my knowledge, something like the
> xge_whatever stuff I found...


One reason to do this is to save the overhead of a function call.
That's rarely worth the effort, though it can be if it's likely to be
invoked repeatedly in a loop. For example, putc() in <stdio.h> is
commonly implemented as a complex macro; likewise for the is*() and
to*() functions/macros in <ctype.h>.

Another reason is to do something that you can't do with a function,
even an inline one. For example, a function argument can only be an
expression; a macro argument can be a type name. The offsetof() macro
is an example that can't be implemented as a function.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-11-2005
Sensei wrote:

> On 2005-10-10 15:31:40 +0200, Chris Dollin <> said:
>
>> Sensei wrote:
>>
>>> So why I see so often something like:
>>>
>>> #define CEXCERPT do { \
>>> some(); \
>>> C_code(); \
>>> here(); \
>>> } while(0)
>>>
>>> Is it just some person thinking he would achieve more speed?

>>
>> No, it's some person arranging that their macro can be expanded
>> as CEXCERPT;
>>
>> without confusion. (I would have thought, even more likely
>> with a parameterised macro.)

>
>
> I understood why he does not use a ; at the end, but why someone would
> have such a big macro... is beyond my knowledge, something like the
> xge_whatever stuff I found...


How big is "big"?

#define NEEDS(mill, n) \
do { \
if (vm->myHeap->available < (Size) (n)) \
(FREEZE(), millRegenerate( mill ), MELT()); \
} while (0)

--
Chris "electric hedgehog" Dollin
"I know three kinds: hot, cool, and what-time-does-the-tune-start?"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good idea or gimmick: Go-style OO-programming in C++ ? jeti789@web.de C++ 52 04-06-2013 03:30 AM
Good programming style Astley Le Jasper Python 5 09-15-2008 02:41 PM
good style guides for python-style documentation ? Fredrik Lundh Python 4 04-07-2006 06:19 AM
Could you tell me if this is good meta programming style? Vincent Foley Ruby 2 04-29-2005 10:31 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57