For the sake of optimization, I decided to make a sign in which sines and cosines will lie. Draws almost as it should.

enter image description here

there is a peak. I can not understand why and from where it is taken.

sincos::sincos(){ double step = 6.28318530717958647692 / 4096.0f; sinTable = new float[4099]; cosTable = new float[4099]; int index = 0; for (double i = 0; i < 6.28318530717958647692; i += step) { sinTable[index] = std::sin(i); cosTable[index] = std::cos(i); index++; } float sincos::getSin(float val) { val = fmod(val, 6.28318530717958647692); return sinTable[(int)(4096.0f * val / 6.28318530717958647692)]; } float sincos::getCos(float val) { val = fmod(val, 6.28318530717958647692); return cosTable[(int)(4096.0f * val / 6.28318530717958647692)]; } 
  • What standard for handling floating-point numbers is used in your compiler? IEEE 754? - Mikhail Vaysman
  • and the schedule is built? - pavel
  • 3
    Well, you can see for yourself what value of the argument has the wrong value of the function. Do you have a debugger? You find it easier than we guess by the method of gazing. - VladD
  • In such cases, a minimal self-reproducible example should be given. Which you can copy yourself, immediately compile and check. - Zealint
  • one
    Somehow I tried to optimize in a similar way and ran into the fact that access to these tables is slower than to consider sincos in the place where they are required (I was not mistaken, it was sincos , and not separate sin and cos ). You first profile .. - borisbn

1 answer 1

There are no peaks in your table - http://ideone.com/T4nigg

So deal with drawing ...

 #include <math.h> #include <stdio.h> class SinCos { public: SinCos() { double step = 6.28318530717958647692 / 4096.0f; sinTable = new float[4099]; cosTable = new float[4099]; int index = 0; for (double i = 0; i < 6.28318530717958647692; i += step) { sinTable[index] = sin(i); cosTable[index] = cos(i); index++; } maxIdx = index; } float getSin(float val) { val = fmod(val, 6.28318530717958647692); return sinTable[(int)(4096.0f * val / 6.28318530717958647692)]; } float getCos(float val) { val = fmod(val, 6.28318530717958647692); return cosTable[(int)(4096.0f * val / 6.28318530717958647692)]; } float maxSinDelta(); float maxCosDelta(); float* sinTable; float* cosTable; int maxIdx; }; float SinCos::maxCosDelta() { float maxval = 0; for(int i = 0; i < maxIdx; ++i) { float delta = fabs(cosTable[i] - cosTable[(i+1)%maxIdx]); if (maxval < delta) maxval = delta; } return maxval; } float SinCos::maxSinDelta() { float maxval = 0; for(int i = 0; i < maxIdx; ++i) { float delta = fabs(sinTable[i] - sinTable[(i+1)%maxIdx]); if (maxval < delta) maxval = delta; } return maxval; } int main() { SinCos s; printf("MaxDelte sin = %f, cos = %f\n", s.maxSinDelta(), s.maxCosDelta()); } 
  • Interesting check, thanks. As it turned out, the problem was in drawing. Negative values ​​were sent there :) - Navira