I am trying to write a method to calculate a value raised to an integer power (which I think is now correct). I need to display all the sines ( out to six decimal places, I assume you would use df.format?) for the angles 0 to 90 and also display the Math.sin values for the same angles. What I have written so far displays Sine of 1=0, Sine of 2=-0, up to sine of 67 where it says Sine of 76=?. I just need to figure out how to get it to display the sine values correctly. I am using eclipse and displaying in the console if that matters. Any help is appreciated! Thanks!
Someone also said, "I'm not familiar with the equation you're using to calculate sine but I think I can see why you keep getting 0.
You initialize "ans" with the value with 0, and then then you do ans = ans * term. But the value of ans is 0 so you are always multiplying "term" by 0, then setting "ans" equal to that value (which is always 0)."
Does anyone know how to fix it?
Welcome to the Ranch
You are doing excess work calculating factorials every time. You should cache the factorial and multiply it each time. Similarly with the powers; it is probably also more precise to use power *= argument; in each iteration of the loop. Even when the absolute value of the term falls below 0.0000001, you have not necessarily reached six‑digit precision.
Paper jam tastes about as you would expect. Try some on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop