Help with this little line of code please...

A

almurph

Hi everyone

I have the following line of code in C:

((x) <= 0 ? 0.0 : 1.0)

I understand that this is shorthand for the following:

if(x <= 0)
{
return 0.0;
}
else
{
return 1.0;
}


what I am confused by is the brackets around the x like "(x)". I don't
know what this means. Can ayone help me with the interpretation
please? I would appreciate any comments/suggestions/explanations that
you may be able to offer.

Thanks,
Al.
 
T

Thomas Maier-Komor

Hi everyone

I have the following line of code in C:

((x) <= 0 ? 0.0 : 1.0)

I understand that this is shorthand for the following:

if(x <= 0)
{
return 0.0;
}
else
{
return 1.0;
}


what I am confused by is the brackets around the x like "(x)". I don't
know what this means. Can ayone help me with the interpretation
please? I would appreciate any comments/suggestions/explanations that
you may be able to offer.

Thanks,
Al.

I guess, you've read the line in question in a #define preprocessor
statement. If this is the case the brackets around x prevent priority
inversion over the <= operator.

If it is not with a #define, the brackets are superfluous, if x is an
identifier. If x is a macro, it might again become necessary to prevent
the same operator priority inversion with <=.


HTH,
Thomas
 
A

almurph

(e-mail address removed) schrieb:












I guess, you've read the line in question in a #define preprocessor
statement. If this is the case the brackets around x prevent priority
inversion over the <= operator.

If it is not with a #define, the brackets are superfluous, if x is an
identifier. If x is a macro, it might again become necessary to prevent
the same operator priority inversion with <=.

HTH,
Thomas- Hide quoted text -

- Show quoted text -

Hi Thomas,

Thanks for the speedy response. Your right "x" is in a macro, but
unfortunately I don't understand your statement "prevent priority
inversion over the <= operator." Could you explain this to me?

Sorry for being such a pleb,
Al.
 
J

James Kuyper

Hi everyone

I have the following line of code in C:

((x) <= 0 ? 0.0 : 1.0)

I understand that this is shorthand for the following:

if(x <= 0)
{
return 0.0;
}
else
{
return 1.0;
}

Sort-of. It's actually an expression, not a function, and therefore it
has a value, rather than returning a value.
what I am confused by is the brackets around the x like "(x)". I don't
know what this means.

The parentheses around the 'x' don't do anything directly; they're
exactly like the parentheses around (5+3). However, I'd guess that this
piece of code was probably the definition of a function-like macro, as
follows:

#define POSITIVE(x) ((x) <= 0 ? 0.0 : 1.0)

In that context, if 'x' is an expression, that expression might contain
an operator of lower precedence than <=, such as a == b. POSITIVE(a ==
b) expands to

((a == b) <= 0 ? 0.0 : 1.0).

Without the parenthesis, it would expand to

( a == b <= 0 ? 0.0 : 1.0)

which would be equivalent to

( a == (b <= 0) ? 0.0 : 1.0)

which is probably not what was wanted. The outermost parentheses serve
the same purpose: to prevent this expression from being unexpected
intermixed with other expressions.
 
J

James Kuyper

Eric said:
Hi everyone

I have the following line of code in C:

((x) <= 0 ? 0.0 : 1.0)
[...]

Just as an aside, the fragment might be more compactly
written as

(double)(x > 0)

For the reasons I explained elsewhere on this thread, make that

((double)((x) > 0))
 
K

Keith Thompson

Eric Sosman said:
Hi everyone

I have the following line of code in C:

((x) <= 0 ? 0.0 : 1.0)
[...]

Just as an aside, the fragment might be more compactly
written as

(double)(x > 0)

Or, since it's a macro definition:

((double)((x) > 0))

But note that the behavior could be different in the presence of NaNs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top