Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on generation 3d perlin noise. The C# Math library seems like overkill for what I need since most of its functions use double percision. I use Math.Sin() in several places to generate the noise. Does anyone know of a faster sine function?

share|improve this question

3 Answers 3

up vote 29 down vote accepted

You can use a parabola to aproximate the value of the sine function. This has the advantage of having the roots at exactly -pi/2 and pi/2 which is usually not the case with other fast approximations based on the TaylorSeries or MaclaurinSeries.

public float Sin(float x)
{
    const float B = 4 / PI;
    const float C = -4 / (PI*PI);

    return -(B * x + C * x * ((x < 0) ? -x : x));
} 

Here is a comparison to the actual sine function:

alt text

share|improve this answer
3  
This is indeed a great solution. Here is an excellent article from devmaster.net which describes why this works and gives some implementation details: devmaster.net/forums/showthread.php?t=5784 –  reverbb Oct 23 '10 at 20:25
    
I don't know about C#, but the abs() function in most C environments will likely be faster than a branch (the ?: operator), when optimized. –  user744 Oct 23 '10 at 20:50
3  
I removed the Math.Abs() call because I assumed that this code might run on the Xbox 360 or Windows Phone 7. The JIT compiler on Xbox 360 does not inline anything. A call to Math.Abs() is actually more expensive. –  arriu Oct 23 '10 at 21:09

What is the range of input values to your sin() function? For what you're using it for, it sounds like they might be limited, which means you could pre-compute the values. For instance, if you're rounding up the input values to the nearest degree, then you only have 360 possible values - just pre-compute them and store in a table.

If you need slightly more values, say to one decimal place, you could interpolate from the table - I'm not familiar with perlin noise, but the word "noise" seems to indicate it doesn't require high accuracy. :) (You could also just make a larger table, 3600 entries isn't much space).

share|improve this answer
3  
If speed is your number-one concern, and you do not mind sacrificing a bit of accuracy, this is the best answer. –  AttackingHobo Oct 24 '10 at 21:07
    
I don't know about "best" - As shown in another answer, you can get another very good approximation in five ops + abs (the speed of which depends on your arch / compiler, but is often branchless). If the lookup table is not in cache, it's going to be much slower. –  user744 Oct 26 '10 at 12:31

You might want to read this too, it's got fast sine and cosine approximations

share|improve this answer
1  
This link is 404. But you can find a backup here: web.archive.org/web/20110925033606/http://lab.polygonal.de/2007/… –  eisberg Jun 26 '13 at 12:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.