Skip to content

stdlib-js/array-float16

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

Float16Array

NPM version Build Status Coverage Status

16-bit half-precision floating-point number array.

Installation

npm install @stdlib/array-float16

Alternatively,

  • To load the package in a website via a script tag without installation and bundlers, use the ES Module available on the esm branch (see README).
  • If you are using Deno, visit the deno branch (see README for usage intructions).
  • For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the umd branch (see README).

The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.

To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.

Usage

var Float16Array = require( '@stdlib/array-float16' );

Float16Array()

Creates a 16-bit half-precision floating-point number array.

var arr = new Float16Array();
// returns <Float16Array>

Float16Array( length )

Creates a 16-bit half-precision floating-point number array having a specified length.

var arr = new Float16Array( 5 );
// returns <Float16Array>

var len = arr.length;
// returns 5

Float16Array( typedarray )

Creates a 16-bit half-precision floating-point number array from a typed array.

var Float64Array = require( '@stdlib/array-float64' );

var buf = new Float64Array( [ 0.5, 0.5, 0.5 ] );
// returns <Float64Array>

var arr = new Float16Array( buf );
// returns <Float16Array>

var len = arr.length;
// returns 3

Float16Array( obj )

Creates a 16-bit half-precision floating-point number array from an array-like object or iterable.

var arr = new Float16Array( [ 0.5, 0.5, 0.5 ] );
// returns <Float16Array>

var len = arr.length;
// returns 3

Float16Array( buffer[, byteOffset[, length]] )

Returns a 16-bit half-precision floating-point number array view of an ArrayBuffer.

var ArrayBuffer = require( '@stdlib/array-buffer' );
var buf = new ArrayBuffer( 8 );

var arr = new Float16Array( buf );
// returns <Float16Array>

var len = arr.length;
// returns 4

arr = new Float16Array( buf, 2 );
// returns <Float16Array>

len = arr.length;
// returns 3

arr = new Float16Array( buf, 2, 2 );
// returns <Float16Array>

len = arr.length;
// returns 2

Properties

Float16Array.BYTES_PER_ELEMENT

Static property returning the size (in bytes) of each array element.

var nbytes = Float16Array.BYTES_PER_ELEMENT;
// returns 2

Float16Array.name

Static property returning the constructor name.

var str = Float16Array.name;
// returns 'Float16Array'

Float16Array.prototype.buffer

Pointer to the underlying data buffer.

var arr = new Float16Array( 5 );
// returns <Float16Array>

var buf = arr.buffer;
// returns <ArrayBuffer>

Float16Array.prototype.byteLength

Size (in bytes) of the array.

var arr = new Float16Array( 5 );
// returns <Float16Array>

var byteLength = arr.byteLength;
// returns 10

Float16Array.prototype.byteOffset

Offset (in bytes) of the array from the start of its underlying ArrayBuffer.

var ArrayBuffer = require( '@stdlib/array-buffer' );

var arr = new Float16Array( 5 );
// returns <Float16Array>

var offset = arr.byteOffset;
// returns 0

var buf = new ArrayBuffer( 20 );
arr = new Float16Array( buf, 10 );
// returns <Float16Array>

offset = arr.byteOffset;
// returns 10

Float16Array.prototype.BYTES_PER_ELEMENT

Size (in bytes) of each array element.

var arr = new Float16Array( 5 );
// returns <Float16Array>

var nbytes = arr.BYTES_PER_ELEMENT;
// returns 2

Float16Array.prototype.length

Number of array elements.

var arr = new Float16Array( 5 );
// returns <Float16Array>

var len = arr.length;
// returns 5

Methods

Float16Array.from( src[, clbk[, thisArg]] )

Creates a new 16-bit half-precision floating-point number array from an array-like object or an iterable.

var arr = Float16Array.from( [ 1.0, 2.0 ] );
// returns <Float16Array>

var v = arr[ 0 ];
// returns 1.0

v = arr[ 1 ];
// returns 2.0

var len = arr.length;
// returns 2

To invoke a function for each src value, provide a callback function.

var arr = Float16Array.from( [ 1.0, 2.0 ], clbk );
// returns <Float16Array>

var v = arr[ 0 ];
// returns 2.0

v = arr[ 1 ];
// returns 4.0

var len = arr.length;
// returns 2

function clbk( v ) {
    return v * 2.0;
}

A callback function is provided two arguments:

  • value: source value.
  • index: source index.

To set the callback execution context, provide a thisArg.

function map( v ) {
    this.count += 1;
    return v * 2.0;
}

var ctx = {
    'count': 0
};

var arr = Float16Array.from( [ 1.0, 2.0 ], map, ctx );
// returns <Float16Array>

var v = arr[ 0 ];
// returns 2.0

v = arr[ 1 ];
// returns 4.0

var n = ctx.count;
// returns 2

Float16Array.of( element0[, element1[, ...elementN]] )

Creates a new 16-bit half-precision floating-point number array from a variable number of arguments.

var arr = Float16Array.of( 1.0, 2.0 );
// returns <Float16Array>

var v = arr[ 0 ];
// returns 1.0

v = arr[ 1 ];
// returns 2.0

Float16Array.prototype.at( i )

Returns an array element located at integer position (index) i, with support for both nonnegative and negative integer positions.

var arr = new Float16Array( [ 10.0, 20.0, 30.0 ] );

var v = arr.at( 0 );
// returns 10.0

v = arr.at( -1 );
// returns 30.0

If provided an out-of-bounds index, the method returns undefined.

var arr = new Float16Array( 10 );

var v = arr.at( 100 );
// returns undefined

v = arr.at( -100 );
// returns undefined

Float16Array.prototype.copyWithin( target, start[, end] )

Copies a sequence of elements within an array starting at start and ending at end (non-inclusive) to the position starting at target.

var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );

// Copy the last two elements to the first two elements:
arr.copyWithin( 0, 3 );

var v = arr[ 0 ];
// returns 4.0

v = arr[ 1 ];
// returns 5.0

v = arr[ 2 ];
// returns 3.0

By default, end equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an end argument.

var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );

// Copy the first two elements to the last two elements:
arr.copyWithin( 3, 0, 2 );

var v = arr[ 3 ];
// returns 1.0

v = arr[ 4 ];
// returns 2.0

v = arr[ 0 ];
// returns 1.0

When a target, start, and/or end index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example:

var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );

// Copy the first two elements to the last two elements:
arr.copyWithin( -2, -5, -3 );

var v = arr[ 3 ];
// returns 1.0

v = arr[ 4 ];
// returns 2.0

v = arr[ 0 ];
// returns 1.0

Float16Array.prototype.entries()

Returns an iterator for iterating over array key-value pairs.

var arr = new Float16Array( [ 1.0, 2.0 ] );

// Create an iterator:
var it = arr.entries();

// Iterate over key-value pairs...
var v = it.next().value;
// returns [ 0, 1.0 ]

v = it.next().value;
// returns [ 1, 2.0 ]

var bool = it.next().done;
// returns true

The returned iterator protocol-compliant object has the following properties:

  • next: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a value property and a done property having a boolean value indicating whether the iterator is finished.
  • return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.

Float16Array.prototype.every( predicate[, thisArg] )

Returns a boolean indicating whether all elements pass a test.

function predicate( v ) {
    return ( v < 0.0 );
}

var arr = new Float16Array( [ 1.0, 2.0 ] );

var bool = arr.every( predicate );
// returns false

arr = new Float16Array( [ -1.0, -2.0 ] );

bool = arr.every( predicate );
// returns true

A predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the callback execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 0.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0 ] );

var bool = arr.every( predicate, ctx );
// returns true

var n = ctx.count;
// returns 2

Float16Array.prototype.fill( value[, start[, end]] )

Returns a modified typed array filled with a fill value.

var arr = new Float16Array( 2 );

// Set all array elements to the same value:
arr.fill( 2.0 );

var v = arr[ 0 ];
// returns 2.0

v = arr[ 1 ];
// returns 2.0

// Set all array elements starting from the first index to the same value:
arr.fill( 3.0, 1 );

v = arr[ 0 ];
// returns 2.0

v = arr[ 1 ];
// returns 3.0

// Set all array elements, except the last element, to the same value:
arr.fill( 4.0, 0, arr.length-1 );

v = arr[ 0 ];
// returns 4.0

v = arr[ 1 ];
// returns 3.0

When a start and/or end index is negative, the respective index is determined relative to the last array element.

var arr = new Float16Array( 2 );

// Set all array elements, except the last element, to the same value:
arr.fill( 2.0, -arr.length, -1 );

var v = arr[ 0 ];
// returns 2.0

v = arr[ 1 ];
// returns 0.0

Float16Array.prototype.filter( predicate[, thisArg] )

Returns a new array containing the elements of an array which pass a test implemented by a predicate function.

function predicate( v ) {
    return ( v > 1.0 );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.filter( predicate );
// returns <Float16Array>

var v = out[ 0 ];
// returns 2.0

v = out[ 1 ];
// returns 3.0

The predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 1.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 2.0, 3.0 ] );
var out = arr.filter( predicate, ctx );
// returns <Float16Array>

var v = out[ 0 ];
// returns 2.0

v = out[ 1 ];
// returns 3.0

var n = ctx.count;
// returns 2

Float16Array.prototype.find( predicate[, thisArg] )

Returns the first element in an array for which a predicate function returns a truthy value.

function predicate( v ) {
    return ( v > 1.0 );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.find( predicate );
// returns 2.0

A predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the callback execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 1.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.find( predicate, ctx );
// returns 2.0

var n = ctx.count;
// returns 2

Float16Array.prototype.findIndex( predicate[, thisArg] )

Returns the index of the first element in an array for which a predicate function returns a truthy value.

function predicate( v ) {
    return ( v > 2.0 );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var idx = arr.findIndex( predicate );
// returns 2

The predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 3.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var idx = arr.findIndex( predicate, ctx );
// returns -1

var n = ctx.count;
// returns 3

Float16Array.prototype.findLast( predicate[, thisArg] )

Returns the last element in an array for which a predicate function returns a truthy value.

function predicate( v ) {
    return v > 2.0;
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.findLast( predicate );
// returns 3.0

The predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 2.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.findLast( predicate, ctx );
// returns 3.0

var count = ctx.count;
// returns 1

Float16Array.prototype.findLastIndex( predicate[, thisArg] )

Returns the index of the last element in an array for which a predicate function returns a truthy value.

function predicate( v ) {
    return v > 2.0;
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.findLastIndex( predicate );
// returns 2

The predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 2.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.findLastIndex( predicate, ctx );
// returns 2

var count = ctx.count;
// returns 1

Float16Array.prototype.forEach( callbackFn[, thisArg] )

Invokes a callback for each array element.

function log( v, i ) {
    console.log( '%s: %s', i, v.toString() );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

arr.forEach( log );
/* =>
    0: 1
    1: 2
    2: 3
*/

The invoked function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function log( v, i ) {
    this.count += 1;
    console.log( '%s: %s', i, v.toString() );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

arr.forEach( log, ctx );
/* =>
    0: 1
    1: 2
    2: 3
*/

var count = ctx.count;
// returns 3

Float16Array.prototype.includes( searchElement[, fromIndex] )

Returns a boolean indicating whether an array includes a provided value.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var bool = arr.includes( 3.0 );
// returns true

bool = arr.includes( 0.0 );
// returns false

Float16Array.prototype.indexOf( searchElement[, fromIndex] )

Returns the first index at which a given element can be found.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var idx = arr.indexOf( 2.0 );
// returns 1

idx = arr.indexOf( 3.0 );
// returns 2

If searchElement is not present in the array, the method returns -1.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var idx = arr.indexOf( 4.0 );
// returns -1

Float16Array.prototype.join( [separator] )

Returns a new string by concatenating all array elements.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var str = arr.join();
// returns '1,2,3'

By default, the method separates serialized array elements with a comma. To use an alternative separator, provide a separator string.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var str = arr.join( '|' );
// returns '1|2|3'

Float16Array.prototype.keys()

Returns an iterator for iterating over each index key in a typed array.

var arr = new Float16Array( [ 1.0, 2.0 ] );

var it = arr.keys();

var v = it.next().value;
// returns 0

v = it.next().value;
// returns 1

var bool = it.next().done;
// returns true

The returned iterator protocol-compliant object has the following properties:

  • next: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a value property and a done property having a boolean value indicating whether the iterator is finished.
  • return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.

Float16Array.prototype.lastIndexOf( searchElement[, fromIndex] )

Returns the last index at which a given element can be found.

var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );

var idx = arr.lastIndexOf( 3.0 );
// returns 2

idx = arr.lastIndexOf( 2.0 );
// returns 4

idx = arr.lastIndexOf( 2.0, 2 );
// returns 1

If searchElement is not present in the array, the method returns -1.

var arr = new Float16Array( [ 1.0, 2.0 ] );

var idx = arr.lastIndexOf( 3.0 );
// returns -1

idx = arr.lastIndexOf( 2.0, 0 );
// returns -1

Float16Array.prototype.map( callbackFn[, thisArg] )

Returns a new array with each element being the result of a provided callback function.

function scale( v ) {
    return v * 2.0;
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.map( scale );
// returns <Float16Array>

var v = out[ 0 ];
// returns 2.0

v = out[ 1 ];
// returns 4.0

v = out[ 2 ];
// returns 6.0

The callback function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function scale( v ) {
    this.count += 1;
    return v * 2.0;
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.map( scale, ctx );
// returns <Float16Array>

var v = out[ 0 ];
// returns 2.0

v = out[ 1 ];
// returns 4.0

v = out[ 2 ];
// returns 6.0

var count = ctx.count;
// returns 3

Float16Array.prototype.reduce( reducerFn[, initialValue] )

Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.

function fcn( acc, v ) {
    return acc + ( v * v );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.reduce( fcn );
// returns 14.0

The reducer function is provided four arguments:

  • acc: accumulated result.
  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

By default, the function initializes the accumulated result to the first element in the array and passes the second array element as value during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as value during the first invocation of the provided callback, provide an initialValue argument.

function fcn( acc, v ) {
    return acc + ( v * v );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.reduce( fcn, 0.0 );
// returns 14.0

Float16Array.prototype.reduceRight( reducerFn[, initialValue] )

Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion.

function fcn( acc, v ) {
    return acc + ( v * v );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.reduceRight( fcn );
// returns 8.0

The reducer function is provided four arguments:

  • acc: accumulated result.
  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

By default, the function initializes the accumulated result to the last element in the array and passes the second-last array element as value during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last array element as value during the first invocation of the provided callback, provide an initialValue argument.

function fcn( acc, v ) {
    return acc + ( v * v );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var v = arr.reduceRight( fcn, 0.0 );
// returns 14.0

Float16Array.prototype.reverse()

Reverses an array in-place.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

// Reverse the array:
arr.reverse();

var v = arr[ 0 ];
// returns 3.0

v = arr[ 1 ];
// returns 2.0

v = arr[ 2 ];
// returns 1.0

Float16Array.prototype.set( arr[, offset] )

Sets one or more array elements.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );
// returns <Float16Array>

arr.set( [ 4.0, 5.0 ] );

var v = arr[ 0 ];
// returns 4.0

v = arr[ 1 ];
// returns 5.0

v = arr[ 2 ];
// returns 3.0

By default, the method sets array elements starting at position (index) i = 0. To set elements starting elsewhere in the array, provide an index argument i.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );
// returns <Float16Array>

// Set the last two array elements:
arr.set( [ 4.0, 5.0 ], 1 );

var v = arr[ 1 ];
// returns 4.0

v = arr[ 2 ];
// returns 5.0

A few notes:

  • If i is out-of-bounds, the method throws an error.
  • If a target array cannot accommodate all values (i.e., the length of source array plus i exceeds the target array length), the method throws an error.
  • If provided a typed array which shares an ArrayBuffer with the target array, the method will intelligently copy the source range to the destination range.

Float16Array.prototype.slice( [begin[, end]] )

Copies a portion of a typed array to a new typed array.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.slice();

var len = out.length;
// returns 3

var v = out[ 0 ];
// returns 1.0

v = out[ 1 ];
// returns 2.0

v = out[ 2 ];
// returns 3.0

By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a start index (inclusive).

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.slice( 1 );

var len = out.length;
// returns 2

var v = out[ 0 ];
// returns 2.0

v = out[ 1 ];
// returns 3.0

By default, the method returns a typed array which includes all array elements after start. To limit the number of array elements after start, provide an end index (exclusive).

var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] );

var out = arr.slice( 1, -1 );

var len = out.length;
// returns 2

var v = out[ 0 ];
// returns 2.0

v = out[ 1 ];
// returns 3.0

Float16Array.prototype.some( predicate[, thisArg] )

Returns a boolean indicating whether at least one element passes a test.

function predicate( v ) {
    return ( v > 2.0 );
}

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var bool = arr.some( predicate );
// returns true

The predicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide a thisArg.

function predicate( v ) {
    this.count += 1;
    return ( v > 2.0 );
}

var ctx = {
    'count': 0
};

var arr = new Float16Array( [ 1.0, 2.0 ] );

var bool = arr.some( predicate, ctx );
// returns false

var n = ctx.count;
// returns 2

Float16Array.prototype.sort( [compareFcn] )

Sorts an array in-place.

function compare( a, b ) {
    if ( a < b ) {
        return -1;
    }
    if ( a > b ) {
        return 1;
    }
    return 0;
}

var arr = new Float16Array( [ 2.0, 3.0, 1.0 ] );

arr.sort( compare );

var v = arr[ 0 ];
// returns 1.0

v = arr[ 1 ];
// returns 2.0

v = arr[ 2 ];
// returns 3.0

The compareFcn determines the order of the elements. The function is called with the following arguments:

  • a: the first element for comparison.
  • b: the second element for comparison.

The function should return a number where:

  • a negative value indicates that a should come before b.
  • a positive value indicates that a should come after b.
  • zero or NaN indicates that a and b are considered equal.

Float16Array.prototype.subarray( [begin[, end]] )

Creates a new typed array view over the same underlying ArrayBuffer and with the same underlying data type as the host array.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var subarr = arr.subarray();
// returns <Float16Array>

var v = subarr[ 0 ];
// returns 1.0

v = subarr[ 1 ];
// returns 2.0

v = subarr[ 2 ];
// returns 3.0

var len = subarr.length;
// returns 3

By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a begin index (inclusive).

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var subarr = arr.subarray( 1 );
// returns <Float16Array>

var v = subarr[ 0 ];
// returns 2.0

v = subarr[ 1 ];
// returns 3.0

var len = subarr.length;
// returns 2

By default, the method creates a typed array view which includes all array elements after begin. To limit the number of array elements after begin, provide an end index (exclusive).

var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] );

var subarr = arr.subarray( 1, -1 );
// returns <Float16Array>

var v = subarr[ 0 ];
// returns 2.0

v = subarr[ 1 ];
// returns 3.0

var len = subarr.length;
// returns 2

Float16Array.prototype.toLocaleString( [locales[, options]] )

Serializes an array as a locale-specific string.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var str = arr.toLocaleString();
// returns '1,2,3'

The method supports the following arguments:

  • locales: a string with a BCP 47 language tag or an array of such strings.
  • options: configuration properties.

Float16Array.prototype.toReversed()

Returns a new typed array containing the elements in reversed order.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.toReversed();
// returns <Float16Array>

var v = out[ 0 ];
// returns 3.0

v = out[ 1 ];
// returns 2.0

v = out[ 2 ];
// returns 1.0

Float16Array.prototype.toSorted( [compareFcn] )

Returns a new typed array containing the elements in sorted order.

function compare( a, b ) {
    if ( a < b ) {
        return -1;
    }
    if ( a > b ) {
        return 1;
    }
    return 0;
}

var arr = new Float16Array( [ 2.0, 3.0, 1.0 ] );

var out = arr.toSorted( compare );
// returns <Float16Array>

var v = out[ 0 ];
// returns 1.0

v = out[ 1 ];
// returns 2.0

v = out[ 2 ];
// returns 3.0

The compareFcn determines the order of the elements. The function is called with the following arguments:

  • a: the first element for comparison.
  • b: the second element for comparison.

The function should return a number where:

  • a negative value indicates that a should come before b.
  • a positive value indicates that a should come after b.
  • zero or NaN indicates that a and b are considered equal.

Float16Array.prototype.toString()

Serializes an array as a string.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var str = arr.toString();
// returns '1,2,3'

Float16Array.prototype.values()

Returns an iterator for iterating over each value in a typed array.

var arr = new Float16Array( [ 1.0, 2.0 ] );

var it = arr.values();

var v = it.next().value;
// returns 1.0

v = it.next().value;
// returns 2.0

var bool = it.next().done;
// returns true

The returned iterator protocol-compliant object has the following properties:

  • next: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a value property and a done property having a boolean value indicating whether the iterator is finished.
  • return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.

Float16Array.prototype.with( index, value )

Returns a new typed array with the element at a provided index replaced with a provided value.

var arr = new Float16Array( [ 1.0, 2.0, 3.0 ] );

var out = arr.with( 0, 0.0 );
// returns <Float16Array>

var v = out[ 0 ];
// returns 0.0

Examples

var Uint16Array = require( '@stdlib/array-uint16' );
var logEach = require( '@stdlib/console-log-each' );
var Float16Array = require( '@stdlib/array-float16' );

// Create a half-precision floating-point number array by specifying a length:
var out = new Float16Array( 3 );
logEach( '%s', out );

// Create a half-precision floating-point number array from an array of numbers:
var arr = [ 1.05, 2.05, 3.05 ];
out = new Float16Array( arr );
logEach( '%s', out );

// Create a half-precision floating-point number array from an array buffer:
arr = new Uint16Array( [ 1000, 2000, 3000, 4000 ] );
out = new Float16Array( arr.buffer );
logEach( '%s', out );

// Create a half-precision floating-point number array from an array buffer view:
arr = new Uint16Array( [ 1000, 2000, 3000, 4000 ] );
out = new Float16Array( arr.buffer, 2, 2 );
logEach( '%s', out );

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2026. The Stdlib Authors.