Can't make natural logs work in java?

In summary, the calculator is not working right now because I'm missing a piece of the equation. I need to find a way to include the monthly payment and rate in the equation so that I can solve for the loan amount.
  • #1
breebreebran
12
0
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:
 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

What is the monthly payment? 212
What is the rate? 12.7
What is the loan amount? 3000
The loan length is: 1.4233611456294047E-4

And then another input using bigger numbers yields

What is the monthly payment? 350
What is the rate? 13
What is the loan amount? 150000
The loan length is: NaN


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.
 
Technology news on Phys.org
  • #2
In Java, a/b*c means (a/b)*c, not a/(b*c).

You need another pair of ( ) in your statement
Code:
answer = (...) / 12 * Math.log(...);
 
  • #3
AlephZero said:
In Java, a/b*c means (a/b)*c, not a/(b*c).

I would say it is not limited to Java.
 
  • #4
Borek said:
I would say it is not limited to Java.
Yes, it's a rule all of use use. The BIDMUS rule.
 
  • #5
adjacent said:
Yes, it's a rule all of use use. The BIDMUS rule.

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.
 
  • #6
AlephZero said:
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.

I was referring to mathematicians.
 
  • #7
adjacent said:
BIDMUS rule.

You sound like a New Yorker. :rolleyes: Uh-dittion? Fuhgeddaboutit!
 
  • #8
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.
 

1. Why am I getting an error when trying to use natural logs in Java?

This could be due to a few reasons. One possibility is that you are not importing the necessary package for natural logs, which is java.lang.Math. Another possibility is that you are not using the correct syntax for calling the natural log function, which is Math.log(). Make sure you have imported the package and are using the correct syntax.

2. Can I use natural logs with any type of data in Java?

Yes, you can use natural logs with any type of numerical data in Java, including integers, floating point numbers, and doubles. However, keep in mind that natural logs are only defined for positive numbers, so you may encounter errors if you try to take the natural log of a negative number.

3. How do I round the result of a natural log calculation in Java?

You can round the result of a natural log calculation using the Math.round() function. This function takes in a double as its parameter and returns the closest long value to the given double. You can also use other rounding functions, such as Math.ceil() or Math.floor(), depending on your specific needs.

4. Is there a way to calculate the natural log base 10 in Java?

Yes, you can use the Math.log10() function to calculate the natural log base 10 in Java. This function takes in a double as its parameter and returns the natural log base 10 of that number. Keep in mind that this function is only available in Java 1.5 or higher.

5. How can I use natural logs to solve exponential equations in Java?

Natural logs can be useful in solving exponential equations in Java. For example, if you have an equation of the form x = a^b, you can take the natural log of both sides to get ln(x) = ln(a^b), which can then be simplified to ln(x) = b * ln(a). From there, you can use the Math.exp() function to solve for x. Keep in mind that this method only works for equations where the base is a positive number.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • General Math
Replies
8
Views
2K
  • Precalculus Mathematics Homework Help
Replies
14
Views
7K
  • General Math
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
2
Views
1K
  • General Math
Replies
4
Views
2K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
Back
Top