MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.63k stars 743 forks source link

NaN raised to a power of zero returns 1 (not NaN) #340

Closed Kolobamanacas closed 1 year ago

Kolobamanacas commented 1 year ago

Repro steps:

import BigNumber from "bignumber.js";

const number = new BigNumber('1 Definitely 2 not 3 a 4 number 5').exponentiatedBy(new BigNumber('0'));

console.log(number.toString()); // Prints 1 (not NaN).
console.log(number.toFixed()); // Prints 1 (not NaN).

I'm not sure whether this behavior is intentional or not, but it seems to me that any arithmetical operation including not-a-number must return not-a-number. Getting "1" creates false impression that operation is valid and was performed using valid operands, which, in my opinion, is not the case with not-a-numbers.

MikeMcl commented 1 year ago

Whether it would be better to return NaN is a moot point here as the power operation intentionally defers to Math.pow:

Math.pow(NaN, 0)    // 1
Kolobamanacas commented 1 year ago

Hmm, I see. I wasn't aware of this Math's behavior, thanks. I don't think it's correct either, but I guess it's not the place from where we could change it. :)