Difference between revisions of "Sinc function"

From HaskellWiki
Jump to navigation Jump to search
Line 1: Line 1:
 
== Sinc function ==
 
== Sinc function ==
   
The sinc function <math>\frac{sin(x)}{x}</math> is a useful function that is a little tricky to use because it becomes 0/0 as x tends to 0. Here is an implementation taken from the [http://www.boost.org/boost/math/special_functions/sinc.hpp Boost] library.
+
The sinc function <math>\frac{sin(x)}{x}</math> is a useful function that is a little tricky to implement because it becomes 0/0 as x tends to 0. Here is an implementation taken from the [http://www.boost.org/boost/math/special_functions/sinc.hpp Boost] library.
   
 
<pre>
 
<pre>

Revision as of 22:00, 28 February 2006

Sinc function

The sinc function is a useful function that is a little tricky to implement because it becomes 0/0 as x tends to 0. Here is an implementation taken from the Boost library.

epsilon :: RealFloat a => a
epsilon = encodeFloat 1 (fromIntegral $ 1-floatDigits epsilon)

{- Boosted from Boost http://www.boost.org/boost/math/special_functions/sinc.hpp -}
sinc :: (RealFloat a) => a -> a
sinc x | (abs x) >= taylor_n_bound = (sin x)/x
       | otherwise = 1 - (x^2/6) + (x^4/120)
 where
  taylor_n_bound = sqrt $ sqrt epsilon