All files main.js

100% Statements 149/149
100% Branches 7/7
100% Functions 1/1
100% Lines 149/149

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 161x 161x 5003x 2x 2x 4842x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 4840x 5003x 501x 501x 5003x 5003x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* The original C code, long comment, copyright, license, and constants are from [Cephes]{@link http://www.netlib.org/cephes}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
*
* Some software in this archive may be from the book _Methods and Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) or from the Cephes Mathematical Library, a commercial product. In either event, it is copyrighted by the author. What you see here may be used freely but it comes with no support or guarantee.
*
* Stephen L. Moshier
* moshier@na-net.ornl.gov
* ```
*/
 
'use strict';
 
// MODULES //
 
var sincos = require( '@stdlib/math/base/special/sincos' ).assign;
var abs = require( '@stdlib/math/base/special/abs' );
var fmod = require( '@stdlib/math/base/special/fmod' );
var HALF_PI = require( '@stdlib/constants/float64/half-pi' );
var PI = require( '@stdlib/constants/float64/pi' );
var polyS = require( './rational_psqs.js' );
var polyF = require( './rational_pfqf.js' );
var polyG = require( './rational_pgqg.js' );
 
 
// VARIABLES //
 
// Array for storing sincos evaluation:
var sc = [ 0.0, 0.0 ];
 
 
// MAIN //
 
/**
* Computes the Fresnel integral S(x).
*
* ## Method
*
* Evaluates the Fresnel integral
*
* ```tex
* \operatorname{S}(x) = \int_0^x \sin\left(\frac{\pi}{2} t^2\right)\,\mathrm{d}t
* ```
*
* The integral is evaluated by a power series for \\( x < 1 \\). For \\( x >= 1 \\) auxiliary functions \\( f(x) \\) and \\( g(x) \\) are employed such that
*
* ```tex
* \operatorname{S}(x) = \frac{1}{2} - f(x) \cos\left( \frac{\pi}{2} x^2 \right) - g(x) \sin\left( \frac{\pi}{2} x^2 \right).
* ```
*
* ## Notes
*
* -   Relative error on test interval \\( \[0,10\] \\):
*
*     | arithmetic | function | # trials | peak    | rms     |
*     |:----------:|:--------:|:--------:|:--------:|:-------:|
*     | IEEE       | S(x)     | 10000    | 2.0e-15 | 3.2e-16 |
*
* @param {number} x - input value
* @returns {number} S(x)
*
* @example
* var v = fresnels( 0.0 );
* // returns 0.0
*
* @example
* var v = fresnels( 1.0 );
* // returns ~0.438
*
* @example
* var v = fresnels( Infinity );
* // returns ~0.5
*
* @example
* var v = fresnels( -Infinity );
* // returns ~-0.5
*
* @example
* var v = fresnels( NaN );
* // returns NaN
*/
function fresnels( x ) {
	var x2;
	var hi;
	var lo;
	var xa;
	var S;
	var f;
	var g;
	var t;
	var u;
 
	xa = abs( x );
	x2 = xa * xa;
	if ( x2 < 2.5625 ) {
		t = x2 * x2;
		S = xa * x2 * polyS( t );
	} else if ( xa > 1.0e16 ) {
		// Above `1e16`, the oscillating terms, which are at most `1/(πx)`, are below half an ulp of `0.5`:
		S = 0.5;
	} else {
		// Asymptotic power series auxiliary functions for large arguments...
		x2 = xa * xa;
		t = PI * x2;
		u = 1.0 / (t * t);
		t = 1.0 / t;
		f = 1.0 - ( u * polyF( u ) );
		g = t * polyG( u );
 
		// `sin( π x^2 / 2 )` and `cos( π x^2 / 2 )` have period 4 in `x^2`, and forming `x*x` rounds away bits that the large argument turns into whole radians. Reduce `x^2` modulo 4 exactly instead: split `x = hi + lo` (Veltkamp, with `134217729 = 2^27+1`) so that `hi*hi`, `2*hi*lo` and `lo*lo` are exact, and reduce each term:
		t = 134217729.0 * xa;
		hi = t - ( t - xa );
		lo = xa - hi;
		t = fmod( hi*hi, 4.0 ) + fmod( 2.0*hi*lo, 4.0 ) + ( lo*lo );
		sincos( HALF_PI * t, sc, 1, 0 );
		t = PI * xa;
		S = 0.5 - ( ( (f*sc[1]) + (g*sc[0]) ) / t );
	}
	if ( x < 0.0 ) {
		S = -S;
	}
	return S;
}
 
 
// EXPORTS //
 
module.exports = fresnels;