All files / base/assert/has-almost-same-values/lib main.js

61.24% Statements 128/209
100% Branches 1/1
0% Functions 0/3
61.24% Lines 128/209

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 2101x 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  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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.
*/
 
'use strict';
 
// MODULES //
 
var isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' );
var isComplex64Array = require( '@stdlib/array/base/assert/is-complex64array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var isAlmostSameValueF32 = require( '@stdlib/number/float32/base/assert/is-almost-same-value' );
var isAlmostSameValueF64 = require( '@stdlib/number/float64/base/assert/is-almost-same-value' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
 
 
// FUNCTIONS //
 
/**
* Tests if two arrays have respective elements which are approximately the same value within a specified number of ULPs (units in the last place).
*
* @private
* @param {Collection} x - first input array
* @param {Collection} y - second input array
* @param {NonNegativeInteger} maxULP - maximum allowed ULP difference
* @param {Function} fcn - function which tests approximate equality
* @returns {boolean} boolean indicating if both arrays have respective elements which are approximately the same value
*
* @example
* var base = require( '@stdlib/number/float64/base/assert/is-almost-same-value' );
*
* var x = [ 0, 0, 1, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = internal( x, y, 1, base );
* // returns true
*
* @example
* var base = require( '@stdlib/number/float64/base/assert/is-almost-same-value' );
*
* var x = [ 0, 0, 0, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = internal( x, y, 1, base );
* // returns false
*/
function internal( x, y, maxULP, fcn ) {
	var i;
	for ( i = 0; i < x.length; i++ ) {
		if ( !fcn( x[ i ], y[ i ], maxULP ) ) {
			return false;
		}
	}
	return true;
}
 
/**
* Tests if two arrays have respective elements which are approximately the same value within a specified number of ULPs (units in the last place).
*
* @private
* @param {Object} x - first input array object
* @param {Object} y - second input array object
* @param {NonNegativeInteger} maxULP - maximum allowed ULP difference
* @param {Function} fcn - function which tests approximate equality
* @returns {boolean} boolean indicating if both arrays have respective elements which are approximately the same value
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
* var assert = require( '@stdlib/assert/is-almost-same-value' );
*
* var x = arraylike2object( toAccessorArray( [ 0, 0, 1, 0 ] ) );
* var y = arraylike2object( toAccessorArray( [ 0, 0, 1, 0 ] ) );
*
* var out = accessors( x, y, 0, assert );
* // returns true
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
* var assert = require( '@stdlib/assert/is-almost-same-value' );
*
* var x = arraylike2object( toAccessorArray( [ 0, 0, 0, 0 ] ) );
* var y = arraylike2object( toAccessorArray( [ 0, 0, 1, 0 ] ) );
*
* var out = accessors( x, y, 1, assert );
* // returns false
*/
function accessors( x, y, maxULP, fcn ) {
	var xdata;
	var ydata;
	var xget;
	var yget;
	var i;

	xdata = x.data;
	ydata = y.data;

	xget = x.accessors[ 0 ];
	yget = y.accessors[ 0 ];

	for ( i = 0; i < xdata.length; i++ ) {
		if ( !fcn( xget( xdata, i ), yget( ydata, i ), maxULP ) ) {
			return false;
		}
	}
	return true;
}
 
 
// MAIN //
 
/**
* Tests if two arrays have respective elements which are approximately the same value within a specified number of ULPs (units in the last place).
*
* @param {Collection} x - first input array
* @param {Collection} y - second input array
* @param {NonNegativeInteger} maxULP - maximum allowed ULP difference
* @returns {boolean} boolean indicating if both arrays have respective elements which are approximately the same value
*
* @example
* var x = [ 0, 0, 1, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = hasAlmostSameValues( x, y, 0 );
* // returns true
*
* @example
* var x = [ 0, 0, 0, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = hasAlmostSameValues( x, y, 1 );
* // returns false
*/
function hasAlmostSameValues( x, y, maxULP ) {
	var FLG;
	var xo;
	var yo;
	var xr;
	var yr;

	if ( x.length !== y.length ) {
		return false;
	}
	xo = arraylike2object( x );
	yo = arraylike2object( y );
	if ( xo.accessorProtocol || yo.accessorProtocol ) {
		FLG = 2;

		// If provided boolean arrays, reinterpret the arrays to avoid using accessors to access array elements...
		if ( isBooleanArray( x ) ) {
			if ( isBooleanArray( y ) ) {
				return internal( reinterpretBoolean( x, 0 ), reinterpretBoolean( y, 0 ), maxULP, isAlmostSameValueF64 ); // eslint-disable-line max-len
			}
			return accessors( xo, yo, maxULP, isAlmostSameValue ); // general accessors should always use general comparison
		}
		// If provided a complex number array, reinterpret as a real typed array and test interleaved real and imaginary components...
		if ( isComplex128Array( x ) ) {
			xr = reinterpret128( x, 0 );
			FLG -= 1;
		} else if ( isComplex64Array( x ) ) {
			xr = reinterpret64( x, 0 );
			FLG -= 1;
		}
		if ( isComplex128Array( y ) ) {
			yr = reinterpret128( y, 0 );
			FLG -= 1;
		} else if ( isComplex64Array( y ) ) {
			yr = reinterpret64( y, 0 );
			FLG -= 1;
		}
		if ( FLG === 0 ) {
			// Determine whether we should use double-precision or single-precision comparison...
			if ( xr.BYTES_PER_ELEMENT === 8 || yr.BYTES_PER_ELEMENT === 8 ) {
				return internal( xr, yr, maxULP, isAlmostSameValueF64 );
			}
			return internal( xr, yr, maxULP, isAlmostSameValueF32 );
		}
		return accessors( xo, yo, maxULP, isAlmostSameValue ); // general accessors should always use general comparison
	}
	// Determine whether we should use double-precision or single-precision comparison...
	if ( x.BYTES_PER_ELEMENT === 4 && y.BYTES_PER_ELEMENT === 4 ) {
		return internal( x, y, maxULP, isAlmostSameValueF32 );
	}
	return internal( x, y, maxULP, isAlmostSameValue ); // default to general comparison
}
 
 
// EXPORTS //
 
module.exports = hasAlmostSameValues;