unary operator- question

J

John J

To write a unary operator- operation that returns the negative of a value is
it just as simple as subtracting the value from itself?

ie.

{
return (value - value);
}

Thanks
 
J

John Carson

John J said:
To write a unary operator- operation that returns the negative of a
value is it just as simple as subtracting the value from itself?

ie.

{
return (value - value);
}

Thanks


Does 5-5 equal -5?
 
J

John J

I can see this is going to be fun!

OK then, 5 - 5 != -5

Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -

Tks
 
M

Mark A. Gibbs

John said:
I can see this is going to be fun!

OK then, 5 - 5 != -5

If you really want to do it that way, value - (2 * value) would do the
trick, I guess.
Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -

Honestly, I'm trying to decode what the hell you mean, and failing.
After all:

int two = 2;
int negative_two = -two;

That's what you asked for if I were to take the literal interpretation
of your question.

But if you're trying to write the negation operator for some class, the
answer is that it depends on what's in your class. I mean, for example:

class number
{
public:
int value;

number operator-()
{
number result;
/*** --> ***/ result.value = -value; /*** <-- ***/
return result;
}
};

Or perhaps this:

class number
{
public:
bool negative;
unsigned int value;

number operator-()
{
number result;
result.value = value;
/*** --> ***/ result.negative = !negative; /*** <-- ***/
return result;
}
};

So I ask - what are you talking about?

mark
 
F

Frederic Banaszak

I can see this is going to be fun!

OK then, 5 - 5 != -5

Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -

I'm sure that a lot of people here are capable of writing your code,
and I'll bet that you are, too.

My advice is to step away from the computer, grab a piece of paper and
a pencil, and figure out how you would do it without the use of a
computer. Once you figure out the method, or algorithm, I'm sure that
you will see that writing the actual code is easy.

The most important thing in programming is not the hardware, or the
compiler, or even the language. The most important thing is that gray
squishy thing between your ears.
 
D

David Harmon

Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -

"unary" means only one operand.
 
R

Rolf Magnus

John said:
I can see this is going to be fun!

OK then, 5 - 5 != -5

Is anyone capable of providing some advice on the syntax I should be
looking at?

That depends on your class. Try to find out what it means to an object
of your class to be negated.
I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -

Again, that depends on the meaning of your value. There is no universal
code for it. If you have a binary operator-, it might (or might not) be
possible to write the unary one as returning 0 - thevalue.
 
O

osmium

John said:
To write a unary operator- operation that returns the negative of a value is
it just as simple as subtracting the value from itself?

ie.

{
return (value - value);
}

You need a language maven to answer that question. My best guess is that
the post you made is a binary operator. But I don't feel like traipsing
thought the BNF to try and arrive at an official answer. I have the
unsettling feeling I would have a good chance of getting it wrong anyhow,
after all that effort. If you don't get a suitable answer here, try posting
to one of the groups that have "standard" coded somehow in the name of the
group.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top