Best way to find the inverse of a number?
I want to return the inverse of a number.
So now I can add the number to itself multiplied by negative 2.
x = y + (y * 2)
Or this x = y * (1) is a little simpler, but...
I know there's a simpler way to do this..


y + (y * 2) = y = the additive inverse of y
Yet you tried to get the multiplicative inverse initially by raising the number to the 1 power?
Which inverse did you want?

JChung2006 wrote:
y + (y * 2) = y = the additive inverse of y
Yet you tried to get the multiplicative inverse initially by raising the number to the 1 power?
Which inverse did you want?
I want to turn 5 into 5 or 10 into 10 
phreaks wrote:Best way to find the inverse of a number?
I thouhgt I could raise the number to the power of negative 1 and it would return the inverse, but sadly; my math must be rusty.
So now I am adding the number to itself multiplied by negative 2.
x = y + (y * 2)
I know there's a simpler way to do this..
NegValue = Value *1
where say Value is decimal 1.25 and NegValue is also deciaml then just
NegValue = Value *1.0M;
and NegValue will be 1.25M;
HTH

0  y also works
Or just y 
phreaks wrote:
JChung2006 wrote:
y + (y * 2) = y = the additive inverse of y
Yet you tried to get the multiplicative inverse initially by raising the number to the 1 power?
Which inverse did you want?
I want to turn 5 into 5 or 10 into 10

Ion Todirel wrote:
phreaks wrote:
JChung2006 wrote:
y + (y * 2) = y = the additive inverse of y
Yet you tried to get the multiplicative inverse initially by raising the number to the 1 power?
Which inverse did you want?
I want to turn 5 into 5 or 10 into 10
Ya, but there is no math.inverse(number) method.
int x =9;
x = x;
Can you do that?
EDIT: apparently you can. Duh!
Thanks. 
WHAT THE HELL!!!
x? That's it. I can't believe I've been using C# this long and haven't caught on to that.
sigh
C# just became the Mc Donalds of programming languages. 
phreaks wrote:
I know there's a simpler way to do this..
If you are only dealing with integers, the operation is flip the bits and add one. Modern computers store integers in what's known as "twos compliment" notation.
The CPU you are targeting probably has a "compliment" instruction to flip the bits and an "increment" instruction to add 1. Two clock cycles is about as good as your going to get on most modern CPUs.
Although, I've seen some CISC machines with a "negate" instruction, which has the effect of the combined two instructions mentioned above.

Detroit Muscle wrote:
phreaks wrote:
I know there's a simpler way to do this..
If you are only dealing with integers, the operation is flip the bits and add one. Modern computers store integers in what's known as "twos compliment" notation.
The CPU you are targeting probably has a "compliment" instruction to flip the bits and an "increment" instruction to add 1. Two clock cycles is about as good as your going to get on most modern CPUs.
Although, I've seen some CISC machines with a "negate" instruction, which has the effect of the combined two instructions mentioned above.
Both x86 assembly and .Net intermediate code (CIL) have a negate instruction. I can't seem to find the number of cycles they're supposed to take, though.

CannotResolveSymbol wrote:
Both x86 assembly and .Net intermediate code (CIL) have a negate instruction. I can't seem to find the number of cycles they're supposed to take, though.
Negate is a cheap operation. It's slightly more expensive than AND and OR, but less expensive than ADD, and a lot faster than MOV, IMUL and IDIV

JoshB wrote:
sigh
C# just became the Mc Donalds of programming languages.
Is that a good thing?

Detroit Muscle wrote:
The CPU you are targeting probably has a "compliment" instruction to flip the bits and an "increment" instruction to add 1. Two clock cycles is about as good as your going to get on most modern CPUs.
The Pentium can do a negate on a register in 1 clock cycle, it takes slightly longer if the operand is in memory (3 cycles IIRC). 
Detroit Muscle wrote:
phreaks wrote:
I know there's a simpler way to do this..
If you are only dealing with integers, the operation is flip the bits and add one. Modern computers store integers in what's known as "twos compliment" notation.
The CPU you are targeting probably has a "compliment" instruction to flip the bits and an "increment" instruction to add 1. Two clock cycles is about as good as your going to get on most modern CPUs.
Although, I've seen some CISC machines with a "negate" instruction, which has the effect of the combined two instructions mentioned above.
Hey, he said a simpler way.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.