Difference between revisions of "Sinc function"

From HaskellWiki
Jump to navigation Jump to search
(Category:Mathematics)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
The sinc function, <math>\frac{\sin(x)}{x}</math>, is a useful function that is a little tricky to implement because it becomes <math>\frac{0}{0}</math> as x approaches <math>0</math>. Here is an implementation taken from the [http://www.boost.org/boost/math/special_functions/sinc.hpp Boost] library.
== Sinc function ==
 
   
  +
<haskell>
The sinc function <math>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.
 
 
<pre>
 
 
epsilon :: RealFloat a => a
 
epsilon :: RealFloat a => a
 
epsilon = encodeFloat 1 (fromIntegral $ 1-floatDigits epsilon)
 
epsilon = encodeFloat 1 (fromIntegral $ 1-floatDigits epsilon)
Line 9: Line 7:
 
{- Boosted from Boost http://www.boost.org/boost/math/special_functions/sinc.hpp -}
 
{- Boosted from Boost http://www.boost.org/boost/math/special_functions/sinc.hpp -}
 
sinc :: (RealFloat a) => a -> a
 
sinc :: (RealFloat a) => a -> a
  +
sinc x =
sinc x | (abs x) >= taylor_n_bound = (sin x)/x
+
if abs x >= taylor_n_bound
| otherwise = 1 - (x^2/6) + (x^4/120)
 
  +
then sin x / x
 
else 1 - x^2/6 + x^4/120
 
where
 
where
 
taylor_n_bound = sqrt $ sqrt epsilon
 
taylor_n_bound = sqrt $ sqrt epsilon
</pre>
+
</haskell>
  +
  +
[[Category:Code]]
  +
[[Category:Mathematics]]

Latest revision as of 16:35, 15 November 2006

The sinc function, , is a useful function that is a little tricky to implement because it becomes as x approaches . 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 =
   if abs x >= taylor_n_bound
     then sin x / x
     else 1 - x^2/6 + x^4/120
 where
  taylor_n_bound = sqrt $ sqrt epsilon