time-to-botec

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

cospi.js (2337B)


      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 /*
     22 * Notes:
     23 *	=> cos(-x) = cos(x)
     24 *	=> sin(-x) = -sin(x)
     25 *	=> cos(π/2) = 0
     26 *	=> cos(0) = 1
     27 *	=> cos(π) = -1
     28 */
     29 
     30 
     31 // MODULES //
     32 
     33 var isnan = require( './../../../../base/assert/is-nan' );
     34 var isInfinite = require( './../../../../base/assert/is-infinite' );
     35 var abs = require( './../../../../base/special/abs' );
     36 var cos = require( './../../../../base/special/cos' );
     37 var sin = require( './../../../../base/special/sin' );
     38 var floor = require( './../../../../base/special/floor' );
     39 var PI = require( '@stdlib/constants/float64/pi' );
     40 var MAX_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
     41 
     42 
     43 // VARIABLES //
     44 
     45 MAX_INTEGER += 1;
     46 
     47 
     48 // MAIN //
     49 
     50 /**
     51 * Computes the value of `cos(πx)`.
     52 *
     53 * @param {number} x - input value
     54 * @returns {number} function value
     55 *
     56 * @example
     57 * var y = cospi( 0.0 );
     58 * // returns 1.0
     59 *
     60 * @example
     61 * var y = cospi( 0.5 );
     62 * // returns 0.0
     63 *
     64 * @example
     65 * var y = cospi( 0.1 );
     66 * // returns ~0.951
     67 *
     68 * @example
     69 * var y = cospi( NaN );
     70 * // returns NaN
     71 */
     72 function cospi( x ) {
     73 	var ax;
     74 	var ix;
     75 	var rx;
     76 	var y;
     77 	if ( isnan( x ) ) {
     78 		return NaN;
     79 	}
     80 	if ( isInfinite( x ) ) {
     81 		return NaN;
     82 	}
     83 	ax = abs( x );
     84 	if ( ax > MAX_INTEGER ) {
     85 		// Always even integer...
     86 		return 1.0;
     87 	}
     88 	// Argument reduction (reduce to [0,1))...
     89 	ix = floor( ax );
     90 	rx = ax - ix;
     91 	if ( rx === 0.5 ) {
     92 		return 0.0;
     93 	}
     94 	if ( rx < 0.25 ) {
     95 		y = cos( PI*rx );
     96 	}
     97 	else if ( rx < 0.75 ) {
     98 		rx = 0.5 - rx;
     99 		y = sin( PI*rx ); // recall sin(-x) = -sin(x), thus returned result will be properly signed
    100 	}
    101 	else {
    102 		rx = 1.0 - rx;
    103 		y = -cos( PI*rx );
    104 	}
    105 	// If the integer of `x` is odd, we need to flip the sign...
    106 	return ( ix%2 === 1 ) ? -y : y;
    107 }
    108 
    109 
    110 // EXPORTS //
    111 
    112 module.exports = cospi;