Positive and negative BigIntegers

Hi

How do you convert a negative BigInteger into a positive BigInteger? I know how to do it with in Integer:

if (y< 0)

{

y = -(y);

}

But how would you do the same type of operation with a BigInteger?

Many thanks, Ron

[354 byte] By [cakea] at [2007-10-2 11:59:18]
# 1
How about abs() for the absolute value? negate() gives you the negative.
Mr.E.H.a at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...
# 2
Hiabs() doesn't work for a BigInterger!Many thanks, Ron
cakea at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...
# 3
HiIgnore that, it does work, sorry!Thanks, again :)Ron
cakea at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...
# 4

> Hi

>

> abs() doesn't work for a BigInterger!

>

> Many thanks, Ron

Yes it does:

java.math.BigInteger neg = new java.math.BigInteger("-666");

java.math.BigInteger pos = neg.abs();

System.out.println("neg="+neg+"\npos="+pos);

http://java.sun.com/j2se/1.3/docs/api/java/math/BigInteger.html

prometheuzza at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...
# 5
> abs() doesn't work for a BigInterger!How so?
Mr.E.H.a at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...
# 6
> Ignore that, it does work, sorry!> > Thanks, again :)> > RonAlways better to find it out yourself!; )Good luck.
prometheuzza at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...
# 7
HiYes, ahem, I was just implementing it wrong!!!!!if (x.signum() < 0){x = x.abs();}Many thanks for the correction...Ron
cakea at 2007-7-13 8:14:07 > top of Java-index,Other Topics,Algorithms...