Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Buffers and Encoding

Node.js buf.readDoubleBE() and floating-point methods

Reference for the Node.js Buffer floating-point methods readFloatBE, readDoubleLE, writeFloatLE and writeDoubleBE: IEEE 754 sizes, precision loss, NaN and errors.

Buffer can store and read IEEE 754 floating-point numbers in two sizes: 32-bit "float" (4 bytes) and 64-bit "double" (8 bytes). There are eight methods, formed from read or write, Float or Double, and BE or LE for the byte order: readFloatBE, readFloatLE, readDoubleBE, readDoubleLE, writeFloatBE, writeFloatLE, writeDoubleBE and writeDoubleLE.

A JavaScript number is already a double, so the double methods round-trip every value exactly. The float methods round to 32 bits when writing, which loses precision. Use floats only when a file format or protocol (sensor data, audio samples, graphics) demands four bytes.

NODE
const buf = Buffer.alloc(8);
buf.writeDoubleBE(Math.PI);
buf.readDoubleBE(0); // 3.141592653589793

Try it

Node.js Floats and doubles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Special values and errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
buf.readFloatBE(offset = 0)        // number; also readFloatLE
buf.readDoubleBE(offset = 0)       // number; also readDoubleLE
buf.writeFloatBE(value, offset = 0)   // returns offset + 4; also writeFloatLE
buf.writeDoubleBE(value, offset = 0)  // returns offset + 8; also writeDoubleLE
Name Description
value The number to store. It is converted to a number first; NaN is stored as a NaN.
offset Index of the first byte. An integer from 0 to buf.length - 4 (float) or buf.length - 8 (double).

Returns: the readers return a number; the writers return offset plus the number of bytes written (4 or 8).

Errors and edge cases:

  • ERR_OUT_OF_RANGE if the offset is negative, fractional, or too large for the size.
  • ERR_INVALID_ARG_TYPE if the offset is not a number when reading.
  • Values outside the float range are written as Infinity or -Infinity; values too small become 0 or a subnormal number. Neither case throws.
  • A non-numeric value such as "x" is written as NaN.

Usage notes

  • Float rounding is visible: 0.1 written as a float reads back as 0.10000000149011612. If exact decimal values matter, store integers (cents) or use doubles.
  • A float has about 7 decimal digits and a double about 15 to 16. Integers are exact up to 2^24 in a float and 2^53 in a double.
  • NaN payloads are not preserved reliably: reading any NaN gives NaN, and the bytes written for it may differ between engines. Do not use NaN bit patterns as data.
  • Byte order: the same rules as for integers. Choose the order the format uses; the numbers in the examples are in IEEE 754 layout (sign bit, exponent, fraction).
  • Arrays of numbers: for many values, a Float32Array or Float64Array over the same memory is much faster than calling these methods in a loop, as long as the byte order is the machine's own (little-endian on all supported Node.js platforms). Otherwise use swap32() or swap64() first.
  • DataView offers getFloat32, getFloat64 and the matching setters with a run-time endianness flag.
  • Typed array alignment: new Float64Array(buf.buffer, buf.byteOffset, n) requires byteOffset to be a multiple of 8, which pooled buffers do not guarantee. Copy first if unsure.