Calculating the value of a mathematical function in Java

I have this function and I was trying to get a list of all the calculations.

f(x) = (100)(1.1)^X

How do I calculate this out in Java. I tried a for loop to get the value of x and the end result of y, but that didn't work. Might have been my code inside. I tried to get the exponent of 1.1 and the multiply that by 100, is this correct?

for(int i = 1; i<=15; i++){
     int expcalc = (int) Math.pow(1.1, i);
     int finalcalc = price * expcalc;
     System.out.println(" " + finalcalc);
}

What am I doing wrong here?

Answers


Why are you casting the result as an int? That will drop everything past the decimal point. Declare expcalc and finalcalc as double instead to obtain an accurate result.

double expcalc = Math.pow(1.1, i);
double finalcalc = price * expcalc;

Need Your Help

Better (more performant) way to get specific DIVs?

javascript jquery dom

I want to add a specific DIV to other DIVs of a defined class. Because the page changes regularly I do this on every DOM-Change. This happens quite often and there are a lot of DIVs (up to a few 10...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.