Mathjs: fix basic arithmetic

Created on 17 Jan 2018  路  3Comments  路  Source: josdejong/mathjs

This is a feature request.
OS: Max OS X 10.12.6
NodeJS: 8.6.0
npm: 5.2.0
mathjs: 3.20.1

In JavaScript the following code:

184.72 + 319.94

gives:

504.65999999999997

I hoped this library fixes such a behavior. Unfortunately:

const math = require('mathjs');

math.eval('184.72 + 319.94')

still gives:

504.65999999999997
question

Most helpful comment

You're seeing regular floating point round-off errors, mathjs has solutions for that.

See: http://mathjs.org/docs/datatypes/numbers.html#roundoff-errors

All 3 comments

The same with math.add(184.72, 319.94)

Please use toFixed(2) to address this. It is a standard Javascript function:
(184.72+319.94).toFixed(2)
This is not a mathjs issue.

You're seeing regular floating point round-off errors, mathjs has solutions for that.

See: http://mathjs.org/docs/datatypes/numbers.html#roundoff-errors

Was this page helpful?
0 / 5 - 0 ratings