Kristopher Agarwal
Kristopher Agarwal

Reputation: 11

Why does the pow() function in C++ not work for me here?

I'm trying to make a table that prints daily compounding interest at different rates over time, but the pow() function does not appear to be working for me. Here is my code:

double balance = principle * pow( (1 + (i / 36000)), (j * 360) );
printf( "%15.2f", balance);

Where i is the interest rate and j is the year. However, the print statement only ever prints the original principle because the pow() is just returning 1. Am I doing something wrong, and how can I fix it? Thank you

Upvotes: 1

Views: 162

Answers (1)

Balram Singh
Balram Singh

Reputation: 33

I think the problem is with the data-type of variable "i"(interest) and maybe you are taking it as an integer. If both numerator and denominator are of integer type then "/" operator does integer division i.e. gives the floor value. So this division results in 0 if (numerator < denominator) and 1+0 = 1. And eventually, any power to 1 results in 1.

Below are the changes, you can do:

  1. Type-cast the variable "i" to float/double
  2. Just change 36000 to 36000.0

Upvotes: 3

Related Questions