Results 1 to 7 of 7
  1. #1
    Member Xtreme Barton's Avatar
    Join Date
    Jan 2004
    Location
    Omaha,NE

    Java - Variable = all negative ??

    is there a simple asignment statement to make a variable all negative numbers



  2. #2
    Member
    Join Date
    Nov 2008
    I don't think there's a way to do this without first checking to see if the number itself is positive. (I'm not familiar w/ all of the Java standard libs, though.) If there is, it's probably some sort of bitwise trickery involving the two's complement representation of the number.

    Instead, you'll need to do something like the following:
    Code:
    int var1 = 5;
    
    int var2 = var1;                // var2 is now 5
    if(var2 > 0) { var2 = -var2; }  // var2 is now -5
    
    // or, using the ternary operator:
    int var3 = (var1 > 0) ? -var1 : var1;   // var3 is now -5
    That last example uses the ternary operator which can be a nice shorthand when you have a simple if/else and want to assign from it. It can also turn code into an unreadable mess if you aren't careful with it.
    Last edited by pcarini; 03-27-12 at 12:25 AM. Reason: Better example
    "We can't stop here, this is bat country!"

  3. #3
    Member dropadrop's Avatar
    10 Year Badge
    Join Date
    Mar 2001
    Location
    Finland
    I'm not quite catching what you mean by "all negative numbers"? Do you mean the variable would have multiple values?

  4. #4
    Badbonji's Avatar
    Join Date
    Apr 2008
    Location
    Birmingham, UK
    Benching Profile SETI Profile Heatware Profile
    If you take the negative of the square root of the square of the variable it will always be negative, no matter if the original number is positve or not. This way you don't need to do a comparison either.

    So x = -sqrt(x^2)

    (not sure how this would be written in java, plus I am not sure on the entire situation)
    i7 3770K 4.5GHz 1.2V | Phanteks PH-TC14PE | 16GB TeamGroup Elite 1600MHz | GIGABYTE G1.Sniper M3 | GTX 980 | M4 256GB + 150GB Raptor | Corsair HX+ 850W | Antec 1200
    i7 965 | 8GB OCZ Gold 1600MHz | GIGABYTE Extreme X58 | EVGA GTX 680 SC| M4 64GB + 7TB | OCZ 500W
    MacBook Pro 15" | i7 2.3GHz | 8GB 1600MHz

  5. #5
    Insatiably Malcontent
    Senior Member
    johan851's Avatar
    10 Year Badge
    Join Date
    Jul 2002
    Location
    Seattle, WA
    I don't think there's a way to do this without first checking to see if the number itself is positive. (I'm not familiar w/ all of the Java standard libs, though.) If there is, it's probably some sort of bitwise trickery involving the two's complement representation of the number.
    *Edit* I was way off about the two's complement notation. It's tricky to make it negative using that method.
    Last edited by johan851; 05-05-12 at 08:39 PM.
    ASRock Z68 Extreme3 Gen3 | 2500K @ 4.6GHz | 2x4GB Samsung DDR3 | GTX 750 Ti 2GB
    120GB Crucial M4 | 2x 2TB Samsung F4 | Seasonic S12 600w
    y2 DAC --> Custom M^3 --> Custom LM3875 ChipAmp --> Modula MTs
    Dual Dell 2007WFP | Watercooled

  6. #6
    Member
    10 Year Badge
    Join Date
    Jan 2002
    Location
    Augusta, GA
    abs(variable) * -1;
    I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours. -- Stephen Roberts

  7. #7
    New Member stphung's Avatar
    Join Date
    May 2012
    Location
    Bay Area, CA
    Quote Originally Posted by Badbonji View Post
    If you take the negative of the square root of the square of the variable it will always be negative, no matter if the original number is positve or not. This way you don't need to do a comparison either.

    So x = -sqrt(x^2)

    (not sure how this would be written in java, plus I am not sure on the entire situation)
    In Java this would be x = -Math.sqrt(Math.pow(x,2)).

    However, I highly recommend against doing this as there will be issues with floating point accuracy since sqrt is an approximated computation. If you are only dealing with recommend using any technique where you do not move from the Integer domain to floating point, it isn't necessary and will cause issues. Many of the posts above will work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •