Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: (RATE func) Validate cash flow signs and support zero PMT scenarios for Excel compatibility (#5910)
Enajjachi Zakariaa6484c6020704
diff --git a/packages/engine-formula/src/functions/financial/rate/__tests__/index.spec.ts b/packages/engine-formula/src/functions/financial/rate/__tests__/index.spec.tsindex 6e558c93a7..db10208812 100644--- a/packages/engine-formula/src/functions/financial/rate/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/financial/rate/__tests__/index.spec.ts@@ -136,5 +136,36 @@ describe('Test rate function', () => {[ErrorType.NA, ErrorType.NA, ErrorType.NA, ErrorType.NA],]);});++ // New tests for all-positive or all-negative cash flows+ it('Returns #NUM! when all cash flows have the same positive sign', () => {+ const result = testFunction.calculate(+ NumberValueObject.create(12),+ NumberValueObject.create(100),+ NumberValueObject.create(1000),+ NumberValueObject.create(100)+ );+ expect(getObjectValue(result)).toBe(ErrorType.NUM);+ });++ it('Returns #NUM! when all cash flows have the same negative sign', () => {+ const result = testFunction.calculate(+ NumberValueObject.create(12),+ NumberValueObject.create(-100),+ NumberValueObject.create(-1000),+ NumberValueObject.create(-100)+ );+ expect(getObjectValue(result)).toBe(ErrorType.NUM);+ });++ it('Calculates rate correctly with mixed signs', () => {+ const result = testFunction.calculate(+ NumberValueObject.create(48),+ NumberValueObject.create(0),+ NumberValueObject.create(-8000),+ NumberValueObject.create(9000)+ );+ expect(typeof getObjectValue(result)).toBe('number');+ });});});diff --git a/packages/engine-formula/src/functions/financial/rate/index.ts b/packages/engine-formula/src/functions/financial/rate/index.tsindex 2eb1ea4c59..8337532b54 100644--- a/packages/engine-formula/src/functions/financial/rate/index.ts+++ b/packages/engine-formula/src/functions/financial/rate/index.ts@@ -14,14 +14,14 @@* limitations under the License.*/-import { ErrorType } from '../../../basics/error-type';+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';import type { BaseValueObject } from '../../../engine/value-object/base-value-object';+import { ErrorType } from '../../../basics/error-type';+import { expandArrayValueObject } from '../../../engine/utils/array-object';+import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';import { ErrorValueObject } from '../../../engine/value-object/base-value-object';import { NumberValueObject } from '../../../engine/value-object/primitive-object';import { BaseFunction } from '../../base-function';-import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';-import { expandArrayValueObject } from '../../../engine/utils/array-object';-import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';export class Rate extends BaseFunction {override minParams = 3;@@ -82,7 +82,13 @@ export class Rate extends BaseFunction {typeValue = typeValue ? 1 : 0;- if (nperValue <= 0 || pmtValue >= 0) {+ if (nperValue <= 0) {+ return ErrorValueObject.create(ErrorType.NUM);+ }++ // ? return error whenever the cash flows doesn’t make economic sense+ if ((pmtValue >= 0 && pvValue >= 0 && fvValue >= 0) ||+ (pmtValue <= 0 && pvValue <= 0 && fvValue <= 0)) {return ErrorValueObject.create(ErrorType.NUM);}