math-expression-evaluator icon indicating copy to clipboard operation
math-expression-evaluator copied to clipboard

remove incomplete fix for floating point issues

Open cmcnulty opened this issue 2 years ago • 0 comments

Suggest removing the code: return parseFloat(stack[0].value.toFixed(15)) from src/postfix_evaluator.ts because it only partially solves the issue floating point math, and because there are better work-arounds. The following code demonstrates both that floating point issues exist, and also that the addToken functionality already allows end-users to fix the issue according to their needs.

import Mexp from './dist/index.js';
import Big from 'big.js';
var mexp = new Mexp();

console.log(mexp.eval('.1 + .2')); // 0.3
console.log(mexp.eval('5.33 + 5.2')); // 10.530000000000001

mexp.addToken([{
    type:2,
    token:"+",
    show:"+",
    value:function(a,b){
        return Big(a).plus(b).toNumber();
    }
}])

console.log(mexp.eval('.1 + .2')); // 0.3
console.log(mexp.eval('5.33 + 5.2')); // 10.53

cmcnulty avatar May 04 '23 13:05 cmcnulty