Mathjs: Correct support of the sin(PI)

Created on 19 Apr 2016  路  7Comments  路  Source: josdejong/mathjs

Why no one not support sin(PI) = 0
For example: I have equation y=x*sin(PI), I try to check knowledges of somebody, and propose to introduce the variables which satisfy: Enter x: __ y: __. User enter y=0, x = 100, and result is false.

question

Most helpful comment

Ah, yes that's true. We can't compare against zero since the function can't know what "scale" to compare the value against. It's explained in the docs:

http://mathjs.org/docs/datatypes/numbers.html#equality

To work around this you could add for example 10 to both values and compare that:

var offset = 10;
math.equal(0 + offset, 132*math.sin(math.pi) + offset); // true

All 7 comments

for now I use if (result >=-1e-10 && result <= 1e-10), but, as for me, it's a dirty hack. Maybe has sense to create static library, with preloaded results?

Calculations with irrational numbers and trigonometric functions result in round off errors. The relational functions of math.js like math.equal reckon with round-off errors.

See relevant docs:
http://mathjs.org/docs/datatypes/numbers.html#roundoff-errors
http://mathjs.org/docs/datatypes/numbers.html#equality
http://mathjs.org/docs/reference/functions/equal.html

@josdejong Thanks for response, but it's not work for me

math.equal(0,132*math.sin(math.pi)) === false
console.log(math.equal(0,math.eval('11234*sin(pi)')));

Ah, yes that's true. We can't compare against zero since the function can't know what "scale" to compare the value against. It's explained in the docs:

http://mathjs.org/docs/datatypes/numbers.html#equality

To work around this you could add for example 10 to both values and compare that:

var offset = 10;
math.equal(0 + offset, 132*math.sin(math.pi) + offset); // true

Thank you! Now it's clear

But it's doesn't work with big numbers

var offset = 10;
math.equal(0 + offset, 999999999*math.sin(math.pi) + offset); // false

You will have to rewrite this check to allow using bignumbers too. Replacing the operators + and * with math.add and math.multiply should do the trick.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gnobre picture gnobre  路  4Comments

kyoncy picture kyoncy  路  4Comments

skgadi picture skgadi  路  3Comments

nioate picture nioate  路  4Comments

SamuelMarks picture SamuelMarks  路  3Comments