programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How do I get this to display correctly?

Jefferey Stevens
Greenhorn
Posts: 7
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?

This is what i have so far,

Mack Wilmot
Ranch Hand
Posts: 88
Yep, what you said is what keeps making it return zero.

Edit: Taylor series is adding the terms, not multiplying them.

Mack Wilmot
Ranch Hand
Posts: 88
Also you are wasting resources creating a new DecimalFormat object for each loop, you only need to do it once.

Campbell Ritchie
Marshal
Posts: 56546
172
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.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?