# Want source code for cmath's sin function



## aewarnick (Sep 3, 2002)

I have found that some basic functions in the windows headers are VERY slow. I would like to make my own sin function to test it out but I have no idea how. Please help me make a sin function.


----------



## brendandonhu (Jul 8, 2002)

Are you trying to write your own header files?
You could look at the code from an open source compiler, like the GNU Compiler Collection. I think the function you want is in mathinline.h


----------



## aewarnick (Sep 3, 2002)

I downloaded GNU source code but I could not find mathinline.h. What I did find was the code for sin split up into about 4 different source files and I still did not get solid code.

Are there any other suggestions? Maybe a math reference explaining how sin works? I tried looking sin up on the net and I couldn't find anything useful.


----------



## AGCurry (Jun 15, 2005)

You will not find the function code in any *.h (include) file - the .h file is for the function prototypes so the linker knows return and argument types. The function itself will be stored in some library. I'm a unix guy, so for me it would be "libmath.a".

As I recall from trigonometry (took it in 1966), the sine, cosine, and tangent functions are all based on the relationships of the three sides of right triangles. The sin() function in C, however, takes a single value in radians (a circle contains 2*pi radians). Perhaps you could simply write new functions, but I really don't see how you're going to improve on what is probably a very simple calculation. Why do you think the math library is slowing you down?


----------



## aewarnick (Sep 3, 2002)

Unix, good for you! I wish I could get away from windows!

Moving on...I don't know if it is slow or not yet. I spoke with someone else about slow functions after finding one myself. He also confirmed that he too found incredibly slow functions. The one I found is SetRect.

//SetRect is extremely slow: 23 sec compared to 5 sec.
void setRect(RECT* R, long l, long t, long r, long b)
{
R->left= l; R->top= t; R->right= r; R->bottom= b;
}

I am using VC++ 2002 with /O2 optimizations. I have noticed that my setRect is only faster because the compiler automatically inlines it for some reason. Because it is just as fast as a direct assign. Though my code is probably not much different than SetRect, the compiler compiles mine better, so it's worth using over SetRect.

Try it for yourself and see.


----------



## AGCurry (Jun 15, 2005)

aewarnick said:


> //SetRect is extremely slow: 23 sec compared to 5 sec.
> void setRect(RECT* R, long l, long t, long r, long b)
> {
> R->left= l; R->top= t; R->right= r; R->bottom= b;
> ...


How could this be slow? It's just 4 integral assignments! 23 seconds? For how many iterations? I do wonder, though, why a RECT needs 4 values when two would do, i.e., the top and bottom will be identical, as will the other two sides...

If it's a lot faster when it's "inlined", perhaps it's a stack problem. Do be cautious when assuming something's slow, because code-execution speed depends a LOT on what else your CPU is doing!


----------



## aewarnick (Sep 3, 2002)

Source Code Included

Let me know what you find out.


----------



## lotuseclat79 (Sep 12, 2003)

aewarnick said:


> I have found that some basic functions in the windows headers are VERY slow. I would like to make my own sin function to test it out but I have no idea how. Please help me make a sin function.


Hi aewarnick,

Found this on the web:

Computing sine ref: http://www.answers.com/topic/lookup-table

Most computers, which only perform basic arithmetic operations, cannot directly calculate the sine of a given value. Instead, they use a complex formula such as the following Taylor series to compute the value of sine to a high degree of precision:

\operatorname{sin}(x) \approx x - \frac{x^3}{6} + \frac{x^5}{120} - \frac{x^7}{5040} (for x close to 0)

However, this can be expensive to compute, especially on slow processors, and there are many applications, particularly in traditional computer graphics, that need to compute many thousands of sine values every second. A common solution is to initially compute the sine of many evenly distributed values, and then to find the sine of x we choose the sine of the value closest to x. This will be close to the correct value because sine is a continuous function. For example:

real array sine_table[-1000..1000]
for x from -1000 to 1000
sine_table[x] := sine(x/1000/pi)

function lookup_sine(x)
return sine_table[round(x/1000/pi)]

Unfortunately, the table requires quite a bit of space: if IEEE double-precision floating-point numbers are used, over 16,000 bytes would be required. We can use less samples, but then our precision will significantly worsen. One good solution is linear interpolation, which draws a line between the two points in the table on either side of the value and locates the answer on that line. This is still quick to compute, and much more accurate for smooth functions such as the sine function. Here is our example using linear interpolation:

function lookup_sine(x)
x1 := floor(x/1000/pi)
y1 := sine_table[x1]
y2 := sine_table[x1+1]
return y1 + 1000*pi*(y2-y1)*(x-x1)

When using interpolation, it's often beneficial to use nonuniform sampling, which means that where the function is close to straight, we use few sample points, while where it changes value quickly we use more sample points to keep the approximation close to the real curve. For more information, see interpolation (a link on the above referenced webpage too long for here).

-- Tom


----------

