isSafeCast

Determine whether an array data type can be safely cast to another array data type.

Usage

var isSafeCast = require( '@stdlib/array/base/assert/is-safe-data-type-cast' );

isSafeCast( from, to )

Returns a boolean indicating whether an array data type can be safely cast to another array data type.

var bool = isSafeCast( 'float32', 'float64' );
// returns true

bool = isSafeCast( 'float64', 'int32' );
// returns false

Examples

var cartesianSquare = require( '@stdlib/array/cartesian-square' );
var dtypes = require( '@stdlib/array/dtypes' );
var isSafeCast = require( '@stdlib/array/base/assert/is-safe-data-type-cast' );

// Generate a list of dtype pairs:
var dt = cartesianSquare( dtypes() );

// For each data type pair, determine whether one can safely cast from one data type to another...
var i;
for ( i = 0; i < dt.length; i++ ) {
    console.log( '%s. Safe? %s.', dt[i].join( ' => ' ), isSafeCast.apply( null, dt[i] ) );
}
Did you find this page helpful?