Git Product home page Git Product logo

vite-plugin-arithmetic's Introduction

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

vite-plugin-arithmetic's People

Contributors

jachinzou avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.