sinpx.js (2055B)
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 * ## Notice 20 * 21 * The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_64_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript. 22 * 23 * ```text 24 * Copyright John Maddock 2006-7, 2013-14. 25 * Copyright Paul A. Bristow 2007, 2013-14. 26 * Copyright Nikhar Agrawal 2013-14. 27 * Copyright Christopher Kormanyos 2013-14. 28 * 29 * Use, modification and distribution are subject to the 30 * Boost Software License, Version 1.0. (See accompanying file 31 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 32 * ``` 33 */ 34 35 'use strict'; 36 37 // TODO: consider moving this to a separate pkg: @stdlib/math/base/special/xsinpi 38 39 // MODULES // 40 41 var isOdd = require( './../../../../../base/assert/is-odd' ); 42 var floor = require( './../../../../../base/special/floor' ); 43 var sinpi = require( './../../../../../base/special/sinpi' ); 44 45 46 // MAIN // 47 48 /** 49 * Calculates `x * sin(pi * x)`, taking extra care near when `x` is near a whole number. 50 * 51 * @private 52 * @param {number} x - input value 53 * @returns {number} function value 54 */ 55 function sinpx( x ) { 56 var result; 57 var dist; 58 var sign; 59 var fl; 60 61 sign = 1; 62 if ( x < 0.0 ) { 63 x = -x; 64 } 65 fl = floor( x ); 66 if ( isOdd(fl) ) { 67 fl += 1; 68 dist = fl - x; 69 sign = -sign; 70 } else { 71 dist = x - fl; 72 } 73 if ( dist > 0.5 ) { 74 dist = 1.0 - dist; 75 } 76 result = sinpi( dist ); 77 return sign*x*result; 78 } 79 80 81 // EXPORTS // 82 83 module.exports = sinpx;