Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java number precision
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HeavenAgain




PostPosted: Sat Nov 17, 2007 9:42 pm   Post subject: 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... Confused
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Sat Nov 17, 2007 9:51 pm   Post subject: RE:Java number precision

I just googled it:

http://forum.java.sun.com/thread.jspa?threadID=477276&messageID=2217025
Java:
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.0)
        return null; // error
    int i = 0;
    while (b.compareTo(MAX) > 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(<num>) as a method.
HeavenAgain




PostPosted: Sat Nov 17, 2007 10:08 pm   Post subject: RE:Java number precision

ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

dont get it :'(
Euphoracle




PostPosted: Sat Nov 17, 2007 10:32 pm   Post subject: 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.

<horrible sentence structure Very Happy>
HeavenAgain




PostPosted: Sat Nov 17, 2007 10:53 pm   Post subject: RE:Java number precision

i understand and yet i dont understand
im going to kill myself if i dont get this Hit 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




PostPosted: Sun Nov 18, 2007 12:45 pm   Post subject: Re: Java number precision

HeavenAgain @ Sat Nov 17, 2007 9:42 pm wrote:
i need the log method, which they dont have... Confused

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




PostPosted: Sun Nov 18, 2007 3:17 pm   Post subject: 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 Rolling Eyes i hope it it helps anyone with helping me

side note, k = 10^((log n)/ p), i think
Barbarrosa




PostPosted: Mon Nov 19, 2007 2:08 am   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Mon Nov 19, 2007 10:08 am   Post subject: 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
code:
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 Neutral
Barbarrosa




PostPosted: Mon Nov 19, 2007 10:38 pm   Post subject: 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




PostPosted: Mon Nov 19, 2007 10:44 pm   Post subject: 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 Very Happy
Barbarrosa




PostPosted: Mon Nov 19, 2007 11:55 pm   Post subject: Re: Java number precision

Welcome, I'm also interested in getting the answer. Could you share it?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: