Skip to content

Commit

Permalink
fix #3192: function isNaN returns false for NaN units in a matr…
Browse files Browse the repository at this point in the history
…ix or array

* Use referToSelf() to recursively check if various types are NaN in an array or matrix

* fix array test description from isNegative to isNaN

* Add test for units in a matrix

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
  • Loading branch information
lgerin and josdejong committed Apr 24, 2024
1 parent 5c12c07 commit 81d2e71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/function/utils/isNaN.js
Expand Up @@ -54,8 +54,6 @@ export const createIsNaN = /* #__PURE__ */ factory(name, dependencies, ({ typed
return Number.isNaN(x.value)
},

'Array | Matrix': function (x) {
return deepMap(x, Number.isNaN)
}
'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
})
})
8 changes: 6 additions & 2 deletions test/unit-tests/function/utils/isNaN.test.js
Expand Up @@ -59,14 +59,18 @@ describe('isNegative', function () {
assert.strictEqual(isNaN(''), false)
})

it('should test isNegative element wise on an Array', function () {
it('should test isNaN element wise on an Array', function () {
assert.deepStrictEqual(isNaN([0, 5, -2, NaN]), [false, false, false, true])
})

it('should test isNegative element wise on a Matrix', function () {
it('should test isNaN element wise on a Matrix', function () {
assert.deepStrictEqual(isNaN(math.matrix([0, 5, -2, NaN])), math.matrix([false, false, false, true]))
})

it('should test isNaN element wise on a Matrix of units', function () {
assert.deepStrictEqual(isNaN(math.matrix([new Unit(3, 'ft'), new Unit(NaN, 'ft')])), math.matrix([false, true]))
})

it('should throw an error in case of unsupported data types', function () {
assert.throws(function () { isNaN(new Date()) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { isNaN({}) }, /TypeError: Unexpected type of argument/)
Expand Down

0 comments on commit 81d2e71

Please sign in to comment.