Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Java - Variable = all negative ??

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 03-26-12, 12:47 PM Thread Starter   #1
Xtreme Barton
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

__________________


Xtreme Barton is offline   QUOTE Thanks
Old 03-27-12, 12:08 AM   #2
pcarini
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.

__________________
"We can't stop here, this is bat country!"

Last edited by pcarini; 03-27-12 at 12:25 AM. Reason: Better example
pcarini is offline   QUOTE Thanks
Old 03-27-12, 04:07 AM   #3
dropadrop
Member

 
dropadrop's Avatar 

Join Date: Mar 2001
Location: Finland

10 Year Badge
 
I'm not quite catching what you mean by "all negative numbers"? Do you mean the variable would have multiple values?
dropadrop is offline   QUOTE Thanks
Old 04-10-12, 04:47 PM   #4
Badbonji

 
Badbonji's Avatar 

Join Date: Apr 2008
Location: Birmingham, UK

 
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 | EVGA GTX680 SC 1254/1500MHz | M4 256GB + 150GB Raptor | Corsair HX+ 850W | Antec 1200
i7 965 | 8GB OCZ Gold 1600MHz | GIGABYTE Extreme X58 | HD5450 | M4 64GB + 7TB | OCZ 500W
MacBook Pro 15" | i7 2.3GHz | 8GB 1600MHz
Badbonji is offline Benching Profile SETI Profile Heatware Profile   QUOTE Thanks
Old 04-11-12, 12:59 AM   #5
johan851
Insatiably Malcontent
Senior Member

 
johan851's Avatar 

Join Date: Jul 2002
Location: Seattle, WA

10 Year Badge
 
Quote:
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.

__________________
ASRock Z68 Extreme3 Gen3 | 2500K @ 4.6GHz | 2x4GB Samsung DDR3 | GTX460 768mb
120GB Crucial M4 | 2x 2TB Samsung F4 | Seasonic S12 600w
y2 DAC --> Custom M^3 --> Custom LM3875 ChipAmp --> Modula MTs
Dual Dell 2007WFP | Watercooled

Last edited by johan851; 05-05-12 at 08:39 PM.
johan851 is online now   QUOTE Thanks
Old 04-16-12, 05:46 AM   #6
Cowboy Shane
Member



Join Date: Jan 2002
Location: Augusta, GA

10 Year Badge
 
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
Cowboy Shane is offline   QUOTE Thanks
Old 05-05-12, 08:28 PM   #7
stphung
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.
stphung is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 10:35 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?