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!
16-bit half-precision floating-point number array.
npm install @stdlib/array-float16Alternatively,
- To load the package in a website via a
scripttag without installation and bundlers, use the ES Module available on theesmbranch (see README). - If you are using Deno, visit the
denobranch (see README for usage intructions). - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umdbranch (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.
varFloat16Array=require('@stdlib/array-float16');Creates a 16-bit half-precision floating-point number array.
vararr=newFloat16Array();// returns <Float16Array>Creates a 16-bit half-precision floating-point number array having a specified length.
vararr=newFloat16Array(5);// returns <Float16Array>varlen=arr.length;// returns 5Creates a 16-bit half-precision floating-point number array from a typed array.
varFloat64Array=require('@stdlib/array-float64');varbuf=newFloat64Array([0.5,0.5,0.5]);// returns <Float64Array>vararr=newFloat16Array(buf);// returns <Float16Array>varlen=arr.length;// returns 3Creates a 16-bit half-precision floating-point number array from an array-like object or iterable.
vararr=newFloat16Array([0.5,0.5,0.5]);// returns <Float16Array>varlen=arr.length;// returns 3Returns a 16-bit half-precision floating-point number array view of an ArrayBuffer.
varArrayBuffer=require('@stdlib/array-buffer');varbuf=newArrayBuffer(8);vararr=newFloat16Array(buf);// returns <Float16Array>varlen=arr.length;// returns 4arr=newFloat16Array(buf,2);// returns <Float16Array>len=arr.length;// returns 3arr=newFloat16Array(buf,2,2);// returns <Float16Array>len=arr.length;// returns 2Static property returning the size (in bytes) of each array element.
varnbytes=Float16Array.BYTES_PER_ELEMENT;// returns 2Static property returning the constructor name.
varstr=Float16Array.name;// returns 'Float16Array'Pointer to the underlying data buffer.
vararr=newFloat16Array(5);// returns <Float16Array>varbuf=arr.buffer;// returns <ArrayBuffer>Size (in bytes) of the array.
vararr=newFloat16Array(5);// returns <Float16Array>varbyteLength=arr.byteLength;// returns 10Offset (in bytes) of the array from the start of its underlying ArrayBuffer.
varArrayBuffer=require('@stdlib/array-buffer');vararr=newFloat16Array(5);// returns <Float16Array>varoffset=arr.byteOffset;// returns 0varbuf=newArrayBuffer(20);arr=newFloat16Array(buf,10);// returns <Float16Array>offset=arr.byteOffset;// returns 10Size (in bytes) of each array element.
vararr=newFloat16Array(5);// returns <Float16Array>varnbytes=arr.BYTES_PER_ELEMENT;// returns 2Number of array elements.
vararr=newFloat16Array(5);// returns <Float16Array>varlen=arr.length;// returns 5Creates a new 16-bit half-precision floating-point number array from an array-like object or an iterable.
vararr=Float16Array.from([1.0,2.0]);// returns <Float16Array>varv=arr[0];// returns 1.0v=arr[1];// returns 2.0varlen=arr.length;// returns 2To invoke a function for each src value, provide a callback function.
vararr=Float16Array.from([1.0,2.0],clbk);// returns <Float16Array>varv=arr[0];// returns 2.0v=arr[1];// returns 4.0varlen=arr.length;// returns 2functionclbk(v){returnv*2.0;}A callback function is provided two arguments:
- value: source value.
- index: source index.
To set the callback execution context, provide a thisArg.
functionmap(v){this.count+=1;returnv*2.0;}varctx={'count': 0};vararr=Float16Array.from([1.0,2.0],map,ctx);// returns <Float16Array>varv=arr[0];// returns 2.0v=arr[1];// returns 4.0varn=ctx.count;// returns 2Creates a new 16-bit half-precision floating-point number array from a variable number of arguments.
vararr=Float16Array.of(1.0,2.0);// returns <Float16Array>varv=arr[0];// returns 1.0v=arr[1];// returns 2.0Returns an array element located at integer position (index) i, with support for both nonnegative and negative integer positions.
vararr=newFloat16Array([10.0,20.0,30.0]);varv=arr.at(0);// returns 10.0v=arr.at(-1);// returns 30.0If provided an out-of-bounds index, the method returns undefined.
vararr=newFloat16Array(10);varv=arr.at(100);// returns undefinedv=arr.at(-100);// returns undefinedCopies a sequence of elements within an array starting at start and ending at end (non-inclusive) to the position starting at target.
vararr=newFloat16Array([1.0,2.0,3.0,4.0,5.0]);// Copy the last two elements to the first two elements:arr.copyWithin(0,3);varv=arr[0];// returns 4.0v=arr[1];// returns 5.0v=arr[2];// returns 3.0By 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.
vararr=newFloat16Array([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);varv=arr[3];// returns 1.0v=arr[4];// returns 2.0v=arr[0];// returns 1.0When 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:
vararr=newFloat16Array([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);varv=arr[3];// returns 1.0v=arr[4];// returns 2.0v=arr[0];// returns 1.0Returns an iterator for iterating over array key-value pairs.
vararr=newFloat16Array([1.0,2.0]);// Create an iterator:varit=arr.entries();// Iterate over key-value pairs...varv=it.next().value;// returns [ 0, 1.0 ]v=it.next().value;// returns [ 1, 2.0 ]varbool=it.next().done;// returns trueThe 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
valueproperty and adoneproperty having abooleanvalue indicating whether the iterator is finished. - return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
Returns a boolean indicating whether all elements pass a test.
functionpredicate(v){return(v<0.0);}vararr=newFloat16Array([1.0,2.0]);varbool=arr.every(predicate);// returns falsearr=newFloat16Array([-1.0,-2.0]);bool=arr.every(predicate);// returns trueA 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.
functionpredicate(v){this.count+=1;return(v>0.0);}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0]);varbool=arr.every(predicate,ctx);// returns truevarn=ctx.count;// returns 2Returns a modified typed array filled with a fill value.
vararr=newFloat16Array(2);// Set all array elements to the same value:arr.fill(2.0);varv=arr[0];// returns 2.0v=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.0v=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.0v=arr[1];// returns 3.0When a start and/or end index is negative, the respective index is determined relative to the last array element.
vararr=newFloat16Array(2);// Set all array elements, except the last element, to the same value:arr.fill(2.0,-arr.length,-1);varv=arr[0];// returns 2.0v=arr[1];// returns 0.0Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
functionpredicate(v){return(v>1.0);}vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.filter(predicate);// returns <Float16Array>varv=out[0];// returns 2.0v=out[1];// returns 3.0The 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.
functionpredicate(v){this.count+=1;return(v>1.0);}varctx={'count': 0};vararr=newFloat16Array([2.0,3.0]);varout=arr.filter(predicate,ctx);// returns <Float16Array>varv=out[0];// returns 2.0v=out[1];// returns 3.0varn=ctx.count;// returns 2Returns the first element in an array for which a predicate function returns a truthy value.
functionpredicate(v){return(v>1.0);}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.find(predicate);// returns 2.0A 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.
functionpredicate(v){this.count+=1;return(v>1.0);}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.find(predicate,ctx);// returns 2.0varn=ctx.count;// returns 2Returns the index of the first element in an array for which a predicate function returns a truthy value.
functionpredicate(v){return(v>2.0);}vararr=newFloat16Array([1.0,2.0,3.0]);varidx=arr.findIndex(predicate);// returns 2The 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.
functionpredicate(v){this.count+=1;return(v>3.0);}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0,3.0]);varidx=arr.findIndex(predicate,ctx);// returns -1varn=ctx.count;// returns 3Returns the last element in an array for which a predicate function returns a truthy value.
functionpredicate(v){returnv>2.0;}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.findLast(predicate);// returns 3.0The 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.
functionpredicate(v){this.count+=1;return(v>2.0);}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.findLast(predicate,ctx);// returns 3.0varcount=ctx.count;// returns 1Returns the index of the last element in an array for which a predicate function returns a truthy value.
functionpredicate(v){returnv>2.0;}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.findLastIndex(predicate);// returns 2The 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.
functionpredicate(v){this.count+=1;return(v>2.0);}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.findLastIndex(predicate,ctx);// returns 2varcount=ctx.count;// returns 1Invokes a callback for each array element.
functionlog(v,i){console.log('%s: %s',i,v.toString());}vararr=newFloat16Array([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.
functionlog(v,i){this.count+=1;console.log('%s: %s',i,v.toString());}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0,3.0]);arr.forEach(log,ctx);/* => 0: 1 1: 2 2: 3*/varcount=ctx.count;// returns 3Returns a boolean indicating whether an array includes a provided value.
vararr=newFloat16Array([1.0,2.0,3.0]);varbool=arr.includes(3.0);// returns truebool=arr.includes(0.0);// returns falseReturns the first index at which a given element can be found.
vararr=newFloat16Array([1.0,2.0,3.0]);varidx=arr.indexOf(2.0);// returns 1idx=arr.indexOf(3.0);// returns 2If searchElement is not present in the array, the method returns -1.
vararr=newFloat16Array([1.0,2.0,3.0]);varidx=arr.indexOf(4.0);// returns -1Returns a new string by concatenating all array elements.
vararr=newFloat16Array([1.0,2.0,3.0]);varstr=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.
vararr=newFloat16Array([1.0,2.0,3.0]);varstr=arr.join('|');// returns '1|2|3'Returns an iterator for iterating over each index key in a typed array.
vararr=newFloat16Array([1.0,2.0]);varit=arr.keys();varv=it.next().value;// returns 0v=it.next().value;// returns 1varbool=it.next().done;// returns trueThe 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
valueproperty and adoneproperty having abooleanvalue indicating whether the iterator is finished. - return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
Returns the last index at which a given element can be found.
vararr=newFloat16Array([1.0,2.0,3.0,4.0,2.0]);varidx=arr.lastIndexOf(3.0);// returns 2idx=arr.lastIndexOf(2.0);// returns 4idx=arr.lastIndexOf(2.0,2);// returns 1If searchElement is not present in the array, the method returns -1.
vararr=newFloat16Array([1.0,2.0]);varidx=arr.lastIndexOf(3.0);// returns -1idx=arr.lastIndexOf(2.0,0);// returns -1Returns a new array with each element being the result of a provided callback function.
functionscale(v){returnv*2.0;}vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.map(scale);// returns <Float16Array>varv=out[0];// returns 2.0v=out[1];// returns 4.0v=out[2];// returns 6.0The 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.
functionscale(v){this.count+=1;returnv*2.0;}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.map(scale,ctx);// returns <Float16Array>varv=out[0];// returns 2.0v=out[1];// returns 4.0v=out[2];// returns 6.0varcount=ctx.count;// returns 3Applies 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.
functionfcn(acc,v){returnacc+(v*v);}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.reduce(fcn);// returns 14.0The 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.
functionfcn(acc,v){returnacc+(v*v);}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.reduce(fcn,0.0);// returns 14.0Applies 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.
functionfcn(acc,v){returnacc+(v*v);}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.reduceRight(fcn);// returns 8.0The 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.
functionfcn(acc,v){returnacc+(v*v);}vararr=newFloat16Array([1.0,2.0,3.0]);varv=arr.reduceRight(fcn,0.0);// returns 14.0Reverses an array in-place.
vararr=newFloat16Array([1.0,2.0,3.0]);// Reverse the array:arr.reverse();varv=arr[0];// returns 3.0v=arr[1];// returns 2.0v=arr[2];// returns 1.0Sets one or more array elements.
vararr=newFloat16Array([1.0,2.0,3.0]);// returns <Float16Array>arr.set([4.0,5.0]);varv=arr[0];// returns 4.0v=arr[1];// returns 5.0v=arr[2];// returns 3.0By 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.
vararr=newFloat16Array([1.0,2.0,3.0]);// returns <Float16Array>// Set the last two array elements:arr.set([4.0,5.0],1);varv=arr[1];// returns 4.0v=arr[2];// returns 5.0A few notes:
- If
iis out-of-bounds, the method throws an error. - If a target array cannot accommodate all values (i.e., the length of source array plus
iexceeds the target array length), the method throws an error. - If provided a typed array which shares an
ArrayBufferwith the target array, the method will intelligently copy the source range to the destination range.
Copies a portion of a typed array to a new typed array.
vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.slice();varlen=out.length;// returns 3varv=out[0];// returns 1.0v=out[1];// returns 2.0v=out[2];// returns 3.0By 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).
vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.slice(1);varlen=out.length;// returns 2varv=out[0];// returns 2.0v=out[1];// returns 3.0By 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).
vararr=newFloat16Array([1.0,2.0,3.0,4.0]);varout=arr.slice(1,-1);varlen=out.length;// returns 2varv=out[0];// returns 2.0v=out[1];// returns 3.0Returns a boolean indicating whether at least one element passes a test.
functionpredicate(v){return(v>2.0);}vararr=newFloat16Array([1.0,2.0,3.0]);varbool=arr.some(predicate);// returns trueThe 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.
functionpredicate(v){this.count+=1;return(v>2.0);}varctx={'count': 0};vararr=newFloat16Array([1.0,2.0]);varbool=arr.some(predicate,ctx);// returns falsevarn=ctx.count;// returns 2Sorts an array in-place.
functioncompare(a,b){if(a<b){return-1;}if(a>b){return1;}return0;}vararr=newFloat16Array([2.0,3.0,1.0]);arr.sort(compare);varv=arr[0];// returns 1.0v=arr[1];// returns 2.0v=arr[2];// returns 3.0The 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
ashould come beforeb. - a positive value indicates that
ashould come afterb. - zero or
NaNindicates thataandbare considered equal.
Creates a new typed array view over the same underlying ArrayBuffer and with the same underlying data type as the host array.
vararr=newFloat16Array([1.0,2.0,3.0]);varsubarr=arr.subarray();// returns <Float16Array>varv=subarr[0];// returns 1.0v=subarr[1];// returns 2.0v=subarr[2];// returns 3.0varlen=subarr.length;// returns 3By 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).
vararr=newFloat16Array([1.0,2.0,3.0]);varsubarr=arr.subarray(1);// returns <Float16Array>varv=subarr[0];// returns 2.0v=subarr[1];// returns 3.0varlen=subarr.length;// returns 2By 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).
vararr=newFloat16Array([1.0,2.0,3.0,4.0]);varsubarr=arr.subarray(1,-1);// returns <Float16Array>varv=subarr[0];// returns 2.0v=subarr[1];// returns 3.0varlen=subarr.length;// returns 2Serializes an array as a locale-specific string.
vararr=newFloat16Array([1.0,2.0,3.0]);varstr=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.
Returns a new typed array containing the elements in reversed order.
vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.toReversed();// returns <Float16Array>varv=out[0];// returns 3.0v=out[1];// returns 2.0v=out[2];// returns 1.0Returns a new typed array containing the elements in sorted order.
functioncompare(a,b){if(a<b){return-1;}if(a>b){return1;}return0;}vararr=newFloat16Array([2.0,3.0,1.0]);varout=arr.toSorted(compare);// returns <Float16Array>varv=out[0];// returns 1.0v=out[1];// returns 2.0v=out[2];// returns 3.0The 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
ashould come beforeb. - a positive value indicates that
ashould come afterb. - zero or
NaNindicates thataandbare considered equal.
Serializes an array as a string.
vararr=newFloat16Array([1.0,2.0,3.0]);varstr=arr.toString();// returns '1,2,3'Returns an iterator for iterating over each value in a typed array.
vararr=newFloat16Array([1.0,2.0]);varit=arr.values();varv=it.next().value;// returns 1.0v=it.next().value;// returns 2.0varbool=it.next().done;// returns trueThe 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
valueproperty and adoneproperty having abooleanvalue indicating whether the iterator is finished. - return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
Returns a new typed array with the element at a provided index replaced with a provided value.
vararr=newFloat16Array([1.0,2.0,3.0]);varout=arr.with(0,0.0);// returns <Float16Array>varv=out[0];// returns 0.0varUint16Array=require('@stdlib/array-uint16');varlogEach=require('@stdlib/console-log-each');varFloat16Array=require('@stdlib/array-float16');// Create a half-precision floating-point number array by specifying a length:varout=newFloat16Array(3);logEach('%s',out);// Create a half-precision floating-point number array from an array of numbers:vararr=[1.05,2.05,3.05];out=newFloat16Array(arr);logEach('%s',out);// Create a half-precision floating-point number array from an array buffer:arr=newUint16Array([1000,2000,3000,4000]);out=newFloat16Array(arr.buffer);logEach('%s',out);// Create a half-precision floating-point number array from an array buffer view:arr=newUint16Array([1000,2000,3000,4000]);out=newFloat16Array(arr.buffer,2,2);logEach('%s',out);@stdlib/array-buffer: ArrayBuffer.@stdlib/array-float32: Float32Array.@stdlib/array-float64: Float64Array.@stdlib/array-int16: Int16Array.@stdlib/array-int32: Int32Array.@stdlib/array-int8: Int8Array.@stdlib/array-uint16: Uint16Array.@stdlib/array-uint32: Uint32Array.@stdlib/array-uint8: Uint8Array.@stdlib/array-uint8c: Uint8ClampedArray.
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.
See LICENSE.
Copyright © 2016-2026. The Stdlib Authors.