Open In App

D3.js Adder() Method

Last Updated : 23 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

With the help of d3.Adder() method, we can get the adder to compute the full precision sum of floating point values and set 0 as a default value by using this method.

Syntax:

const adder = new d3.Adder()

Parameter: This function has no parameters.

Return Value: It returns the adder to compute the full precision sum of float values.

Note: To execute the below examples you have to install the d3 library by using this command prompt we have to execute the following command.

npm install d3

Example 1: In this example, we can see that by using d3.Adder() method, we are able to get the adder for computation of the full precision sum of floating point numbers by using this method.

Filename: index.js




// Defining d3 variable  
var d3 = require('d3');
  
const adder = new d3.Adder();
for (let i = 0; i < 3; i++) {
  adder.add(.01);
}
  
console.log(adder.valueOf());


Output:

0.03

Example 2: Filename: index.js




// Defining d3 contrib variable  
var d3 = require('d3');
  
var arr = []
for(var i = 0; i < 10; i++) {
    arr.push(i);
}
  
const adder = new d3.Adder();
for (let i = 0; i < 10; i++) {
  adder.add(arr[i]);
}
  
console.log(adder.valueOf());


Output:

45

Note: The above program will compile and run by using the following command:

node index.js

Reference: https://github.com/d3/d3-array/blob/master/README.md


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads