Closed dev-kg closed 2 years ago
I'm not able to reproduce this. For me, I get:
nthRoot(-5*0, 2) = 0
Some ideas:
nthRoot
with a capital R?As for the second case:
nthRoot(-5, 2) = error
nthRoot
always returns a real number by design; there's a good discussion of it in #851. Here are some alternatives to get the complex roots:
pow(-5, 1/2)
, which returns the principal root (the root with the smallest "angle")nthRoots(-5, 2)
, which returns an array of all the rootsYes the configuration is on bigNumbers
For the first case, ie nthRoot(-5*0,2) = error.
I created a test that shows the same as was what @ericman314 got, and even setup the configuration to be on bigNumbers:
import * as math from 'mathjs';
import { create, all } from 'mathjs';
beforeAll(() => {
const mathjs = create(all);
mathjs.config({ number: 'BigNumber' });
});
it('nthRoot does what it should', () => {
expect(math.nthRoot(-5 * 0, 2)).toBe(0);
});
This returns back that the test indeed passes. I doubt that the problem is with the capital R in nthRoot
since if you don't have the R
an error is thrown saying that it is not a function.
What version of the package are you on?
@nwiatrek I don't think the beforeAll
in your test is doing anything: you're not using the created mathjs
and also not using the expression parser which would parsed the numbers as BigNumbers.
you are correct, my apologies. is this something that is more correct:
it('nthRoot does what it should', () => {
const mathjs = create(all);
mathjs.config({ number: 'BigNumber' });
expect(mathjs.evaluate(mathjs.nthRoot(-5 * 0, 2))['e']).toBe(0);
});
@nwiatrek you'll have to pass a string to mathjs.evaluate
in order to create BigNumbers from values in the expression.
I can reproduce the case like this (node.js):
const { create, all } = require('.')
const mathNumber = create(all)
console.log(mathNumber.evaluate('nthRoot(-5 * 0, 2)'))
// 0 (as expected)
const mathBigNumber = create(all, { number: 'BigNumber' })
console.log(mathBigNumber.evaluate('nthRoot(-5 * 0, 2)'))
// Error: Root must be odd when a is negative.
The reason that the error is thrown in case of BigNumbers is that the current implementation sees -0
as a negative number in case of a BigNumber, but not in case of a number. We we improve that edge case for BigNumbers.
@KevinGervais would fixing this edge case address your issue?
Closing this issue because if inactivity.
by example if I do:
it shoud be:
also, when we have any other negative number it equals error when it could be a complex number by example:
it should be:
the sqrt function works properly with those cases, maybe you could replace nthroot by sqrt when the root is 2, but the problem is that if we have any even root, the same problem occurs