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

Comments

Projects
None yet
3 participants
@gpaucot

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

This comment has been minimized.

Owner

josdejong commented Aug 26, 2017

Thanks for reporting this bug @gpaucot

@josdejong

This comment has been minimized.

Owner

josdejong commented Aug 26, 2017

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

This comment has been minimized.

Collaborator

firepick1 commented Aug 28, 2017

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

This comment has been minimized.

Collaborator

firepick1 commented Aug 28, 2017

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

@gpaucot

This comment has been minimized.

gpaucot commented Aug 28, 2017

Thank you very much both ! ;)

@josdejong

This comment has been minimized.

Owner

josdejong commented Aug 28, 2017

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

@josdejong

This comment has been minimized.

Owner

josdejong commented Aug 28, 2017

This should be fixed now in v3.16.3

@josdejong josdejong closed this Aug 28, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment