Skip to content

JachinZou/vite-plugin-arithmetic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A vite plugin as a solution for the problem in the calculation precision of the floating points numbers.

Usage

npm install vite-plugin-arithmetic --save-dev

Add vite-plugin-arithmetic

vite.config.js

import arithmetic from 'vite-plugin-arithmetic'

{
    // other config ....
    plugins: [
      vue(), // if vue plugin exist, it should be front of arithmetic
      // vuejsx(), // maybe you should use this, also should be fron of arithmetic
      arithmetic(),  // this plugin should behind of vue plugin, like vue, vuejsx etc.
    ],
    // other config ....
  }

Example

By this plugin, it translate BinaryExpression to FunctionCall for a right result with float number.

var a = 0.1 + 0.2;
//0.30000000000000004
	     
import { add as $$add_87676230 } from 'virtual:calc';
var a = $$add_87676230(0.1, 0.2);
//0.3
var a = 0.1 + 0.2;
var b = 0.8 - 0.2;
//0.30000000000000004
//0.6000000000000001
	     
import { add as $$add_87676230, sub as $$sub_87676230 } from 'virtual:calc';
var a = $$add_87676230(0.1, 0.2);
var a = $$sub_87676230(0.8, 0.2);
//0.3
//0.6

Note: 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

TODO: resolved by https://www.npmjs.com/package/@javascript-obfuscator/escodegen PropertyDefinition estools/escodegen#443

About

A vite plugin for resolving the problem of losing precision of floating-point number

Resources

Stars

Watchers

Forks

Packages

No packages published