Skip to content

Commit

Permalink
change isZero to take epsilon into account
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Jan 25, 2024
1 parent e2bdd4a commit a131845
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/function/utils/isZero.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
import { isZeroNumber } from '../../plain/number/index.js'

const name = 'isZero'
const dependencies = ['typed']
const dependencies = ['typed', 'equalScalar']

export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed, equalScalar }) => {
/**
* Test whether a value is zero.
* The function can check for zero for types `number`, `BigNumber`, `Fraction`,
Expand Down Expand Up @@ -40,18 +39,8 @@ export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed
* Throws an error in case of an unknown data type.
*/
return typed(name, {
number: isZeroNumber,

BigNumber: function (x) {
return x.isZero()
},

Complex: function (x) {
return x.re === 0 && x.im === 0
},

Fraction: function (x) {
return x.d === 1 && x.n === 0
'number | BigNumber | Complex | Fraction': function (x) {
return equalScalar(x, 0)
},

Unit: typed.referToSelf(self =>
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/function/utils/isZero.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe('isZero', function () {
assert.strictEqual(isZero(NaN), false)
})

it('should test whether a number is almost zero', function () {
const mymath = math.create()
assert.strictEqual(mymath.isZero(1e-16), true)
assert.strictEqual(mymath.isZero(1e-2), false)
mymath.config({ epsilon: 1e-3 })
assert.strictEqual(mymath.isZero(1e-16), true)
// assert.strictEqual(mymath.isZero(1e-4), true)
// assert.strictEqual(mymath.isZero(1e-2), false)
})

it('should test whether a boolean is zero', function () {
assert.strictEqual(isZero(true), false)
assert.strictEqual(isZero(false), true)
Expand Down

0 comments on commit a131845

Please sign in to comment.