Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong calculation in derivative with unaryMinus #934

Closed
gpaucot opened this issue Aug 25, 2017 · 7 comments
Closed

Wrong calculation in derivative with unaryMinus #934

gpaucot opened this issue Aug 25, 2017 · 7 comments
Labels

Comments

@gpaucot
Copy link

gpaucot commented Aug 25, 2017

var math = require('mathjs'); // 3.16.2

console.log(math.derivative('1 - 2 * x', 'x').toString());       // => -2 ok
console.log(math.derivative('1 - +2 * x', 'x').toString());      // => -2 ok
console.log(math.derivative('1 - -2 * x', 'x').toString());      // => -2 not ok, should be 2
console.log(math.derivative('1 - (-2) * x', 'x').toString());    // => -2 not ok, should be 2
console.log(math.derivative('1 - (-2 * x)', 'x').toString());    // => -2 not ok, should be 2
console.log(math.derivative('1 - ((-2) * x)', 'x').toString());  // => -2 not ok, should be 2
console.log(math.derivative('-((-2) * x) - 1', 'x').toString()); // => -2 not ok, should be 2

BTW awesome work guys thanks ! 👍

@josdejong josdejong added the bug label Aug 26, 2017
@josdejong
Copy link
Owner

Thanks for reporting this bug @gpaucot

@josdejong
Copy link
Owner

It looks like this issue is originating in simplify and not in derivative:

math.derivative('1 - -2x', 'x', {simplify: false}).toString(); // Correct: 0 - -2 * 1
math.simplify('0 - -2 * 1').toString()  // Wrong: -2

Or simpler:

math.simplify('--2') // Wrong: -2 (should be 2)

@firepick1
Copy link
Collaborator

EEK! I'm on it. Sorry for the delay--was having fun elsewhere. 😢

firepick1 added a commit that referenced this issue Aug 28, 2017
@firepick1
Copy link
Collaborator

Fixed in simplify2 5b175d5
Added unit test for double unaryMinus. @jos thanks for triage and diagnosis.

@gpaucot
Copy link
Author

gpaucot commented Aug 28, 2017

Thank you very much both ! ;)

@josdejong
Copy link
Owner

Thanks @firepick1 for picking this up, I couldn't immediately pinpoint the underlying cause.

@josdejong
Copy link
Owner

This should be fixed now in v3.16.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants