All files main.js

100% Statements 242/242
100% Branches 22/22
100% Functions 1/1
100% Lines 242/242

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 2431x 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 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 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 3x 3x 3x 3x 13x 13x 16x 4x 4x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 9x 9x 16x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 7x 7x 16x 5x 5x 5x 5x 5x 5x 2x 2x 5x 3x 3x 1x 1x 2x 2x 2x 4x 4x 4x 4x 4x 4x 5x 3x 3x 3x 1x 1x 2x 16x 1x 1x 1x 16x 1x 1x 1x 1x 1x  
/* eslint-disable stdlib/jsdoc-no-shortcut-reference-link, stdlib/jsdoc-no-undefined-references */
 
/**
* @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 following copyright, license, and long comment were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/s_erff.c}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ```
*/
 
/* eslint-enable stdlib/jsdoc-no-shortcut-reference-link, stdlib/jsdoc-no-undefined-references */
 
'use strict';
 
// MODULES //
 
var exp = require( '@stdlib/math/base/special/exp' );
var absf = require( '@stdlib/math/base/special/absf' );
var toWordf = require( '@stdlib/number/float32/base/to-word' );
var fromWordf = require( '@stdlib/number/float32/base/from-word' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var polyvalPP = require( './polyval_pp.js' );
var polyvalQQ = require( './polyval_qq.js' );
var polyvalPA = require( './polyval_pa.js' );
var polyvalQA = require( './polyval_qa.js' );
var polyvalRA = require( './polyval_ra.js' );
var polyvalSA = require( './polyval_sa.js' );
var polyvalRB = require( './polyval_rb.js' );
var polyvalSB = require( './polyval_sb.js' );
 
 
// VARIABLES //
 
var TINY = f32( 1.0e-30 );
var HALF = f32( 0.5 );
var ONE = f32( 1.0 );
var TWO = f32( 2.0 );
 
// 0x3f57bb00
var ERX = f32( 8.42697144e-01 );
 
 
// MAIN //
 
/**
* Evaluates the complementary error function for a single-precision floating-point number.
*
* ## Method
*
* 1.  For \\( x \in [0, 0.84375] \\), we use the approximation
*
*     ```tex
*     \operatorname{erfc}(x) = 1 - \operatorname{erf}(x) = 1 - x \cdot \frac{P(x^2)}{Q(x^2)}
*     ```
*
*     where \\( P \\) and \\( Q \\) are polynomials (pp and qq).
*
* 2.  For \\( x \in [0.84375, 1.25] \\), we use the approximation
*
*     ```tex
*     \operatorname{erfc}(x) = 1 - \operatorname{erf}(x) = 1 - \operatorname{erx} - \frac{P(x-1)}{Q(x-1)}
*     ```
*
*     where \\( P \\) and \\( Q \\) are polynomials (pa and qa).
*
* 3.  For \\( x \in [1.25, 11] \\), we use the approximation
*
*     ```tex
*     \operatorname{erfc}(x) = \frac{1}{x} e^{-x^2 - 0.5625 + \frac{R(1/x^2)}{S(1/x^2)}}
*     ```
*
*     where \\( R \\) and \\( S \\) are rational functions.
*
* 4.  For \\( x \geq 11 \\), we use the approximation
*
*     ```tex
*     \operatorname{erfc}(x) = 0
*     ```
*
* ## Notes
*
* -   Relative error:
*     -   \\( |x| < 0.84375 \\): \\( 2^{-31} \\)
*     -   \\( 0.84375 \leq |x| < 1.25 \\): \\( 2^{-31} \\)
*     -   \\( 1.25 \leq |x| < 2.85715 \\): \\( 2^{-30} \\)
*     -   \\( 2.85715 \leq |x| < 11 \\): \\( 2^{-30} \\)
*
* @param {number} x - input value
* @returns {number} function value
*
* @see [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/s_erff.c}
*
* @example
* var y = erfcf( 0.0 );
* // returns 1.0
*
* @example
* var y = erfcf( 1.0 );
* // returns ~0.1573
*
* @example
* var y = erfcf( -1.0 );
* // returns ~1.8427
*
* @example
* var y = erfcf( Infinity );
* // returns 0.0
*
* @example
* var y = erfcf( -Infinity );
* // returns 2.0
*
* @example
* var y = erfcf( NaN );
* // returns NaN
*/
function erfcf( x ) {
	var xm2;
	var hx;
	var ix;
	var ax;
	var R;
	var S;
	var P;
	var Q;
	var s;
	var y;
	var z;
	var r;
 
	x = f32( x );
 
	hx = toWordf( x ) | 0;
	ix = (hx & 0x7fffffff) | 0; // remove sign bit
 
	// Handle NaN and ±Infinity
	if ( ix >= 0x7f800000 ) {
		// ((hx >>> 31) << 1) gives 0 for positive, 2 for negative
		// ONE/x gives: 1/NaN=NaN, 1/+Inf=0, 1/-Inf=-0
		return f32( f32( ((hx >>> 31) << 1) ) + f32( ONE / x ) );
	}
 
	// Case: |x| < 0.84375
	if ( ix < 0x3f580000 ) { // 0x3f580000 is the float32 representation of 0.84375
		// Case: |x| < 2**-24
		if ( ix < 0x33800000 ) {
			return f32( ONE - x );
		}
		z = f32( x * x );
		r = f32( polyvalPP( z ) );
		s = f32( polyvalQQ( z ) );
		y = f32( r / s );
		if ( hx < 0x3e800000 ) {
			// Case: x < 1/4
			return f32( ONE - f32( x + f32( x * y ) ) );
		}
		r = f32( x * y );
		r = f32( r + f32( x - HALF ) );
		return f32( HALF - r );
	}
 
	// Case: 0.84375 <= |x| < 1.25
	if ( ix < 0x3fa00000 ) {
		ax = absf( x );
		s = f32( f32( ax ) - ONE );
		P = f32( polyvalPA( s ) );
		Q = f32( polyvalQA( s ) );
		if ( hx >= 0 ) {
			z = f32( ONE - ERX );
			return f32( z - f32( P / Q ) );
		}
		z = f32( ERX + f32( P / Q ) );
		return f32( ONE + z );
	}
 
	// Case: |x| < 11
	if ( ix < 0x41300000 ) {
		ax = absf( x );
		ax = f32( ax );
		s = f32( ONE / f32( ax * ax ) );
 
		// Case: |x| < 2.85715 ~ 1/0.35
		if ( ix < 0x4036db8c ) {
			R = f32( polyvalRA( s ) );
			S = f32( polyvalSA( s ) );
		} else {
			// Case: x < -5
			if ( hx < 0 && ix >= 0x40a00000 ) {
				return TWO-TINY;
			}
			R = f32( polyvalRB( s ) );
			S = f32( polyvalSB( s ) );
		}
 
		z = f32( fromWordf( (hx & 0xffffe000) >>> 0 ) );
		xm2 = f32( f32( z - ax ) * f32( z + ax ) );
		r = f32( exp( f32( f32( f32( -z * z ) - f32( 0.5625 ) ) +
			f32( xm2 + f32( R / S ) ) ) ) );
 
		if ( hx > 0 ) {
			r = f32( r / ax );
			return r;
		}
		return f32( TWO - f32( r / ax ) );
	}
	// Case: |x| >= 11
	if ( hx > 0 ) {
		return TINY * TINY;
	}
	return TWO - TINY;
}
 
 
// EXPORTS //
 
module.exports = erfcf;