user3855182
user3855182

Reputation: 11

I don't want to round off the double values, what should i do?

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);

Upvotes: 0

Views: 891

Answers (3)

Yogesh D
Yogesh D

Reputation: 1666

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

Upvotes: 1

Samarendra
Samarendra

Reputation: 780

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

Upvotes: 1

Jurgen
Jurgen

Reputation: 347

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.

Upvotes: 0

Related Questions