Jefferey Stevens

Greenhorn

Posts: 7

posted 4 years ago

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,

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,

Campbell Ritchie

Marshal

Posts: 56546

172

posted 4 years ago

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

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? |