Velocity Reviews > Good C programming style

# 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

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.

Sensei
Guest
Posts: n/a

 10-10-2005
On 2005-10-09 21:34:29 +0200, cs <(E-Mail Removed)> 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 <(E-Mail Removed)>

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

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

Richard Tobin
Guest
Posts: n/a

 10-10-2005
In article <didqfs\$8ct\$(E-Mail Removed)>,
Chris Dollin <(E-Mail Removed)> 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

cs
Guest
Posts: n/a

 10-10-2005
On Mon, 10 Oct 2005 14:29:18 +0200, Sensei <(E-Mail Removed)> wrote:
>On 2005-10-09 21:34:29 +0200, cs <(E-Mail Removed)> 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

cs
Guest
Posts: n/a

 10-10-2005
On Mon, 10 Oct 2005 17:16:33 +0200, cs <(E-Mail Removed)> wrote:
>On Mon, 10 Oct 2005 14:29:18 +0200, Sensei <(E-Mail Removed)> wrote:
>>On 2005-10-09 21:34:29 +0200, cs <(E-Mail Removed)> 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

Sensei
Guest
Posts: n/a

 10-10-2005
On 2005-10-10 15:31:40 +0200, Chris Dollin <(E-Mail Removed)> 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 <(E-Mail Removed)>

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

Keith Thompson
Guest
Posts: n/a

 10-10-2005
Sensei <(E-Mail Removed)> writes:
> On 2005-10-10 15:31:40 +0200, Chris Dollin <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.

Chris Dollin
Guest
Posts: n/a

 10-11-2005
Sensei wrote:

> On 2005-10-10 15:31:40 +0200, Chris Dollin <(E-Mail Removed)> 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?"

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jeti789@web.de C++ 52 04-06-2013 03:30 AM Astley Le Jasper Python 5 09-15-2008 02:41 PM Fredrik Lundh Python 4 04-07-2006 06:19 AM Vincent Foley Ruby 2 04-29-2005 10:31 PM Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM

Advertisments