Float32Array having a specified byte order.
npm install @stdlib/array-fixed-endian-float32We 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!
About stdlib...
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
> Typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in a specified byte order.
In contrast to the [Float32Array][@stdlib/array/float32] typed array constructor which stores values according to the host platform byte order, the Float32ArrayFE constructor allows enforcing a specific byte order. Such enforcement can be particularly advantageous when working with memory buffers which do not necessarily follow host platform byte order, such as [WebAssembly memory][@stdlib/wasm/memory].
``bash`
npm install @stdlib/array-fixed-endian-float32
`javascript`
var Float32ArrayFE = require( '@stdlib/array-fixed-endian-float32' );
#### Float32ArrayFE( endianness )
A typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in a specified byte order.
`javascript`
var arr = new Float32ArrayFE( 'little-endian' );
// returns
#### Float32ArrayFE( endianness, length )
Returns a typed array having a specified length and byte order.
`javascript`
var arr = new Float32ArrayFE( 'little-endian', 5 );
// returns
#### Float32ArrayFE( endianness, typedarray )
Creates a typed array from another typed array.
`javascript
var Float32Array = require( '@stdlib/array-float32' );
var arr1 = new Float32Array( [ 0.5, 0.5, 0.5 ] );
var arr2 = new Float32ArrayFE( 'little-endian', arr1 );
// returns
var v = arr2.get( 0 );
// returns 0.5
`
#### Float32ArrayFE( endianness, obj )
Creates a typed array from an array-like object or iterable.
`javascript
var arr = new Float32ArrayFE( 'little-endian', [ 0.5, 0.5, 0.5 ] );
// returns
var v = arr.get( 0 );
// returns 0.5
`
#### Float32ArrayFE( endianness, buffer\[, byteOffset\[, length]] )
Returns a typed array view of an [ArrayBuffer][@stdlib/array/buffer].
`javascript
var ArrayBuffer = require( '@stdlib/array-buffer' );
var buf = new ArrayBuffer( 32 );
var arr = new Float32ArrayFE( 'little-endian', buf, 0, 4 );
// returns
`
*
#### Float32ArrayFE.BYTES_PER_ELEMENT
Number of bytes per view element.
`javascript`
var nbytes = Float32ArrayFE.BYTES_PER_ELEMENT;
// returns 4
#### Float32ArrayFE.name
Typed array constructor name.
`javascript`
var str = Float32ArrayFE.name;
// returns 'Float32ArrayFE'
#### Float32ArrayFE.prototype.buffer
Read-only property which returns the [ArrayBuffer][@stdlib/array/buffer] referenced by the typed array.
`javascript`
var arr = new Float32ArrayFE( 'little-endian', 5 );
var buf = arr.buffer;
// returns
#### Float32ArrayFE.prototype.byteLength
Read-only property which returns the length (in bytes) of the typed array.
`javascript`
var arr = new Float32ArrayFE( 'little-endian', 5 );
var byteLength = arr.byteLength;
// returns 20
#### Float32ArrayFE.prototype.byteOffset
Read-only property which returns the offset (in bytes) of the typed array from the start of its [ArrayBuffer][@stdlib/array/buffer].
`javascript`
var arr = new Float32ArrayFE( 'little-endian', 5 );
var byteOffset = arr.byteOffset;
// returns 0
#### Float32ArrayFE.prototype.BYTES_PER_ELEMENT
Number of bytes per view element.
`javascript`
var arr = new Float32ArrayFE( 'little-endian', 5 );
var nbytes = arr.BYTES_PER_ELEMENT;
// returns 4
#### Float32ArrayFE.prototype.length
Read-only property which returns the number of view elements.
`javascript`
var arr = new Float32ArrayFE( 'little-endian', 5 );
var len = arr.length;
// returns 5
*
#### Float32ArrayFE.from( endianness, src\[, map\[, thisArg]] )
Creates a new typed array from an array-like object or an iterable.
`javascript
var arr = Float32ArrayFE.from( 'little-endian', [ 1.0, -1.0 ] );
// returns
var v = arr.get( 0 );
// returns 1.0
`
To invoke a function for each src value, provide a callback function.
`javascript
function mapFcn( v ) {
return v * 2.0;
}
var arr = Float32ArrayFE.from( 'little-endian', [ 1.0, -1.0 ], mapFcn );
// returns
var v = arr.get( 0 );
// returns 2.0
`
A callback function is provided two arguments:
- value: source value.
- index: source index.
To set the callback execution context, provide a thisArg.
`javascript
function mapFcn( v ) {
this.count += 1;
return v * 2.0;
}
var ctx = {
'count': 0
};
var arr = Float32ArrayFE.from( 'little-endian', [ 1.0, -1.0 ], mapFcn, ctx );
// returns
var v = arr.get( 0 );
// returns 2.0
var n = ctx.count;
// returns 2
`
#### Float32ArrayFE.of( endianness, element0\[, element1\[, ...elementN]] )
Creates a new typed array from a variable number of arguments.
`javascript
var arr = Float32ArrayFE.of( 'little-endian', 1.0, -1.0 );
// returns
var v = arr.get( 0 );
// returns 1.0
`
#### Float32ArrayFE.prototype.get( i )
Returns an array element located at a nonnegative integer position (index) i.
`javascript
var arr = new Float32ArrayFE( 'little-endian', 10 );
// Set the first element:
arr.set( 1.0, 0 );
// Get the first element:
var v = arr.get( 0 );
// returns 1.0
`
If provided an out-of-bounds index, the method returns undefined.
`javascript
var arr = new Float32ArrayFE( 'little-endian', 10 );
var v = arr.get( 100 );
// returns undefined
`
#### Float32ArrayFE.prototype.set( arr\[, offset] )
Sets array elements.
`javascript
var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
// returns
var v = arr.get( 0 );
// returns 1.0
v = arr.get( 1 );
// returns 2.0
// Set the first two array elements:
arr.set( [ 4.0, 5.0 ] );
v = arr.get( 0 );
// returns 4.0
v = arr.get( 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.
`javascript
var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
// returns
// Set the last two array elements:
arr.set( [ 4.0, 5.0 ], 1 );
var v = arr.get( 1 );
// returns 4.0
v = arr.get( 2 );
// returns 5.0
`
A few notes:
- If i is out-of-bounds, the method throws an error.i
- If a target array cannot accommodate all values (i.e., the length of source array plus exceeds the target array length), the method throws an error.ArrayBuffer
- If provided a typed array which shares an [][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.
#### Float32ArrayFE.prototype.toString()
Serializes an array as a string.
`javascript
var arr = new Float32ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
var str = arr.toString();
// returns '1,2,3'
`
*
- The constructor supports the following byte orders:
- little-endian: store values such that bytes are stored from least-to-most significant bytes. This is the dominant ordering for processor architectures and their associated memory. This is also the ordering for [WebAssembly memory][@stdlib/wasm/memory].
- big-endian: store values such that bytes are stored from most-to-least significant bytes. This is the dominant ordering in network protocols.
- While a Float32ArrayFE _strives_ to maintain (but does not guarantee) consistency with [typed arrays][@stdlib/array/typed], significant deviations from ECMAScript-defined [typed array][@stdlib/array/typed] behavior are as follows:
- The constructor does not require the new operator.X[i]
- Accessing array elements using bracket syntax (e.g., ) is not supported. Instead, one must use the .get() method.
*
`javascript
var Float32Array = require( '@stdlib/array-float32' );
var logEach = require( '@stdlib/console-log-each' );
var Float32ArrayFE = require( '@stdlib/array-fixed-endian-float32' );
// Create a typed array by specifying a length:
var out = new Float32ArrayFE( 'little-endian', 3 );
logEach( '%s', out );
// Create a typed array from an array:
var arr = [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ];
out = new Float32ArrayFE( 'big-endian', arr );
logEach( '%s', out );
// Create a typed array from an array buffer:
arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order
out = new Float32ArrayFE( 'little-endian', arr.buffer );
logEach( '%s', out );
// Create a typed array from an array buffer view:
arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] ); // host byte order
out = new Float32ArrayFE( 'big-endian', arr.buffer, 8, 2 );
logEach( '%s', out );
`
*
- [@stdlib/array-fixed-endian-float64][@stdlib/array/fixed-endian-float64]: Float64Array having a specified byte order.
- [@stdlib/array-float32][@stdlib/array/float32]: Float32Array.
*
This package is part of [stdlib][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][stdlib], see the main project [repository][stdlib].
#### Community
[![Chat][chat-image]][chat-url]
---
See [LICENSE][stdlib-license].
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-fixed-endian-float32.svg
[npm-url]: https://npmjs.org/package/@stdlib/array-fixed-endian-float32
[test-image]: https://github.com/stdlib-js/array-fixed-endian-float32/actions/workflows/test.yml/badge.svg?branch=v0.1.1
[test-url]: https://github.com/stdlib-js/array-fixed-endian-float32/actions/workflows/test.yml?query=branch:v0.1.1
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-fixed-endian-float32/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/array-fixed-endian-float32?branch=main
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
[chat-url]: https://stdlib.zulipchat.com
[stdlib]: https://github.com/stdlib-js/stdlib
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[deno-url]: https://github.com/stdlib-js/array-fixed-endian-float32/tree/deno
[deno-readme]: https://github.com/stdlib-js/array-fixed-endian-float32/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/array-fixed-endian-float32/tree/umd
[umd-readme]: https://github.com/stdlib-js/array-fixed-endian-float32/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/array-fixed-endian-float32/tree/esm
[esm-readme]: https://github.com/stdlib-js/array-fixed-endian-float32/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/array-fixed-endian-float32/blob/main/branches.md
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-fixed-endian-float32/main/LICENSE
[@stdlib/array/typed]: https://www.npmjs.com/package/@stdlib/array-typed
[@stdlib/array/buffer]: https://www.npmjs.com/package/@stdlib/array-buffer
[@stdlib/wasm/memory]: https://www.npmjs.com/package/@stdlib/wasm-memory
[@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array-float32
[@stdlib/array/fixed-endian-float64]: https://www.npmjs.com/package/@stdlib/array-fixed-endian-float64