Tech Off Thread

15 posts

Math question

Back to Forum: Tech Off
  • User profile image
    phreaks

    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..

  • User profile image
    JChung2006

    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?

  • User profile image
    phreaks

    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

  • User profile image
    figuerres

    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


  • User profile image
    phreaks

    thnx

  • User profile image
    Matthew van Eerde

    0 - y also works
    Or just -y

  • User profile image
    Ion Todirel

    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
    Expressionless do i miss something? don't you just do -number?

  • User profile image
    phreaks

    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
    do i miss something? don't you just do -number?


    Ya, but there is no math.inverse(number) method.

    int x =9;
    x = -x;

    Can you do that?

    EDIT: apparently you can. Duh!

    Thanks.

  • User profile image
    JoshB

    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.

  • User profile image
    Detroit Muscle

    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.

  • User profile image
    Cannot​Resolve​Symbol

    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.

  • User profile image
    evildictait​or

    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

  • User profile image
    Rossj

    JoshB wrote:
    
    -sigh-

    C# just became the Mc Donalds of programming languages.


    Is that a good thing?

  • User profile image
    AndyC

    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).

  • User profile image
    Matthew van Eerde

    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. Smiley

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.