1.0.0 • Published 5 years ago
babel-plugin-precise-arithmetic v1.0.0
babel-plugin-precise-arithmetic
A babel plugin as a solution for the problem in the calculation precision of the floating points numbers.
 
 
Usage
npm install babel-plugin-precise-arithmetic --save-devAdd babel-plugin-precise-arithmetic
.babelrc
{
  "plugins": ["precise-arithmetic"]
}or
webpack.config.js
...
{
	test: /\.js$/,
	loader: 'babel-loader',
	option: {
		plugins: [
			require('babel-plugin-precise-arithmetic')
		]
	},
},
...Example
By this plugin, it translate BinaryExpression to FunctionCall for a right result with float number.
var a = 0.1 + 0.2;
//0.30000000000000004
	↓ ↓ ↓ ↓ ↓ ↓
var { accAdd } = require('babel-plugin-precise-arithmetic/src/calc.js');
var a = accAdd(0.1, 0.2); // 0.3var a = 0.1 + 0.2;
var b = 0.8 - 0.2;
//0.30000000000000004
//0.6000000000000001
	↓ ↓ ↓ ↓ ↓ ↓
var { accAdd, accSub } = require('babel-plugin-precise-arithmetic/src/calc.js');
var a = accAdd(0.1, 0.2); //0.3
var a = accSub(0.8, 0.2); //0.6Note: it doesn't work with eval() And just support (+ - * \ += -=), if the members of the operator is not Number type, it will return the result as it should be
1.0.0
5 years ago