
-----------------------------------
HeavenAgain
Sat Nov 17, 2007 9:42 pm

Java number precision
-----------------------------------
for example, how do i get the value of 1234 to the power of 7? it reached the number format limit
there must be a way to show it.... BigInteger or BigDecimal? how does those work, if they are related.... and if they are related, i need the log method, which they dont have...  :?

-----------------------------------
Euphoracle
Sat Nov 17, 2007 9:51 pm

RE:Java number precision
-----------------------------------
I just googled it:

http://forum.java.sun.com/thread.jspa?threadID=477276&messageID=2217025
private static final BigDecimal MAX = new BigDecimal(Double.MAX_VALUE);
private static final BigDecimal HALF = new BigDecimal(0.5);
public static BigDecimal bigLog(BigDecimal b) {
    double s = b.signum();
    if (s  0) {
        b = b.multiply(HALF);
        i++;
    }
    double d = b.doubleValue();
    if (i > 0) {
        double n = i;
        d = Math.pow(d,1.0/n) * 2.0;
        d = n * Math.log(d);        
    } else 
        d = Math.log(d); 
    return new BigDecimal(d);
}


Also, I do believe that BigDecimal, etc have pow() as a method.

-----------------------------------
HeavenAgain
Sat Nov 17, 2007 10:08 pm

RE:Java number precision
-----------------------------------
ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

dont get it :'(

-----------------------------------
Euphoracle
Sat Nov 17, 2007 10:32 pm

RE:Java number precision
-----------------------------------
Meaning you have created a non terminating non repeating fraction, which BigDecimal cannot represent becuase it strives for absolute accuracy, and whatnot, perhaps I'm wrong, who knows, etc.



-----------------------------------
HeavenAgain
Sat Nov 17, 2007 10:53 pm

RE:Java number precision
-----------------------------------
i understand and yet i dont understand
im going to kill myself if i dont get this :wall: must, research....!!

edit: im not saying you MUST use BigInteger or BigDecimal, that was just my idea, becuase i dont see how long or double is going to hold a number like 4357186184021382204544 (1234 to the power of 7)

-----------------------------------
OneOffDriveByPoster
Sun Nov 18, 2007 12:45 pm

Re: Java number precision
-----------------------------------
i need the log method, which they dont have...  :?
You need log in what base?  And are your inputs exact powers of the base?  Might be easier if you don't need a general log function.

-----------------------------------
HeavenAgain
Sun Nov 18, 2007 3:17 pm

RE:Java number precision
-----------------------------------
input is 2 numbers, n and p
n square root of p, find k (the result)

and if p is 7 and n is 4357186184021382204544, result i should i get is 1234
now n is too big for double or long, thats what im talking about :roll: i hope it it helps anyone with helping me

side note, k = 10^((log n)/ p), i think

-----------------------------------
Barbarrosa
Mon Nov 19, 2007 2:08 am

Re: Java number precision
-----------------------------------
I've tried to use BigDecimal to do it, but to no avail. There is no floating point power, nor any root method for BigDecimal.

-----------------------------------
HeavenAgain
Mon Nov 19, 2007 10:08 am

RE:Java number precision
-----------------------------------
Java's BigDecimal have something weird going on inside, so for this problem, im not sure if it will be suitable for it, becuase
BigDecimal num = new BigDecimal(1.1);
System.out.println(num);
System.out.println(num.pow(2));
as you can see num is not 1.1, its acually some number with some useless trail of floating point

but back to the problem, there was a hint given, and it said, double should be able to handle it :|

-----------------------------------
Barbarrosa
Mon Nov 19, 2007 10:38 pm

Re: Java number precision
-----------------------------------
Well, Eclipse will accept it if you put it in scientific notation. Won't give me 1234 as an answer, though.

-----------------------------------
HeavenAgain
Mon Nov 19, 2007 10:44 pm

RE:Java number precision
-----------------------------------
hehe, never mind about this question, i have figured out the way to solve this type of problem
something about high percision algorith, involving array
thanks for all the inputs :D

-----------------------------------
Barbarrosa
Mon Nov 19, 2007 11:55 pm

Re: Java number precision
-----------------------------------
Welcome, I'm also interested in getting the answer. Could you share it?
