mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Uncaught Error: Cannot convert "kg^4" to a number

Open Areeba4427 opened this issue 3 years ago • 5 comments

var e = math.simplify(equation , { [p1]: (var2[0]), [p2]: (var2[1])

});
console.log(e);
return(e);

}

equation contains a dynamic equation like p1+p2 , p1-p2 or 5p1/4. Values of P1 and P2 are units like kg^2 , m^-4 etc. Any idea how can i resolve this issue?

Areeba4427 avatar Jun 13 '22 09:06 Areeba4427

Can you provide a minimal example demonstrating the issue?

josdejong avatar Jun 13 '22 09:06 josdejong

OK. let suppose a user ha entered an equation to be solved. like equation = a + b. it can be any variable rather than a or b. these variables have values. like a = kg and b = kg^-1.

I want these units (kg and kg^-1) to be solved according to given equation. But the maths js library throws error for the above syntax. Can you help me with this?


 var e = math.parse(equation , {
        [p1]: (var2[0]),
        [p2]:  (var2[1])
      
    });
    console.log(e);
    return(e);
}

Areeba4427 avatar Jun 13 '22 09:06 Areeba4427

The contents of the scope is taken as a value, not as an expression. I think you can evaluate your variables first too if they contain an expression?

josdejong avatar Jun 13 '22 10:06 josdejong

contents of scope {p1 , p2} contains names of dynamic variables like a and b etc. Thats why I have to pass them as value. This case is working fine for arithmetic variables but is problematic in case of units.

Areeba4427 avatar Jun 13 '22 10:06 Areeba4427

can you give a concrete example? Maybe I can give you a concrete suggestion then :)

josdejong avatar Jun 14 '22 08:06 josdejong