0

This is the code and it gives an infinite output because of rounding 0.993 to 1.0 double amount=100; double intrest=8.5; double quaters =21 / 3; double first=intrest / 400 + 1 ; double secound=(-1/3);

   double    monthpayment = amount * ((Math.pow(intrest / 400 + 1, quaters) - 1) / (1-(Math.pow(intrest / 400 + 1,(-1/3)))));

  System.out.println(monthpayment);
Munib
  • 957
  • 1
  • 14
  • 30
user3855182
  • 11
  • 1
  • 3

3 Answers3

1

You are getting infinity simply because you are dividing the number by zero. The value of 1-(Math.pow(intrest / 400 + 1,(-1/3))) is 0

Here is the debugged code:

public class Round {


    public static void main(String args[]){


        double intrest=8.5;
        double quaters =21.0 / 3.0;
        double first=intrest / 400.0 + 1 ; 
        double secound=(-1.0/3.0);
        double amount=100.0; 
        System.out.println("intrest"+intrest);
        System.out.println(secound);
        System.out.println(quaters);
        System.out.println(first);
        System.out.println(Math.pow(intrest / 400 + 1, quaters) - 1);
        System.out.println(1-(Math.pow(intrest / 400 + 1,(-1/3))));



       double    monthpayment = amount * ((Math.pow(intrest / 400 + 1, quaters) - 1) / (1-(Math.pow(intrest / 400 + 1,(-1/3)))));

       System.out.println(monthpayment);
    }
}

Here is the output for each line:

intrest8.5
-0.3333333333333333
7.0
1.02125
0.15857589055432686
**0.0**
Infinity

See you are dividing it by zero, hence you are getting infinity.

If you don't want infinity, you can just do following changes:

double monthpayment = amount * ((Math.pow(intrest / 400 + 1, quaters) - 1) / (1-(Math.pow(intrest / 400 + 1,((double)-1/3)))));

Now the value (1-(Math.pow(intrest / 400 + 1,((double)-1/3)))) would be 0.006984615789001336 instead of 0

Yogesh D
  • 1,558
  • 14
  • 29
  • @user3855182: Please accept the answer if it helped. 3.0 is not the only reason, you are getting one equation as zero, hence you are getting infinity, since you are dividing by zero. – Yogesh D Nov 05 '16 at 05:46
1

The Problem you are facing is with -1/3 as it is returning 0,

That's because 1 and 3 are treated as integers when you don't specify otherwise, so -1/3 evaluates to the integer 0 which is then cast to the double 0. try (-1.0/3), or maybe -1D/3

any value raised to the power zero has a numerical value of 1. That is the reason you are receiving value as 1 for expression

Math.pow(intrest / 400 + 1,(-1/3))

just replace this with

Math.pow(intrest / 400 + 1,((double)-1/3))

Final expression

double    monthpayment = amount * ((Math.pow(intrest / 400 + 1, quaters) - 1) / (1-(Math.pow(intrest / 400 + 1,((double)-1/3)))));
System.out.println(monthpayment);

see this for more details https://stackoverflow.com/a/366240/3933557

Community
  • 1
  • 1
Samarendra
  • 760
  • 10
  • 23
0

when setting your doubles and dividing make sure your using 3.0 and not 3 (set all your values as floats including 100.00, and whatever else there is).

even though your variable type is double, your dividing int types and thats causing a round of error.

try it I could be wrong.

Jurgen
  • 337
  • 3
  • 10