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

Substitution and negative power returns wrong result #159

Open
truebluepl opened this issue Apr 25, 2024 · 4 comments
Open

Substitution and negative power returns wrong result #159

truebluepl opened this issue Apr 25, 2024 · 4 comments

Comments

@truebluepl
Copy link

Description

Substitution of x and negative power returns wrong result.

Steps to Reproduce

const ce = new ComputeEngine.ComputeEngine();

//x=3/7
let a = ce.parse('\frac{x^{-1}}{9}').subs({
x: ce.parse('\frac{3}{7}'),
});

//x=3/7 but included in expression
let b = ce.parse('\frac{(\frac{3}{7})^{-1}}{9}');
console.log(a,b);

Actual Behavior

Substitution returns 21.

Expected Behavior

Should returns 7/27.

@arnog
Copy link
Member

arnog commented Apr 25, 2024

I can't reproduce this. I'm getting 7/27 as the answer, as expected.

@truebluepl
Copy link
Author

Hmm... I've prepares fiddle: https://jsfiddle.net/uprwaxj7/

Here is screenshot. At bottom right corner is console output:
image

@arnog
Copy link
Member

arnog commented Apr 25, 2024

OK, I think this is a duplicate of #147. I can't reproduce the problem with top of tree.

@truebluepl
Copy link
Author

truebluepl commented Apr 29, 2024

I think there is a problem when the power of the numerator is exactly -1 (when we deal with fraction).
I made some test:

//wrong, result=8, should be 0.5
let a = ce.parse('\\frac{x^{-1}}{4}').subs({
  x: ce.parse('0.5'),
}).N().value;
console.log(a);

//wrong, result=1, should be 0.25
let b = ce.parse('\\frac{x^{-1}}{2}').subs({
  x: ce.parse('2'),
}).N().value;
console.log(b);

//ok
let c = ce.parse('x^{-1}').subs({
  x: ce.parse('2'),
}).N().value;
console.log(c);

//ok
let d = ce.parse('\\frac{2}{x^{-1}}').subs({
  x: ce.parse('2'),
}).N().value;
console.log(d);

//ok
let e = ce.parse('\\frac{x^{-2}}{x^{-1}}').subs({
  x: ce.parse('2'),
}).N().value;
console.log(e);

//ok
let f = ce.parse('\\frac{x^{-2}}{4}').subs({
  x: ce.parse('2'),
}).N().value;
console.log(f);

//ok
let g = ce.parse('\\frac{x^{-3}}{4}').subs({
  x: ce.parse('2'),
}).N().value;
console.log(g);

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

No branches or pull requests

2 participants