Can't make natural logs work in java?

Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue related to implementing a formula for calculating loan length using natural logarithms. Participants explore potential errors in the code and clarify mathematical operator precedence in Java.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster presents a Java code snippet for calculating loan length and reports unexpected results, questioning whether the formula is implemented correctly.
  • Some participants suggest that the issue may stem from operator precedence in Java, specifically that a/b*c is interpreted as (a/b)*c rather than a/(b*c).
  • Others emphasize the importance of using parentheses to clarify mathematical expressions and avoid ambiguity in operator precedence.
  • There is mention of the BIDMUS rule (Brackets, Indices, Division and Multiplication, Addition and Subtraction) as a general guideline for mathematical operations in programming.
  • Some participants note that while BIDMUS applies to most programming languages, there are exceptions, such as APL.
  • A later reply highlights the need for caution with operator precedence in Java, mentioning specific operators that can lead to unexpected behavior if misused.

Areas of Agreement / Disagreement

Participants generally agree on the importance of operator precedence and the use of parentheses in mathematical expressions, but there is no consensus on the specific cause of the original poster's issue with the formula.

Contextual Notes

Some limitations in the discussion include potential misunderstandings of the formula's implementation and the specific mathematical assumptions underlying the calculations.

breebreebran
Messages
12
Reaction score
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
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(...);
 
AlephZero said:
In Java, a/b*c means (a/b)*c, not a/(b*c).

I would say it is not limited to Java.
 
Borek said:
I would say it is not limited to Java.
Yes, it's a rule all of use use. The BIDMUS rule.
 
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.
 
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.
 
adjacent said:
BIDMUS rule.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
14
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
16
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K