Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can't make natural logs work in java?

Tags:
  1. Jun 10, 2014 #1
    I'm trying to learn java. So I'm practicing what I've learned so far by making a calculator to do formulas I learned in my finance class.
    But it's not working right now.

    Code (Text):
               
     case ("loan length"):
     Scanner inp_ll = new Scanner(System.in);
     System.out.print("What is the monthly payment? ");
     monthlypmt = inp_ll.nextDouble();
     System.out.print("What is the rate? ");
     percent = inp_ll.nextDouble();
     rate = percent/100;
     System.out.print("What is the loan amount? ");
     principal = inp_ll.nextDouble();
     answer = (Math.log(monthlypmt/principal)-Math.log((monthlypmt/principal)-(rate/12)))/12*Math.log(1+rate/12);
     System.out.print(("The loan length is: ")+ (answer));
     break;
    Here's one input I tested

    And then another input using bigger numbers yields


    Did I just do the formula wrong?
    I looked it over a bunch and it looks right to me.
    I even erased it and tried typing it in again.

    Here's the original formula
    34oea7a.jpg

    where ln is natural log, m is monthly payment, p is principal and r is rate.
     
  2. jcsd
  3. Jun 10, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    In Java, a/b*c means (a/b)*c, not a/(b*c).

    You need another pair of ( ) in your statement
    Code (Text):
    answer = (....) / 12 * Math.log(....);
     
  4. Jun 11, 2014 #3

    Borek

    User Avatar

    Staff: Mentor

    I would say it is not limited to Java.
     
  5. Jun 11, 2014 #4

    adjacent

    User Avatar
    Gold Member

    Yes, it's a rule all of use use. The BIDMUS rule.
     
  6. Jun 11, 2014 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Did you read the section "Exceptions to the standard" in that link?

    I agree most programming languages the BIDMUS rules, but not all - for example APL.
     
  7. Jun 11, 2014 #6

    adjacent

    User Avatar
    Gold Member

    I was referring to mathematicians.
     
  8. Jun 11, 2014 #7

    jtbell

    User Avatar

    Staff: Mentor

    You sound like a New Yorker. :rolleyes: Uh-dittion? Fuhgeddaboutit!
     
  9. Jun 11, 2014 #8

    jedishrfu

    Staff: Mentor

    The safest solution is to always use parens around all factors and terms to make it explicitly clear what the math precedence is supposed to be.

    This is especially true with operator precedence rules of the more arcane operators like the boolean and bitshift operators.

    For Java, the operator precedence is (now entering the BIDMAS zone):

    http://en.wikipedia.org/wiki/Operator_precedence

    Java has some interesting gotchas too like when you use == vs = or && vs & ... that may not issue compile-time errors but do something quite unexpected because you typed in the wrong operator.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook