Jump to content
  • Advertisement

gustavo rincones

Member
  • Content Count

    13
  • Joined

  • Last visited

Community Reputation

5 Neutral

About gustavo rincones

  • Rank
    Member

Personal Information

  • Role
    Programmer
  • Interests
    Education
    Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. gustavo rincones

    C++ 3 quick ways to calculate the square root in c++

    Hola chicos Since I created this post, I've only heard things I already knew, such as undefined behaviors, static members, trap representation in c ++ and modern compilers. I just want to see more efficient ways to calculate the square root. and since the creation of this publication, nobody has placed any sample code. chao
  2. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    Hola Como estas! Alvaro I think that the two functions cannot be compared because their function cannot calculate the sine at all angles and much less negative angles and also because it uses atan that is part of the STL. I think it's not worth it, I say it in a good way and I don't try to insult him. That is all I have to say.
  3. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    Hola Álvaro This function is almost 10 times slower than my current function and is also less accurate in some cases, for example "sine (30)" and still depends on the STL. If I wanted a precise but slow function, I would simply use the Taylor series for sine and cosine, which converges slowly but obtains the exact result.
  4. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    The union can be removed for greater speed. When I wrote this function, I used the union as a static member. static union{int i; float f;} u; which consumes less memory and maintains the same speed. The union takes the largest size bits. In this case the float type. The function can be better written in the form. void sameRadian(const float& rad, float& c, float& s) { int i; float f; i = int(rad * 0.31830988f + 0.5f); c = (i % 2)? 3.14159265f * i - rad - 1.570796f : rad + 1.570796f - 3.14159265f * i; i = int(rad * 0.31830988f); s = (i % 2)? 3.14159265f * i - rad : rad - 3.14159265f * i; if(c < 0.0f) { f = (1.273239f + c * 0.405284f) * c; c = (f + 1.0f) * f * -0.225f + f; } else { f = (1.273239f - c * 0.405284f) * c; c = (f - 1.0f) * f * 0.225f + f; } if(s < 0.0f) { f = (1.273239f + s * 0.405284f) * s; s = (f + 1.0f) * f * -0.225f + f; } else { f = (1.273239f - s * 0.405284f) * s; s = (f - 1.0f) * f * 0.225f + f; } } the union is unnecessary only delays the function.
  5. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    PI / 4 is the case of least convergence and the result is: 0.707816 three digits of precision. As I said from the beginning it is good only for video games. I have used this function a huge number of times to draw circles and calculate rotation matrices without any problem.
  6. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    SameRadian times are better since you avoid calling functions twice. I'm going to place a code fragment that takes the times in microseconds. This code only works on "OS" windows: #include <Windows.h> #include <iostream> #define TIMER_INIT \ LARGE_INTEGER frequency; \ LARGE_INTEGER t1,t2; \ double elapsedTime; \ QueryPerformanceFrequency(&frequency); #define TIMER_START QueryPerformanceCounter(&t1); #define TIMER_STOP \ QueryPerformanceCounter(&t2); \ elapsedTime = (float)(t2.QuadPart-t1.QuadPart)/frequency.QuadPart; \ std::cout << elapsedTime * 1000000.0f << " microsecond\n"; void sameRadian(const float& rad, float& c, float& s) { union{int i; float f;} u; u.i = int(rad * 0.31830988f + 0.5f); c = (u.i % 2)? 3.14159265f * u.i - rad - 1.570796f : rad + 1.570796f - 3.14159265f * u.i; u.i = int(rad * 0.31830988f); s = (u.i % 2)? 3.14159265f * u.i - rad : rad - 3.14159265f * u.i; //Calculando La Funcion Coseno if(c < 0.0f) { u.f = (1.273239f + c * 0.405284f) * c; c = (u.f + 1.0f) * u.f * -0.225f + u.f; } else { u.f = (1.273239f - c * 0.405284f) * c; c = (u.f - 1.0f) * u.f * 0.225f + u.f; } //Calculando La Funcion Seno if(s < 0.0f) { u.f = (1.273239f + s * 0.405284f) * s; s = (u.f + 1.0f) * u.f * -0.225f + u.f; } else { u.f = (1.273239f - s * 0.405284f) * s; s = (u.f - 1.0f) * u.f * 0.225f + u.f; } } int main(int args, char** argv) { float cose, seno, DEG2RAD = 3.14159265358979f / 180; while(true) { TIMER_INIT TIMER_START for(int i = 0; i <= 1000; i++) sameRadian(i * DEG2RAD, cose, seno); /* for(int i = 0; i <= 1000; i++) { sinf(i * DEG2RAD); cosf(i * DEG2RAD); } */ TIMER_STOP } } try it and tell me what results it shows you
  7. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    I really didn't know about the blog, but thanks I had no idea. I am new at this.
  8. gustavo rincones

    C++ how to calculate sine and cosine in c++ without STL

    by the beginning of next year I will launch my educational 2d physics engine CirObb engine with educational purposes to help people understand more about this wonderful world of game physics. I am 19 years old and I hope one day to pursue this as a career.
  9. Hi, I want to make this forum on how to calculate the sine and cosine in c++ without the STL. I'm sorry if my grammar is not accurate. Well let's get started: There are several methods to calculate sine and cosine and any other transcendent function. The Taylor series, polynomial approximations and the CORDIC algorithm "for sine and cosine" are some of these methods. There are occasions where we have to calculate the same Angle for sine and cosine functions for those cases we can create a single function. The next function receives 3 parameters the first is the same radian for the cosine and sine and the last two are two variables by reference to store these values. I must clarify that this function has only been tested in video games has not been used for another purpose. This function uses a polynomial approximation. void sameRadian(const float& rad, float& c, float& s) { union{int i; float f;} u; u.i = int(rad * 0.31830988f + 0.5f); c = (u.i % 2)? 3.14159265f * u.i - rad - 1.570796f : rad + 1.570796f - 3.14159265f * u.i; u.i = int(rad * 0.31830988f); s = (u.i % 2)? 3.14159265f * u.i - rad : rad - 3.14159265f * u.i; //Calculando La Funcion Coseno if(c < 0.0f) { u.f = (1.273239f + c * 0.405284f) * c; c = (u.f + 1.0f) * u.f * -0.225f + u.f; } else { u.f = (1.273239f - c * 0.405284f) * c; c = (u.f - 1.0f) * u.f * 0.225f + u.f; } //Calculando La Funcion Seno if(s < 0.0f) { u.f = (1.273239f + s * 0.405284f) * s; s = (u.f + 1.0f) * u.f * -0.225f + u.f; } else { u.f = (1.273239f - s * 0.405284f) * s; s = (u.f - 1.0f) * u.f * 0.225f + u.f; } } There are several constants and they were placed directly for speed reasons. Here are the two separate functions. float cosf2(const float& rad) { union{int i; float f;} u; u.i = int(rad * 0.31830988f + 0.5f); u.f = (u.i % 2)? 3.14159265f * u.i - rad - 1.570796f : rad + 1.570796f - 3.14159265f * u.i; //Calculando La Funcion Coseno if(u.f < 0.0f) { u.f = (u.f * 0.405284f + 1.273239f) * u.f; return (u.f + 1.0f) * u.f * -0.225f + u.f; } else { u.f = (1.273239f - u.f * 0.405284f) * u.f; return (u.f - 1.0f) * u.f * 0.225f + u.f; } } float sinf2(const float& rad) { union{int i; float f;} u; u.i = int(rad * 0.31830988f); u.f = (u.i % 2)? 3.14159265f * u.i - rad : rad - 3.14159265f * u.i; //Calculando La Funcion Seno if(u.f < 0.0f) { u.f = (1.273239f + u.f * 0.405284f) * u.f; return (u.f + 1.0f) * u.f * -0.225f + u.f; } else { u.f = (1.273239f - u.f * 0.405284f) * u.f; return (u.f - 1.0f) * u.f * 0.225f + u.f; } } well thanks to you for reading my forum.
  10. gustavo rincones

    C++ 3 quick ways to calculate the square root in c++

    The third method can also be written this way: float sqrt3(const float& n) { int i = 0x2035AD0C + (*(int*)&n >> 1); return n / *(float*)&i + *(float*)&i * 0.25f; } the static member was removed from the function as a quote from: SeanMiddleditch.
  11. gustavo rincones

    C++ 3 quick ways to calculate the square root in c++

    Very good information and also useful in its entirety. You are right, the "static" member avoids continuous creation of the union in exchange for speed but also completely limits the function. On the other hand, I like being able to see a function written in c ++ on how to calculate the square root in a more efficient way as you just described. I would really like to see that function please.
  12. Hi, this is my first forum and I want to do it: quick way to calculate the square root in c ++ with floating data types. These types of functions are very useful to gain some CPU time, especially when used continuously. I will show you 3 similar functions and indicate the advantages and disadvantages of each of them. The last of these three functions was written by me. If you notice that the writing is a bit out of grammar, it is because I do not speak English and I am using a support tool. My native language is Spanish. Well, let's start: The First method is very famous was used in the video game quake III arena and you can find a reference in Wikipedia as: :https://en.wikipedia.org/wiki/Fast_inverse_square_root. The Function was optimized for improvements in computing times. float sqrt1(const float &n) { static union{int i; float f;} u; u.i = 0x5F375A86 - (*(int*)&n >> 1); return (int(3) - n * u.f * u.f) * n * u.f * 0.5f; } -Advantages: * When Root of 0 is calculated the function returns 0. * The convergence of the function is acceptable enough for games. * It generates very good times. * The Reciprocal of the root can be calculated by removing the second “n” from the third line. According to the property of: 1 / sqrt (n) * n = sqrt (n). -Disadvantages: * Convergence decreases when the root to be calculated is very large. The second method is not as famous as the first. But it does the same function calculate the root. float sqrt2(const float& n) { union {int i; float f;} u; u.i = 0x1FB5AD00 + (*(int*)&n >> 1); u.f = n / u.f + u.f; return n / u.f + u.f * 0.25f; } -Advantages: * The convergence of the function is high enough to be used in applications other than games. -Disadvantages: * Computing times are much larger. * The square root of “0” is a number very close to “0” but never “0”. * The division operation is the bottleneck in this function. because the division operation is more expensive than the other arithmetic operations of Arithmetic Logic Units (ALU). The third method takes certain characteristics of the two previous methods. float sqrt3(const float& n) { static union {int i; float f;} u; u.i = 0x2035AD0C + (*(int*)&n >> 1); return n / u.f + u.f * 0.25f; } Advantages: * The convergence of the function is greater than that of the first method. * Generates times equal to or greater than the first method. Disadvantages: * The square root of “0” is a number very close to “0” but never “0”. The 3 previous methods have something in common. They are based on the definition of the Newton-Raphson Method. according to the function of the square root > f (x) = x ^ 2 - s. well thanks to you for reading my forum. well thanks to you for reading my forum.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!