isSafeCast

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

Usage

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

isSafeCast( from, to )

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

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

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

Examples

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

var DTYPES;
var bool;
var dt;
var i;
var j;

// Get a list of supported ndarray data types:
DTYPES = dtypes();

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