K
karthikbalaguru
Hi,
What is the use of the Unary + Operator ?
Thx in advans,
Karthik Balaguru
What is the use of the Unary + Operator ?
Thx in advans,
Karthik Balaguru
not much some ppl like it as a defense against
if (x = y) ...
they rite
if (+x = y) ...
instead, which the cplr will complain about . unless there
r Macros involved i think its slly ur wlcm
karthikbalaguru said:Cool !!
I was eager to know of its use becuase some
referred it as dummy operator.
So, it is applicable only in this scenario and not
in other places.
... A better example would be in a list of constants
where you wanted to make the sign of every constant,
both positive and negative, explicit rather than only
putting a sign on the negative ones, which might make
it easier to read or help detect typos.
However, I suspect that it was included mainly for
completeness and because there was nothing else one
could reasonably do with a unary + that wouldn't
confuse a lot of programmers.
Peter said:Which people like this?
blargg said:Stephen Sprunk wrote:
[...]However, I suspect that it [unary +] was included mainly for
completeness and because there was nothing else one could
reasonably do with a unary + that wouldn't confuse a lot of
programmers.
It could have been made illegal. Then this thread would instead be asking
"Why is unary + illegal? It could be useful for noting positive constants
among several negative ones."
> "blargg" wrote: ....
>
> That's the best response we are likely to see.
Dik T. Winter said:The unary + was added in the process for a fairly bizarre reason.
Originally in C all expressions could be rearranged when the
*mathematical* result would remain the same, it did not matter
whether the *numerical* result would remain the same. This gave
numerical mathematicians problems. Of course they could use
additional variables and subexpressions, but in many cases the
result would not be nice to look at. So it was invented that if an
expression between parenthesis was preceded by a unary +, the
subexpression had to be calculated as is, and parts could not be
moved out from it. So: a + +(b - a) must first calculate b-a and
afterwards add the result to a. On the other hand: a + (b - a)
could be rearranged, the compiler would see that the a's cancel and
could give only b as result of the expression.
That was the start of the career of unary + in C.
Of course it is no longer necessary for that purpose as the current
wording is that an expression can be rearranged if the *numerical*
result remains the same, and so in floating-point, a + (b - a) can
not be rearranged at all.
Dik T. Winter said:.... snip ....
On the other hand:
a + (b - a)
could be rearranged, the compiler would see that the a's cancel
and could give only b as result of the expression.
That was the start of the career of unary + in C.
Of course it is no longer necessary for that purpose as the
current wording is that an expression can be rearranged if the
*numerical* result remains the same, and so in floating-point,
a + (b - a) can not be rearranged at all.
....
> That's an interesting explanation, and one that I hadn't heard before.
> "Dik T. Winter" wrote: ....
>
> Huh? Why can't it be rearranged, since cancelling a's produces the
> same result, barring overflow conditions?
>
> Not in *floating-point*. Rounding can be different. Consider the following
> code given some (positive) a and b with a > b where the requirement is that
> after the operations
> CBFalconer wrote: ....
>
> Are you sure you aren't Han?
>
> Try it with |a|>>|b|
>
> Say a = 1.0e30 and b = 0.5e0.
Keith Thompson said:Here's what the the rationale for the 1989 ANSI C standard says
<http://www.lysator.liu.se/c/rat/c3.html#3-3-3-3>:
Unary plus was adopted by the Committee from several
implementations, for symmetry with unary minus.
It may be that those "several implementations" added unary + for the
reasons you've presented.
pete said:Floating point addition isn't associative in C.
You can have
(x + y + z < x + (y + z))
be true, if x, y, and z are floating types.
CBFalconer said:Fair enough, if you include floats, which are always intrinsically
approximations. I was considering integral value only.
> pete wrote:
> [...]>> > Floating point addition isn't associative in C.
> >
> > You can have
> >
> > (x + y + z < x + (y + z))
> >
> > be true, if x, y, and z are floating types.
> I assume you're talking about rounding possibly resulting in different
> results?
>
> However, barring side-effects and volatiles, can't the compiler treat
> the above as "always false"?
>
> No, I think using unary + to enforce order of evaluation was a committee
> invention (and a not very good one, which is why it didn't make it into
> the standard).
> I don't remember whether unary + was proposed on its own
> for symmetry before the evaluation ordering proposal or if it was just a
> secondary reason for adopting it.
> In any event, there's nothing that
> says the committee has to document their mistakes.![]()
Keith said:So you didn't see the phrase "and so in floating-point" in Dik's
article? You even quoted it.
....
> Fair enough, if you include floats, which are always intrinsically
> approximations. I was considering integral value only.
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.