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 7673385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:30:00+00:00 2026-05-31T16:30:00+00:00

Is there a way to convince the C preprocessor to evaluate a transcendental function

  • 0

Is there a way to convince the C preprocessor to evaluate a transcendental function of a constant at compile-time?

For example, replace (int)256*sin(PI/4) with 181. This will help me keep magic numbers out of my code.

If it makes a difference, I’m using MSPGCC 4.5.3 and I have no sin() or cos() available at runtime.

  • 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-31T16:30:01+00:00Added an answer on May 31, 2026 at 4:30 pm

    As long as your arguments are all within the range [-π/4,+π/4], you can use the same formula standard implementations of libm use to compute sin. It’s correct up to the last place (at most 1ulp error) just like the IEEE standard requires:

    static const double 
    half =  5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
    S1  = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
    S2  =  8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
    S3  = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
    S4  =  2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
    S5  = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
    S6  =  1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
    
    double __kernel_sin(double x, double y, int iy)
    {
        double z,r,v;
        int ix;
        ix = __HI(x)&0x7fffffff;    /* high word of x */
        if(ix<0x3e400000)           /* |x| < 2**-27 */
           {if((int)x==0) return x;}        /* generate inexact */
        z   =  x*x;
        v   =  z*x;
        r   =  S2+z*(S3+z*(S4+z*(S5+z*S6)));
        if(iy==0) return x+v*(S1+z*r);
        else      return x-((z*(half*y-v*r)-y)-v*S1);
    }
    

    Source: http://www.netlib.org/fdlibm/k_sin.c

    While not what I’d call pleasant, you definitely can convert that whole function into a macro that will evaluate to a (compile-time) floating point constant expression. (Ignore the bit hackery at the beginning that has nothing to do with the value, and as far as I know you should assume iy is 0.)

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

Sidebar

Related Questions

Is there a way to convince Crystal Reports to export a page / group
Is there a way to convince the VBA editor in Excel to stop auto-formatting
I have a workable solution but I'm pretty convinced there's a better way of
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or
is there way how to get name ov event from Lambda expression like with
Is there way to better identify design pattern in source codes, esp. if you
Is there way to mute an audio stream or at least control the volume?
Is there way to set @include mixin(); to variable? I tried this @mixin bg-gradient($fallback,
Is there way that I can read the file from remote server using fopen

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.