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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 2x 2x 2x 2x 10008x 10008x 10008x 10008x 10010x 386x 386x 386x 10010x 6x 6x 6x 9622x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 9616x 10010x 1004x 1004x 1004x 10008x 10010x 3x 3x 3x 3x 3x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 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 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 HALF_PI = require( '@stdlib/constants/float32/half-pi' );
var PI = require( '@stdlib/constants/float32/pi' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var absf = require( '@stdlib/math/base/special/absf' );
var fmodf = require( '@stdlib/math/base/special/fmodf' );
var sincosf = require( '@stdlib/math/base/special/sincosf' ).assign;
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var polyC = require( './polyval_cn.js' );
var polyF = require( './polyval_fn.js' );
var polyG = require( './polyval_gn.js' );
var polyS = require( './polyval_sn.js' );
// VARIABLES //
// Array for storing sincos evaluation:
var sc = [ 0.0, 0.0 ];
// MAIN //
/**
* Computes the Fresnel integrals S(x) and C(x) for a single-precision floating-point number and assigns results to a provided output array.
*
* ## Method
*
* Evaluates the Fresnel integrals
*
* ```tex
* \begin{align*}
* \operatorname{S}(x) &= \int_0^x \sin\left(\frac{\pi}{2} t^2\right)\,\mathrm{d}t, \\
* \operatorname{C}(x) &= \int_0^x \cos\left(\frac{\pi}{2} t^2\right)\,\mathrm{d}t.
* \end{align*}
* ```
*
* The integrals are evaluated by a power series for small \\( x \\). For larger \\( x \\), auxiliary functions \\( f(x) \\) and \\( g(x) \\) are employed such that
*
* ```tex
* \begin{align*}
* \operatorname{C}(x) &= \frac{1}{2} + f(x) \sin\left( \frac{\pi}{2} x^2 \right) - g(x) \cos\left( \frac{\pi}{2} x^2 \right), \\
* \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).
* \end{align*}
* ```
*
* ## Notes
*
* - Relative error on test interval \\( \[0,10\] \\):
*
* | arithmetic | function | # trials | peak | rms |
* |:----------:|:--------:|:--------:|:-------:|:-------:|
* | IEEE | S(x) | 30000 | 1.1e-6 | 1.9e-7 |
* | IEEE | C(x) | 30000 | 1.1e-6 | 2.0e-7 |
*
* @param {number} x - input value
* @param {Collection} out - output array
* @param {integer} stride - output array stride
* @param {NonNegativeInteger} offset - output array index offset
* @returns {Collection} output array
*
* @example
* var v = fresnelf( 0.0, [ 0.0, 0.0 ], 1, 0 );
* // returns [ 0.0, 0.0 ]
*
* @example
* var v = fresnelf( 1.0, [ 0.0, 0.0 ], 1, 0 );
* // returns [ ~0.438, ~0.780 ]
*
* @example
* var v = fresnelf( Infinity, [ 0.0, 0.0 ], 1, 0 );
* // returns [ ~0.5, ~0.5 ]
*
* @example
* var v = fresnelf( -Infinity, [ 0.0, 0.0 ], 1, 0 );
* // returns [ ~-0.5, ~-0.5 ]
*
* @example
* var v = fresnelf( NaN, [ 0.0, 0.0 ], 1, 0 );
* // returns [ NaN, NaN ]
*/
function fresnelf( x, out, stride, offset ) {
var x2;
var hi;
var lo;
var xa;
var f;
var g;
var t;
var u;
// Special case: NaN
if ( isnanf( x ) ) {
out[ offset ] = NaN;
out[ offset + stride ] = NaN;
return out;
}
x = f32( x );
xa = absf( x );
x2 = f32( xa * xa );
if ( x2 < 2.5625 ) {
t = f32( x2 * x2 );
out[ offset ] = f32( f32( xa * x2 ) * polyS( t ) );
out[ offset + stride ] = f32( xa * polyC( t ) );
} else if ( xa > 16777216.0 ) {
// Above `2^24`, the oscillating terms, which are at most `1/(πx)`, are below half an ulp of `0.5`:
out[ offset ] = 0.5;
out[ offset + stride ] = 0.5;
} else {
// Asymptotic power series auxiliary functions for large arguments...
t = f32( PI * x2 );
u = f32( 1.0 / f32( t * t ) );
t = f32( 1.0 / t );
f = f32( 1.0 - f32( u * polyF( u ) ) );
g = f32( 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 `4097 = 2^12+1`) so that `hi*hi`, `2*hi*lo` and `lo*lo` are exact, and reduce each term:
t = f32( 4097.0 * xa );
hi = f32( t - f32( t - xa ) );
lo = f32( xa - hi );
t = f32( f32( fmodf( f32( hi*hi ), 4.0 ) + fmodf( f32( 2.0*f32( hi*lo ) ), 4.0 ) ) + f32( lo*lo ) );
sincosf( f32( HALF_PI * t ), sc, 1, 0 );
t = f32( PI * xa );
out[ offset + stride ] = f32( 0.5 + f32( f32( f32( f * sc[0] ) - f32( g * sc[1] ) ) / t ) ); // eslint-disable-line max-len
out[ offset ] = f32( 0.5 - f32( f32( f32( f * sc[1] ) + f32( g * sc[0] ) ) / t ) ); // eslint-disable-line max-len
}
if ( x < 0.0 ) {
out[ offset ] = -out[ offset ];
out[ offset + stride ] = -out[ offset + stride ];
}
return out;
}
// EXPORTS //
module.exports = fresnelf;
|