Win a copy of Rust Web Development this week in the Other Languages forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# How do I get this to display correctly?

Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
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,

Ranch Hand
Posts: 88
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Also you are wasting resources creating a new DecimalFormat object for each loop, you only need to do it once.

Marshal
Posts: 74687
335
• Number of slices to send:
Optional 'thank-you' note:
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 https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton