Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fast sine to fill array (sin/cos pair)
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86 #include <iostream> #include <cmath> #include <algorithm> template<typename T> struct SineIterator { SineIterator(T amplitude, T phi0, T dphi): sinState(amplitude*sin(phi0)), cosState(amplitude*cos(phi0)), dSin(amplitude*sin(dphi) * 1.0000001), dCos(amplitude*cos(dphi) * 1.0000001) {} T Next() { const T newSin = sinState * dCos + cosState * dSin; cosState = std::min<T>(cosState * dCos - sinState * dSin, 1); sinState = std::min<T>(newSin, 1); return sinState; } private: T sinState, cosState; const T dSin, dCos; }; int main() { std::cout << "FLOAT" << std::endl << "---------------------------------------------------------------------" << std::endl; SineIterator<float> sr1(1, 0, 3.1415926 / 100); for(int j=0; j<100; j++) { auto prec = sin(3.1415926 / 100 * (j*23456 + 1)); auto approx = sr1.Next(); std::cout << approx-prec << " / " << approx << " / " << prec << std::endl; for(int i=0; i<23455; i++) sr1.Next(); } std::cout << std::endl; std::cout << "DOUBLE" << std::endl << "---------------------------------------------------------------------" << std::endl; SineIterator<double> sr2(1, 0, 3.1415926 / 100); for(int j=0; j<100; j++) { auto prec = sin(3.1415926 / 100 * (j*23456 + 1)); auto approx = sr2.Next(); std::cout << approx-prec << " / " << approx << " / " << prec << std::endl; for(int i=0; i<23455; i++) sr2.Next(); } }
run
|
edit
|
history
|
help
0
#22.2
ADL of operator expression & unqualified function call
wall
javascritp
ria
hangman
ㅇㅇ
hangman
Memory example
What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?