From a131845524812c2117197cbfcbc8e054f8b73a24 Mon Sep 17 00:00:00 2001 From: David Contreras Date: Thu, 25 Jan 2024 08:27:42 -0600 Subject: [PATCH] change isZero to take epsilon into account --- src/function/utils/isZero.js | 19 ++++--------------- test/unit-tests/function/utils/isZero.test.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/function/utils/isZero.js b/src/function/utils/isZero.js index 4134200dc8..65131eb4e3 100644 --- a/src/function/utils/isZero.js +++ b/src/function/utils/isZero.js @@ -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`, @@ -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 => diff --git a/test/unit-tests/function/utils/isZero.test.js b/test/unit-tests/function/utils/isZero.test.js index 6e1f7dd4d5..da9b4593a5 100644 --- a/test/unit-tests/function/utils/isZero.test.js +++ b/test/unit-tests/function/utils/isZero.test.js @@ -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)