Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problem with nth square root of any number

  1. #1
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Problem with nth square root of any number

    OK, so now I think this is sending me crazy. I'm looking at a problem that is a mathematical problem. Now I have no problem coming up with a solution to it, that runs in under 1 second. However I want to do it using some funky Math. Anyways, after working out the Math I got one of the equations I need to compute down to

    n^(1/6)

    Or the 6th root of n.

    Now the Java code I have for this is as follows, since Java has no nth root function I'm raising it to a power of a 1/6.
    System.out.println(Math.pow(64.0, 1/6));
    Now, the only thing it will print is 1.0
    However the 6th root of 64 is 2!!!!

    I'm going crazy please help

    Chris


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Mathematics in Java

    ARRRRRRRRRGGGGGGGHHHHHHHHHHH

    I should make the 1/6 into a double... I think I may kill someone!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Mathematics in Java

    Ah Chris! I was just about to submit my solution!!!

    System.out.println(Math.round(Math.pow(64.0, .16)));
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Mathematics in Java

    Hehe thanks, but I still face one problem and that is when I wish to calculate the 6th root of 64000000, which is 20. But java makes it aprox 19.999999999999998. Unfortunately rounding is not an option

    Chris

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Mathematics in Java

    Why is rounding not an option?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Mathematics in Java

    because if i calculate the 6th root of 64999999 then it will be just under 19.9999999999999998, but it shouldn't be rounded up to 20, it should be rounded down to 19 xD

    Chris

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Mathematics in Java

    Could always substring it lol
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Mathematics in Java

    How do you mean?