• Node.js Video Tutorials

Node.js - Buffer.readDoubleLE() Method



The Node.JS Buffer.readDoubleLE() method helps to read a little-endian double 64-bit number at a given offset from the current buffer object.

A double 64-bit is also called FP64 or float64. When used it takes up 64-bit of memory in your computer.

The double 64-bit is divided as sign, exponent, and significand precision. The sign takes 1 bit, exponent takes 11 bit and the rest 53 (52 used for explicit storage) bits are taken by significand precision.

  • The sign bit tells about the sign of the number positive or negative.

  • The exponent is an 11-bit unsigned integer and has minimum value as 0 and maximum value as 2047.

  • The significand is 53 bits. For example, a number is 120.53. So here the integer value 12053 is the significand, $10^{-2}$ is power term, with -2 as the exponent.

Syntax

Following is the syntax of the Node.JS Buffer.readDoubleLE() Method

buf.readDoubleLE([offset])

Parameters

  • offset − The offset that indicates the position to start reading. The offset is greater than or equal to 0 and also less than or equal to buffer.length-8. The default value is 0.

Return value

This method returns the 64-bit little endian double number from the current buffer at the given offset.

Example

To create a buffer, we are going to make use of NodeJS Buffer.from() method −

const buffer = Buffer.from([11, 12, 13, 14, 15, 16, 17, 18]);
console.log(buffer.readDoubleLE(0));
console.log(buffer);

Output

The offset we have used with this method is 0. The 64-bit double little endian number at the 0th position will be returned. The buffer length created for the above is 8. So we can only use the offset with the value 0. If any value >0 it will give an error ERR_OUT_OF_RANGE.

1.1800807103066695e-221
<Buffer 0b 0c 0d 0e 0f 10 11 12>

Example

Let us create a buffer with 16 bits and see the value returned using the Node.JS Buffer.readDoubleLE() method.

const buffer = Buffer.from([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
console.log("Length of buffer is ", buffer.length);  
console.log("Reading at big integer at offset 1:", buffer.readDoubleLE(1));

Output

In the above example, the length of the buffer created is 16. So for offset, we can use the value from 0 to 8. On executing the above program, it will generate the following output −

Length of buffer is  16
Reading at big integer at offset 1: 3.7258146895053074e-265

Example

This example will check on the error if the offset is greater than the buffer.length −8. Let us create a buffer having a length of 8 and with that, you can offset the value as 0.

const buffer = Buffer.from([1, 2 ,3, 4, 5, 6, 7, 8]);
console.log("buffer length is ", buffer.length);  
console.log("Reading at big integer at offset 1:", buffer.readDoubleLE(1));

Output

Since we have used an offset greater than 0 in the above program, it will throw an error as shown below −

buffer length is 8
internal/buffer.js:58
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 0. Received 1
   at boundsError (internal/buffer.js:58:9)
   at Buffer.readDoubleForwards [as readDoubleLE] (internal/buffer.js:460:5)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:3:59)
   at Module._compile (internal/modules/cjs/loader.js:816:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
   at Module.load (internal/modules/cjs/loader.js:685:32)
   at Function.Module._load (internal/modules/cjs/loader.js:620:12)
   at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
   at internal/main/run_main_module.js:21:11
nodejs_buffer_module.htm
Advertisements