time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

sinc.js (1876B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var sinpi = require( './../../../../base/special/sinpi' );
     24 var isnan = require( './../../../../base/assert/is-nan' );
     25 var isInfinite = require( './../../../../base/assert/is-infinite' );
     26 var PI = require( '@stdlib/constants/float64/pi' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Computes the normalized cardinal sine of a number.
     33 *
     34 * ## Method
     35 *
     36 * For \\( x \neq 0 \\), the normalized cardinal sine is calculated as
     37 *
     38 * ```tex
     39 * \operatorname{sinc}(x) = \frac{\operatorname{sin}(\pi x)}{\pi x}.
     40 * ```
     41 *
     42 *
     43 * ## Special Cases
     44 *
     45 * ```tex
     46 * \begin{align*}
     47 * \operatorname{sinc}(0) &= 1 & \\
     48 * \operatorname{sinc}(\infty) &= 0 & \\
     49 * \operatorname{sinc}(-\infty) &= 0 & \\
     50 * \operatorname{sinc}(\mathrm{NaN}) &= \mathrm{NaN}
     51 * \end{align*}
     52 * ```
     53 *
     54 *
     55 * @param {number} x - input value
     56 * @returns {number} cardinal sine
     57 *
     58 * @example
     59 * var v = sinc( 0.5 );
     60 * // returns ~0.637
     61 *
     62 * @example
     63 * var v = sinc( -1.2 );
     64 * // returns ~-0.156
     65 *
     66 * @example
     67 * var v = sinc( 0.0 );
     68 * // returns 1.0
     69 *
     70 * @example
     71 * var v = sinc( NaN );
     72 * // returns NaN
     73 */
     74 function sinc( x ) {
     75 	if ( isnan( x ) ) {
     76 		return NaN;
     77 	}
     78 	if ( isInfinite( x ) ) {
     79 		return 0.0;
     80 	}
     81 	if ( x === 0.0 ) {
     82 		return 1.0;
     83 	}
     84 	return sinpi( x ) / ( PI*x );
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = sinc;