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

  • SEARCH
  • Home
  • 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 5954835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:01:40+00:00 2026-05-22T18:01:40+00:00

My game needs to move by a certain angle. To do this I get

  • 0

My game needs to move by a certain angle. To do this I get the vector of the angle via sin and cos. Unfortunately sin and cos are my bottleneck. I’m sure I do not need this much precision. Is there an alternative to a C sin & cos and look-up table that is decently precise but very fast?

I had found this:

float Skeleton::fastSin( float x )
{
    const float B = 4.0f/pi;
    const float C = -4.0f/(pi*pi);

    float y = B * x + C * x * abs(x);

    const float P = 0.225f;

    return P * (y * abs(y) - y) + y; 
}

Unfortunately, this does not seem to work. I get significantly different behavior when I use this sin rather than C sin.

Thanks

  • 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. Editorial Team
    Editorial Team
    2026-05-22T18:01:41+00:00Added an answer on May 22, 2026 at 6:01 pm

    For your fastSin(), you should check its documentation to see what range it’s valid on. The units you’re using for your game could be too big or too small and scaling them to fit within that function’s expected range could make it work better.

    EDIT:

    Someone else mentioned getting it into the desired range by subtracting PI, but apparently there’s a function called fmod for doing modulus division on floats/doubles, so this should do it:

    #include <iostream>
    #include <cmath>
    
    float fastSin( float x ){
        x = fmod(x + M_PI, M_PI * 2) - M_PI; // restrict x so that -M_PI < x < M_PI
        const float B = 4.0f/M_PI;
        const float C = -4.0f/(M_PI*M_PI);
    
        float y = B * x + C * x * std::abs(x);
    
        const float P = 0.225f;
    
        return P * (y * std::abs(y) - y) + y; 
    }
    
    int main() {
        std::cout << fastSin(100.0) << '\n' << std::sin(100.0) << std::endl;
    }
    

    I have no idea how expensive fmod is though, so I’m going to try a quick benchmark next.

    Benchmark Results

    I compiled this with -O2 and ran the result with the Unix time program:

    int main() {
        float a = 0;
        for(int i = 0; i < REPETITIONS; i++) {
            a += sin(i); // or fastSin(i);
        }
        std::cout << a << std::endl;
    }
    

    The result is that sin is about 1.8x slower (if fastSin takes 5 seconds, sin takes 9). The accuracy also seemed to be pretty good.

    If you chose to go this route, make sure to compile with optimization on (-O2 in gcc).

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

Sidebar

Related Questions

I am currently working on a game engine that needs to move values between
I'm working on a game (something like chess) where every move needs to be
game.h needs: - packet.h - socket.h socket.h needs: - game.h The problem comes when
I am making a game that needs a crosshair. I have been playing with
I have to simulate a game where each player has turns and needs to
I'm working on a game where the client needs to continue processing windows messages
I'm currently developing an iPhone game where the player needs to tilt the device
With reference to this programming game I am currently building. Important: Scroll down to
I've got a very simplistic game set up on the iPhone. Things move around,
I am writing a chess game which allows two programs compete, the player needs

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.