-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
math: Sine and cosine accuracy #191
Comments
I can confirm that the accuracy of the sin function slips as the value of theta gets larger, say greater than pi. It could be related to symmetry. I ran a test using just a c version on my computer as I don't have a parallela. I've also provided an alternate approach which is slightly more memory hungry as it uses a lookup table and bilinear interpolation. I ran a test for a smooth range from [0 to 2pi], the results show the operating systems sin function in comparison to the iterative method implemented here and my approach of using interpolation and some pre-calculated values. In this example I pre-calculated 360 values to match the standard number of degrees although the table could be larger or smaller if required. The other values are calculated via bilinear interpolation. Note that there are divide operations in the code but only for constants which should be removed by the compiler so wouldn't need to be calculated on the Parallela. It is really a matter of whether it is worth using a little extra memory and getting accurate results or whether the iterative method can be improved by taking advantage of symmetries or perhaps more iterations? I've attached the test code - please build and test for yourselves: ` #include <stdio.h> const double sinTable[] = { const double RAD_TO_DEG = 180.0/3.14159265359; #define wrap(i) (i % 360 < 0) ? (360 + ((i) % 360)) : ((i) % 360) double interpolated_sin(double theta) {
} double iterative_sin(double theta) { int main() {
} ` PS. Is that deal still on where you get a Parallela if you contribute a function - I'm unemployed at the moment! |
|
Hi,
When I try to test the sine and cosine implementations with inputs on [0, 2pi] rather than [-1, 1], they are only accurate to two or three decimal places.
I know people put in the effort and I don't want to be rude and mess with their code, but I would like to suggest two formulas which are more accurate and probably just as fast. They are polynomial approximations accurate to nine decimal places. The formulas are here: http://eliteraspberries.github.io/pal/formulas/
Polynomial approximations involve mainly floating-point multiplication and addition, which is good for modern hardware with a multiply-accumulate instruction.
The text was updated successfully, but these errors were encountered: