time-to-botec

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

snanmskmin.js (2278B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 // MODULES //
     22 
     23 var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
     24 var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes the minimum value of a single-precision floating-point strided array according to a mask, ignoring `NaN` values.
     31 *
     32 * @param {PositiveInteger} N - number of indexed elements
     33 * @param {Float32Array} x - input array
     34 * @param {integer} strideX - `x` stride length
     35 * @param {Uint8Array} mask - mask array
     36 * @param {integer} strideMask - `mask` stride length
     37 * @returns {number} minimum value
     38 *
     39 * @example
     40 * var Float32Array = require( '@stdlib/array/float32' );
     41 * var Uint8Array = require( '@stdlib/array/uint8' );
     42 *
     43 * var x = new Float32Array( [ 1.0, -2.0, -4.0, 2.0, NaN ] );
     44 * var mask = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
     45 *
     46 * var v = snanmskmin( x.length, x, 1, mask, 1 );
     47 * // returns -2.0
     48 */
     49 function snanmskmin( N, x, strideX, mask, strideMask ) {
     50 	var min;
     51 	var ix;
     52 	var im;
     53 	var v;
     54 	var i;
     55 
     56 	if ( N <= 0 ) {
     57 		return NaN;
     58 	}
     59 	if ( strideX < 0 ) {
     60 		ix = (1-N) * strideX;
     61 	} else {
     62 		ix = 0;
     63 	}
     64 	if ( strideMask < 0 ) {
     65 		im = (1-N) * strideMask;
     66 	} else {
     67 		im = 0;
     68 	}
     69 	for ( i = 0; i < N; i++ ) {
     70 		v = x[ ix ];
     71 		if ( v === v && mask[ im ] === 0 ) {
     72 			break;
     73 		}
     74 		ix += strideX;
     75 		im += strideMask;
     76 	}
     77 	if ( i === N ) {
     78 		return NaN;
     79 	}
     80 	min = v;
     81 	i += 1;
     82 	for ( i; i < N; i++ ) {
     83 		ix += strideX;
     84 		im += strideMask;
     85 		if ( mask[ im ] ) {
     86 			continue;
     87 		}
     88 		v = x[ ix ];
     89 		if ( isnanf( v ) ) {
     90 			continue;
     91 		}
     92 		if ( v < min || ( v === min && isNegativeZerof( v ) ) ) {
     93 			min = v;
     94 		}
     95 	}
     96 	return min;
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = snanmskmin;