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.
const buf = Buffer.alloc(8); buf.writeDoubleBE(Math.PI); buf.readDoubleBE(0); // 3.141592653589793
Try it
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.
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
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_RANGEif the offset is negative, fractional, or too large for the size.ERR_INVALID_ARG_TYPEif the offset is not a number when reading.- Values outside the float range are written as
Infinityor-Infinity; values too small become0or a subnormal number. Neither case throws. - A non-numeric
valuesuch as"x"is written asNaN.
Usage notes
- Float rounding is visible:
0.1written as a float reads back as0.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
Float32ArrayorFloat64Arrayover 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 useswap32()orswap64()first. DataViewoffersgetFloat32,getFloat64and the matching setters with a run-time endianness flag.- Typed array alignment:
new Float64Array(buf.buffer, buf.byteOffset, n)requiresbyteOffsetto be a multiple of 8, which pooled buffers do not guarantee. Copy first if unsure.