compute-engine icon indicating copy to clipboard operation
compute-engine copied to clipboard

Substitution and negative power returns wrong result

Open truebluepl opened this issue 1 year ago • 4 comments

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.

truebluepl avatar Apr 25 '24 07:04 truebluepl

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

arnog avatar Apr 25 '24 16:04 arnog

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

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

truebluepl avatar Apr 25 '24 17:04 truebluepl

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

arnog avatar Apr 25 '24 19:04 arnog

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);

truebluepl avatar Apr 29 '24 07:04 truebluepl

Verified these all work with TOT.

arnog avatar Jun 25 '24 14:06 arnog

Hello! What do you mean by TOT? I prepared those examples using JSFiddle: https://jsfiddle.net/dxr6u7La/ Still with bugs...

truebluepl avatar Jun 25 '24 14:06 truebluepl

TOT is top of tree, the latest HEAD in github.

arnog avatar Jun 25 '24 15:06 arnog