jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
517 stars 82 forks source link

Incorrect output for some numbers that are raised to 1/2. #449

Closed Saikedo closed 5 years ago

Saikedo commented 5 years ago

So I noticed today that some specific numbers raised to 1/2 produce an incorrect result. For example

Number(nerdamer("(11.85)^(1/2)").evaluate())

Outputs 13.769531582446806 while the expected result is 3.4423828956117.

What is weird is that the 11.85 number is very specific. Meaning 11.851 raised to 1/2 works just fine.

jiggzson commented 5 years ago

@Saikedo, I don't even know how to feel about the fact that bugs like this persist after all this time. Dividing 13.769531582446806 by 3.4423828956117 gives me 4 which lets me know that I'm off by a factor of 2 somewhere since it's the square root. I'll take a look at this. Thanks for reporting.

jiggzson commented 5 years ago

@Saikedo, the problem can be traced back to factors of squares moved from under the square root. 20^(-1/2) gives the same incorrect result. You can fix the problem by changing this line from

return sqrt(symbol.group === P ? new Symbol(symbol.value) : symbol.toLinear()).setPower(new Frac(sign));

to

var retval = sqrt(symbol.group === P ? new Symbol(symbol.value) : symbol.toLinear());
//place back the sign of the power
if(sign < 0)
    retval.invert();
return retval;

I'll leave this issue open until I can apply this fix.