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!
Float32Array
Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in the platform byte order.
Installation
npm install @stdlib/array-float32
Usage
var Float32Array = require( '@stdlib/array-float32' );
Float32Array()
A typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in the platform byte order.
var arr = new Float32Array();// returns <Float32Array>
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.
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 Float32Array( [ 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.0v = arr[ 1 ];// returns 5.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 Float32Array( [ 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.0v = arr[ 4 ];// returns 2.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 Float32Array( [ 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.0v = arr[ 4 ];// returns 2.0
Float32Array.prototype.entries()
Returns an iterator for iterating over array key-value pairs.
var arr = new Float32Array( [ 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
Fills an array from a start index to an end index (non-inclusive) with a provided value.
var arr = new Float32Array( 2 );// Set all array elements to the same value:arr.fill( 2.0 );var v = 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.0
When a start and/or end index is negative, the respective index is determined relative to the last array element.
var arr = new Float32Array( 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.0v = arr[ 1 ];// returns 0.0
Applies a function against an accumulator and each element in an array and returns the accumulated result.
function fcn( acc, v ) { return acc + ( v*v );}var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] );var v = arr.reduce( fcn );// returns 12.0
If not provided an initial value, the method invokes a provided function with the first array element as the first argument and the second array element as the second argument.
If provided an initial value, the method invokes a provided function with the initial value as the first argument and the first array element as the second argument.
function fcn( acc, v ) { return acc + ( v*v );}var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] );var v = arr.reduce( fcn, 0.0 );// returns 14.0
Applies a function against an accumulator and each element in an array and returns the accumulated result, iterating from right to left.
function fcn( acc, v ) { return acc + ( v*v );}var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] );var v = arr.reduceRight( fcn );// returns 8.0
If not provided an initial value, the method invokes a provided function with the last array element as the first argument and the second-to-last array element as the second argument.
If provided an initial value, the method invokes a provided function with the initial value as the first argument and the last array element as the second argument.
function fcn( acc, v ) { return acc + ( v*v );}var arr = new Float32Array( [ 2.0, 1.0, 3.0 ] );var v = arr.reduce( fcn, 0.0 );// returns 14.0
A callback is provided four arguments:
acc: accumulated result.
value: array element.
index: array index.
arr: array on which the method is invoked.
Float32Array.prototype.reverse()
Reverses an array in-place (thus mutating the array on which the method is invoked).
var arr = new Float32Array( [ 2.0, 0.0, 3.0 ] );// Reverse the array:arr.reverse();var v = arr[ 0 ];// returns 3.0v = arr[ 1 ];// returns 0.0v = arr[ 2 ];// returns 2.0
Float32Array.prototype.set( arr[, offset] )
Sets array elements.
var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] );// returns <Float32Array>[ 1.0, 2.0, 3.0 ]// Set the first two array elements:arr.set( [ 4.0, 5.0 ] );var v = arr[ 0 ];// returns 4.0v = arr[ 1 ];// returns 5.0
By default, the method starts writing values at the first array index. To specify an alternative index, provide an index offset.
var arr = new Float32Array( [ 1.0, 2.0, 3.0 ] );// returns <Float32Array>[ 1.0, 2.0, 3.0 ]// Set the last two array elements:arr.set( [ 4.0, 5.0 ], 1 );var v = arr[ 1 ];// returns 4.0v = arr[ 2 ];// returns 5.0
Float32Array.prototype.slice( [begin[, end]] )
Copies array elements to a new array with the same underlying data type as the host array.
By default, the method copies elements beginning with the first array element. To specify an alternative array index at which to begin copying, provide a begin index (inclusive).
var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] );var arr2 = arr1.slice( 1 );var len = arr2.length;// returns 2var v = arr2[ 0 ];// returns 2.0v = arr2[ 1 ];// returns 3.0
By default, the method copies all array elements after begin. To specify an alternative array index at which to end copying, provide an end index (exclusive).
var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] );var arr2 = arr1.slice( 0, 2 );var len = arr2.length;// returns 2var v = arr2[ 0 ];// returns 1.0v = arr2[ 1 ];// returns 2.0
When a begin and/or end index is negative, the respective index is determined relative to the last array element.
var arr1 = new Float32Array( [ 1.0, 2.0, 3.0 ] );var arr2 = arr1.slice( -arr1.length, -1 );var len = arr2.length;// returns 2var v = arr2[ 0 ];// returns 1.0v = arr2[ 1 ];// returns 2.0
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).
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).