Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 160179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:59:20+00:00 2026-05-11T10:59:20+00:00

For a videogame I’m implementing in my spare time, I’ve tried implementing my own

  • 0

For a videogame I’m implementing in my spare time, I’ve tried implementing my own versions of sinf(), cosf(), and atan2f(), using lookup tables. The intent is to have implementations that are faster, although with less accuracy.

My initial implementation is below. The functions work, and return good approximate values. The only problem is that they are slower than calling the standard sinf(), cosf(), and atan2f() functions.

So, what am I doing wrong?

// Geometry.h includes definitions of PI, TWO_PI, etc., as // well as the prototypes for the public functions #include 'Geometry.h'  namespace {     // Number of entries in the sin/cos lookup table     const int SinTableCount = 512;      // Angle covered by each table entry     const float SinTableDelta = TWO_PI / (float)SinTableCount;      // Lookup table for Sin() results     float SinTable[SinTableCount];      // This object initializes the contents of the SinTable array exactly once     class SinTableInitializer {     public:         SinTableInitializer() {             for (int i = 0; i < SinTableCount; ++i) {                 SinTable[i] = sinf((float)i * SinTableDelta);             }         }     };     static SinTableInitializer sinTableInitializer;      // Number of entries in the atan lookup table     const int AtanTableCount = 512;      // Interval covered by each Atan table entry     const float AtanTableDelta = 1.0f / (float)AtanTableCount;      // Lookup table for Atan() results     float AtanTable[AtanTableCount];      // This object initializes the contents of the AtanTable array exactly once     class AtanTableInitializer {     public:         AtanTableInitializer() {             for (int i = 0; i < AtanTableCount; ++i) {                 AtanTable[i] = atanf((float)i * AtanTableDelta);             }         }     };     static AtanTableInitializer atanTableInitializer;      // Lookup result in table.     // Preconditions: y > 0, x > 0, y < x     static float AtanLookup2(float y, float x) {         assert(y > 0.0f);         assert(x > 0.0f);         assert(y < x);          const float ratio = y / x;         const int index = (int)(ratio / AtanTableDelta);         return AtanTable[index];         }  }  float Sin(float angle) {     // If angle is negative, reflect around X-axis and negate result     bool mustNegateResult = false;     if (angle < 0.0f) {         mustNegateResult = true;         angle = -angle;     }      // Normalize angle so that it is in the interval (0.0, PI)     while (angle >= TWO_PI) {         angle -= TWO_PI;     }      const int index = (int)(angle / SinTableDelta);     const float result = SinTable[index];      return mustNegateResult? (-result) : result; }  float Cos(float angle) {     return Sin(angle + PI_2); }  float Atan2(float y, float x) {     // Handle x == 0 or x == -0     // (See atan2(3) for specification of sign-bit handling.)     if (x == 0.0f) {         if (y > 0.0f) {             return PI_2;         }         else if (y < 0.0f) {             return -PI_2;         }         else if (signbit(x)) {             return signbit(y)? -PI : PI;         }         else {             return signbit(y)? -0.0f : 0.0f;         }     }      // Handle y == 0, x != 0     if (y == 0.0f) {         return (x > 0.0f)? 0.0f : PI;     }      // Handle y == x     if (y == x) {         return (x > 0.0f)? PI_4 : -(3.0f * PI_4);     }      // Handle y == -x     if (y == -x) {         return (x > 0.0f)? -PI_4 : (3.0f * PI_4);     }      // For other cases, determine quadrant and do appropriate lookup and calculation     bool right = (x > 0.0f);     bool top = (y > 0.0f);     if (right && top) {         // First quadrant         if (y < x) {             return AtanLookup2(y, x);         }         else {             return PI_2 - AtanLookup2(x, y);         }     }     else if (!right && top) {         // Second quadrant         const float posx = fabsf(x);         if (y < posx) {             return PI - AtanLookup2(y, posx);         }         else {             return PI_2 + AtanLookup2(posx, y);         }     }     else if (!right && !top) {         // Third quadrant         const float posx = fabsf(x);         const float posy = fabsf(y);         if (posy < posx) {             return -PI + AtanLookup2(posy, posx);         }         else {             return -PI_2 - AtanLookup2(posx, posy);         }     }     else { // right && !top         // Fourth quadrant         const float posy = fabsf(y);         if (posy < x) {             return -AtanLookup2(posy, x);         }         else {             return -PI_2 + AtanLookup2(x, posy);         }     }      return 0.0f; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:59:21+00:00Added an answer on May 11, 2026 at 10:59 am

    ‘Premature optimization is the root of all evil’ – Donald Knuth

    Nowadays compilers provide very efficient intrinsics for trigonometric functions that get the best from modern processors (SSE etc.), which explains why you can hardly beat the built-in functions. Don’t lose too much time on these parts and instead concentrate on the real bottlenecks that you can spot with a profiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer PYTHONPATH may only be set when you run from the… May 11, 2026 at 1:37 pm
  • added an answer The DeploymentServerType paramater is optional (MSDN), if specified it restricts… May 11, 2026 at 1:37 pm
  • added an answer You should not return the HttpWebResponse. This is internal to… May 11, 2026 at 1:37 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.