Skip to content

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.

fix(formula): read complex parts after the first sign like Excel (#8178)

wpxp123456f7983f5ef329
diff --git a/packages/engine-formula/src/basics/__tests__/complex.spec.ts b/packages/engine-formula/src/basics/__tests__/complex.spec.ts
index 5aee0ac773..bdf61e82a5 100644
--- a/packages/engine-formula/src/basics/__tests__/complex.spec.ts
+++ b/packages/engine-formula/src/basics/__tests__/complex.spec.ts
@@ -14,9 +14,18 @@
* limitations under the License.
*/
+import { DateSystem, excelDateSerial } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
import { Complex } from '../complex';
+/**
+ * `2-3` is the text to number conversion of February 3 of the current year,
+ * just like Excel, so the expected serial cannot be a constant.
+ */
+function februaryThirdSerial(dateSystem: DateSystem = DateSystem.Date1900): number {
+ return excelDateSerial(new Date(Date.UTC(new Date().getUTCFullYear(), 1, 3)), dateSystem);
+}
+
describe('Test complex', () => {
it('Parses a real part written with an exponent', () => {
const complex = new Complex('1e-7-i');
@@ -43,9 +52,62 @@ describe('Test complex', () => {
expect(complex.getINum()).toBe(-1);
});
- it('Keeps rejecting two separators', () => {
- expect(new Complex('1+2-3i').isError()).toBe(true);
+ it('Keeps the sign after the first one inside the imaginary part', () => {
+ const complex = new Complex('1+2-3i');
+
+ expect(complex.isError()).toBe(false);
+ expect(complex.getRealNum()).toBe(1);
+ expect(complex.getINum()).toBe(februaryThirdSerial());
+
+ const negated = new Complex('1-2-3i');
+
+ expect(negated.isError()).toBe(false);
+ expect(negated.getRealNum()).toBe(1);
+ expect(negated.getINum()).toBe(-februaryThirdSerial());
+ });
+
+ it('Rejects a sign inside the imaginary part that is not a number', () => {
expect(new Complex('1-2+3i').isError()).toBe(true);
+ expect(new Complex('1+2+3i').isError()).toBe(true);
+ });
+
+ it('Reads a date part with the workbook date system', () => {
+ const complex = new Complex('1+2-3i', DateSystem.Date1904);
+
+ expect(complex.isError()).toBe(false);
+ expect(complex.getRealNum()).toBe(1);
+ expect(complex.getINum()).toBe(februaryThirdSerial(DateSystem.Date1904));
+ });
+
+ it('Reads the parts with the text to number conversion of a cell', () => {
+ const percent = new Complex('1+50%i');
+
+ expect(percent.isError()).toBe(false);
+ expect(percent.getRealNum()).toBe(1);
+ expect(percent.getINum()).toBe(0.5);
+
+ const spaced = new Complex(' 1+2i ');
+
+ expect(spaced.isError()).toBe(false);
+ expect(spaced.getRealNum()).toBe(1);
+ expect(spaced.getINum()).toBe(2);
+
+ const currency = new Complex('1+$2i');
+
+ expect(currency.isError()).toBe(false);
+ expect(currency.getRealNum()).toBe(1);
+ expect(currency.getINum()).toBe(2);
+
+ const grouped = new Complex('1+1,234i');
+
+ expect(grouped.isError()).toBe(false);
+ expect(grouped.getRealNum()).toBe(1);
+ expect(grouped.getINum()).toBe(1234);
+ });
+
+ it('Rejects a part that is not a finite number', () => {
+ expect(new Complex('1+0x10i').isError()).toBe(true);
+ expect(new Complex('1+Infinity i').isError()).toBe(true);
});
it('Writes tiny numbers in plain decimal notation', () => {
diff --git a/packages/engine-formula/src/basics/complex.ts b/packages/engine-formula/src/basics/complex.ts
index 8198a7a8b4..a27bf13dce 100644
--- a/packages/engine-formula/src/basics/complex.ts
+++ b/packages/engine-formula/src/basics/complex.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import { DateSystem, getNumfmtParseValueFilter } from '@univerjs/core';
import Decimal from 'decimal.js';
// Decimal.cos has a bug when calculating PI/2
@@ -25,11 +26,15 @@ Decimal.prototype.cos = function (): Decimal {
/**
* Finds the sign that separates the real and imaginary parts. A sign at the
* start of the string or right after the exponent marker of a number such as
- * `1e-7` belongs to that number, so it is skipped.
+ * `1e-7` belongs to that number, so it is skipped. Excel splits the text at
+ * the first remaining sign, which keeps every later sign inside the imaginary
+ * part, e.g. `1+2-3i` reads its imaginary part as `2-3`.
*/
-function findSignIndex(inumberStr: string, sign: '+' | '-'): number {
+function findSeparatorIndex(inumberStr: string): number {
for (let i = 1; i < inumberStr.length; i++) {
- if (inumberStr[i] === sign && inumberStr[i - 1] !== 'e' && inumberStr[i - 1] !== 'E') {
+ const char = inumberStr[i];
+
+ if ((char === '+' || char === '-') && inumberStr[i - 1] !== 'e' && inumberStr[i - 1] !== 'E') {
return i;
}
}
@@ -37,6 +42,58 @@ function findSignIndex(inumberStr: string, sign: '+' | '-'): number {
return -1;
}
+/**
+ * Matches the parts that are already plain numbers, so the common case skips
+ * the full text to number conversion.
+ */
+const DECIMAL_PART_REG = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
+
+/**
+ * Reads one part of a complex number with the same text to number conversion
+ * Excel applies to cell text, so a part may also be a percentage such as `50%`
+ * or a date such as `2-3`, which is February 3 of the current year. The date
+ * serial follows the workbook date system, as it does for cell text.
+ */
+function parseComplexPart(part: string, dateSystem: DateSystem): number | null {
+ const value = part.trim();
+
+ if (value === '') {
+ return null;
+ }
+
+ if (DECIMAL_PART_REG.test(value)) {
+ const number = Number(value);
+
+ return Number.isFinite(number) ? number : null;
+ }
+
+ const parsed = getNumfmtParseValueFilter(value, { dateSystem });
+
+ if (!parsed || typeof parsed.v !== 'number' || !Number.isFinite(parsed.v)) {
+ return null;
+ }
+
+ return parsed.v;
+}
+
+/**
+ * Reads the coefficient of the imaginary unit. Excel accepts an empty
+ * coefficient and a bare sign, e.g. `i`, `1+i` and `-i`.
+ */
+function parseImaginaryCoefficient(part: string, dateSystem: DateSystem): number | null {
+ const value = part.trim();
+
+ if (value === '' || value === '+') {
+ return 1;
+ }
+
+ if (value === '-') {
+ return -1;
+ }
+
+ return parseComplexPart(value, dateSystem);
+}
+
/**
* Excel prints the parts of a complex number with at most 15 significant
* digits and keeps plain decimal notation while the text fits in 21
@@ -126,140 +183,66 @@ export class Complex {
private _isError: boolean = false;
- constructor(inumber: number | string) {
- if (`${inumber}`.trim() === '') {
- this._isError = true;
- return;
- }
+ private _dateSystem: DateSystem = DateSystem.Date1900;
+ constructor(inumber: number | string, dateSystem: DateSystem = DateSystem.Date1900) {
this._inumber = inumber;
- this._getImReal();
- this._getImAginary();
- this._getImSuffix();
+ this._dateSystem = dateSystem;
+ this._parseInumber();
}
- private _getImReal(): void {
- if (this._inumber === 0 || this._inumber === '0') {
- this._realNum = 0;
- return;
- }
+ /**
+ * Reads the text into its real and imaginary parts. Excel converts every
+ * part with the same text to number conversion it applies to cell values,
+ * so `1+2-3i` keeps `2-3` as its imaginary part, and any part that cannot
+ * be read makes the whole number invalid.
+ */
+ private _parseInumber(): void {
+ const inumberStr = `${this._inumber}`.trim();
- const inumberStr = `${this._inumber}`;
-
- if (['i', '+i', '1i', '+1i', '-i', '-1i', 'j', '+j', '1j', '+1j', '-j', '-1j'].indexOf(inumberStr) >= 0) {
- this._realNum = 0;
+ if (inumberStr === '') {
+ this._isError = true;
return;
}
- const plus = findSignIndex(inumberStr, '+');
- const minus = findSignIndex(inumberStr, '-');
-
- const last = inumberStr.substring(inumberStr.length - 1, inumberStr.length);
+ const last = inumberStr.substring(inumberStr.length - 1);
const unit = last === 'i' || last === 'j';
+ const body = unit ? inumberStr.substring(0, inumberStr.length - 1) : inumberStr;
+ const separatorIndex = findSeparatorIndex(body);
- if (plus >= 0 || minus >= 0) {
- if (!unit) {
- this._isError = true;
- return;
- }
-
- if (plus >= 0) {
- if (Number.isNaN(+inumberStr.substring(0, plus)) || Number.isNaN(+inumberStr.substring(plus + 1, inumberStr.length - 1))) {
- this._isError = true;
- } else {
- this._realNum = +inumberStr.substring(0, plus);
- }
- } else {
- if (Number.isNaN(+inumberStr.substring(0, minus)) || Number.isNaN(+inumberStr.substring(minus + 1, inumberStr.length - 1))) {
- this._isError = true;
- } else {
- this._realNum = +inumberStr.substring(0, minus);
- }
- }
- } else {
- if (unit) {
- if (Number.isNaN(+inumberStr.substring(0, inumberStr.length - 1))) {
- this._isError = true;
- } else {
- this._realNum = 0;
- }
- } else {
- if (Number.isNaN(+inumberStr)) {
- this._isError = true;
- } else {
- this._realNum = +inumberStr;
- }
- }
- }
- }
-
- private _getImAginary(): void {
- if (this._isError) {
- return;
- }
-
- if (this._inumber === 0 || this._inumber === '0') {
- this._iNum = 0;
- return;
- }
-
- let inumberStr = `${this._inumber}`;
-
- if (['i', 'j'].indexOf(inumberStr) >= 0) {
- this._iNum = 1;
+ if (!unit && separatorIndex >= 0) {
+ // A separator without an imaginary unit is not a complex number,
+ // e.g. `1+2-3`.
+ this._isError = true;
return;
}
- inumberStr = inumberStr.replace('+i', '+1i').replace('-i', '-1i').replace('+j', '+1j').replace('-j', '-1j');
+ let realText = body;
+ let iText: string | null = null;
- const plus = findSignIndex(inumberStr, '+');
- const minus = findSignIndex(inumberStr, '-');
-
- const last = inumberStr.substring(inumberStr.length - 1, inumberStr.length);
- const unit = last === 'i' || last === 'j';
-
- if (plus >= 0 || minus >= 0) {
- if (!unit) {
- this._isError = true;
- return;
- }
+ if (unit) {
+ realText = '';
- if (plus >= 0) {
- if (Number.isNaN(+inumberStr.substring(0, plus)) || Number.isNaN(+inumberStr.substring(plus + 1, inumberStr.length - 1))) {
- this._isError = true;
- } else {
- this._iNum = +inumberStr.substring(plus + 1, inumberStr.length - 1);
- }
- } else {
- if (Number.isNaN(+inumberStr.substring(0, minus)) || Number.isNaN(+inumberStr.substring(minus + 1, inumberStr.length - 1))) {
- this._isError = true;
- } else {
- this._iNum = -(+inumberStr.substring(minus + 1, inumberStr.length - 1));
- }
- }
- } else {
- if (unit) {
- if (Number.isNaN(+inumberStr.substring(0, inumberStr.length - 1))) {
- this._isError = true;
- } else {
- this._iNum = +inumberStr.substring(0, inumberStr.length - 1);
- }
+ if (separatorIndex >= 0) {
+ realText = body.substring(0, separatorIndex);
+ iText = body.substring(separatorIndex + 1);
} else {
- if (Number.isNaN(+inumberStr)) {
- this._isError = true;
- } else {
- this._iNum = 0;
- }
+ iText = body;
}
}
- }
- private _getImSuffix(): void {
- const inumberStr = `${this._inumber}`;
+ const realNum = realText === '' ? 0 : parseComplexPart(realText, this._dateSystem);
+ const iNum = iText === null ? 0 : parseImaginaryCoefficient(iText, this._dateSystem);
+ const iSign = separatorIndex >= 0 && body[separatorIndex] === '-' ? -1 : 1;
- const suffix = inumberStr.substring(inumberStr.length - 1);
+ if (realNum === null || iNum === null) {
+ this._isError = true;
+ return;
+ }
- this._suffix = suffix === 'i' || suffix === 'j' ? suffix : '';
+ this._realNum = realNum;
+ this._iNum = iSign * iNum;
+ this._suffix = unit ? last : '';
}
getRealNum(): number {
diff --git a/packages/engine-formula/src/functions/engineering/imabs/index.ts b/packages/engine-formula/src/functions/engineering/imabs/index.ts
index 4d618261be..9852b8e0b2 100644
--- a/packages/engine-formula/src/functions/engineering/imabs/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imabs/index.ts
@@ -38,7 +38,7 @@ export class Imabs extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imaginary/__tests__/index.spec.ts b/packages/engine-formula/src/functions/engineering/imaginary/__tests__/index.spec.ts
index c147bfa85d..c85324c801 100644
--- a/packages/engine-formula/src/functions/engineering/imaginary/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/engineering/imaginary/__tests__/index.spec.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import { DateSystem, excelDateSerial } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
@@ -32,6 +33,51 @@ describe('Test imaginary function', () => {
expect(result.getValue()).toBe(12);
});
+ it('Value is complex string with a sign inside the imaginary part', () => {
+ const inumber = StringValueObject.create('1+2-3i');
+ const result = testFunction.calculate(inumber);
+ // `2-3` is February 3 of the current year, as in Excel.
+ const februaryThird = excelDateSerial(new Date(Date.UTC(new Date().getUTCFullYear(), 1, 3)));
+ expect(result.getValue()).toBe(februaryThird);
+
+ const inumber2 = StringValueObject.create('1-2+3i');
+ const result2 = testFunction.calculate(inumber2);
+ expect(result2.getValue()).toBe(ErrorType.NUM);
+ });
+
+ it('Value is complex string with a sign inside the imaginary part on a 1904 workbook', () => {
+ const dateSystem1904 = new Imaginary(FUNCTION_NAMES_ENGINEERING.IMAGINARY);
+ dateSystem1904.setDateSystem(DateSystem.Date1904);
+
+ const inumber = StringValueObject.create('1+2-3i');
+ const result = dateSystem1904.calculate(inumber);
+ // The same February 3, written as a serial in the 1904 date system.
+ const februaryThird = excelDateSerial(new Date(Date.UTC(new Date().getUTCFullYear(), 1, 3)), DateSystem.Date1904);
+ expect(result.getValue()).toBe(februaryThird);
+ });
+
+ it('Value is complex string with a part written like cell text', () => {
+ const percent = StringValueObject.create('1+50%i');
+ expect(testFunction.calculate(percent).getValue()).toBe(0.5);
+
+ const currency = StringValueObject.create('1+$2i');
+ expect(testFunction.calculate(currency).getValue()).toBe(2);
+
+ const grouped = StringValueObject.create('1+1,234i');
+ expect(testFunction.calculate(grouped).getValue()).toBe(1234);
+
+ const spaced = StringValueObject.create(' 1+2i ');
+ expect(testFunction.calculate(spaced).getValue()).toBe(2);
+ });
+
+ it('Value is complex string with a part that is not a number', () => {
+ const hex = StringValueObject.create('1+0x10i');
+ expect(testFunction.calculate(hex).getValue()).toBe(ErrorType.NUM);
+
+ const infinity = StringValueObject.create('1+Infinity i');
+ expect(testFunction.calculate(infinity).getValue()).toBe(ErrorType.NUM);
+ });
+
it('Value is large numbers', () => {
const inumber = NumberValueObject.create(25698432);
const result = testFunction.calculate(inumber);
diff --git a/packages/engine-formula/src/functions/engineering/imaginary/index.ts b/packages/engine-formula/src/functions/engineering/imaginary/index.ts
index c58f6bed88..100be6aebc 100644
--- a/packages/engine-formula/src/functions/engineering/imaginary/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imaginary/index.ts
@@ -38,7 +38,7 @@ export class Imaginary extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imargument/index.ts b/packages/engine-formula/src/functions/engineering/imargument/index.ts
index 705b740e5c..f0342df4d7 100644
--- a/packages/engine-formula/src/functions/engineering/imargument/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imargument/index.ts
@@ -38,7 +38,7 @@ export class Imargument extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imconjugate/index.ts b/packages/engine-formula/src/functions/engineering/imconjugate/index.ts
index 59172cbd90..0905ac0cb0 100644
--- a/packages/engine-formula/src/functions/engineering/imconjugate/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imconjugate/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imconjugate extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imcos/index.ts b/packages/engine-formula/src/functions/engineering/imcos/index.ts
index 9dbc793117..a743e77eba 100644
--- a/packages/engine-formula/src/functions/engineering/imcos/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imcos/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imcos extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imcosh/index.ts b/packages/engine-formula/src/functions/engineering/imcosh/index.ts
index 555a887943..ed6e1c798d 100644
--- a/packages/engine-formula/src/functions/engineering/imcosh/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imcosh/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imcosh extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imcot/index.ts b/packages/engine-formula/src/functions/engineering/imcot/index.ts
index 8af2872c53..bfab038b25 100644
--- a/packages/engine-formula/src/functions/engineering/imcot/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imcot/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imcot extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imcoth/index.ts b/packages/engine-formula/src/functions/engineering/imcoth/index.ts
index dd92ecf044..4e94847304 100644
--- a/packages/engine-formula/src/functions/engineering/imcoth/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imcoth/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imcoth extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imcsc/index.ts b/packages/engine-formula/src/functions/engineering/imcsc/index.ts
index b3011ea6b9..166e8e6711 100644
--- a/packages/engine-formula/src/functions/engineering/imcsc/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imcsc/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imcsc extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imcsch/index.ts b/packages/engine-formula/src/functions/engineering/imcsch/index.ts
index 1aa182a064..dae8a85ccb 100644
--- a/packages/engine-formula/src/functions/engineering/imcsch/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imcsch/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imcsch extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imdiv/index.ts b/packages/engine-formula/src/functions/engineering/imdiv/index.ts
index e45027c0ad..646dbc12c6 100644
--- a/packages/engine-formula/src/functions/engineering/imdiv/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imdiv/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -44,8 +44,8 @@ export class Imdiv extends BaseFunction {
const inumber1Value = `${inumber1Object.getValue()}`;
const inumber2Value = `${inumber2Object.getValue()}`;
- const complex1 = new Complex(inumber1Value);
- const complex2 = new Complex(inumber2Value);
+ const complex1 = new Complex(inumber1Value, this.getDateSystem());
+ const complex2 = new Complex(inumber2Value, this.getDateSystem());
if (complex1.isError() || complex2.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imexp/index.ts b/packages/engine-formula/src/functions/engineering/imexp/index.ts
index 305a85f9be..3b52fd54d4 100644
--- a/packages/engine-formula/src/functions/engineering/imexp/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imexp/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imexp extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imln/index.ts b/packages/engine-formula/src/functions/engineering/imln/index.ts
index 5cb65abd11..c908d756ac 100644
--- a/packages/engine-formula/src/functions/engineering/imln/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imln/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imln extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imlog/index.ts b/packages/engine-formula/src/functions/engineering/imlog/index.ts
index 60a3dd2d97..91786bf808 100644
--- a/packages/engine-formula/src/functions/engineering/imlog/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imlog/index.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
+import { isRealNum } from '@univerjs/core';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean, checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
@@ -62,7 +62,7 @@ export class Imlog extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
const baseValue = +baseObject.getValue();
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imlog10/index.ts b/packages/engine-formula/src/functions/engineering/imlog10/index.ts
index e5f3851a74..a205aa1f98 100644
--- a/packages/engine-formula/src/functions/engineering/imlog10/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imlog10/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imlog10 extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imlog2/index.ts b/packages/engine-formula/src/functions/engineering/imlog2/index.ts
index 9551b97e84..457297d61a 100644
--- a/packages/engine-formula/src/functions/engineering/imlog2/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imlog2/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imlog2 extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/impower/index.ts b/packages/engine-formula/src/functions/engineering/impower/index.ts
index cc74fd88a0..9eefec5091 100644
--- a/packages/engine-formula/src/functions/engineering/impower/index.ts
+++ b/packages/engine-formula/src/functions/engineering/impower/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -43,7 +43,7 @@ export class Impower extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/improduct/index.ts b/packages/engine-formula/src/functions/engineering/improduct/index.ts
index a8a87fbd60..0e76846382 100644
--- a/packages/engine-formula/src/functions/engineering/improduct/index.ts
+++ b/packages/engine-formula/src/functions/engineering/improduct/index.ts
@@ -15,9 +15,9 @@
*/
import type { Nullable } from '@univerjs/core';
+import { isRealNum } from '@univerjs/core';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
@@ -86,7 +86,7 @@ export class Improduct extends BaseFunction {
const value = `${variant.getValue()}`;
if (typeof result !== 'number' && !result) {
- const complex = new Complex(value);
+ const complex = new Complex(value, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
@@ -94,8 +94,8 @@ export class Improduct extends BaseFunction {
_result = complex.toString();
} else {
- const complex1: Complex = new Complex(result);
- const complex2 = new Complex(value);
+ const complex1: Complex = new Complex(result, this.getDateSystem());
+ const complex2 = new Complex(value, this.getDateSystem());
if (complex1.isError() || complex2.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imreal/__tests__/index.spec.ts b/packages/engine-formula/src/functions/engineering/imreal/__tests__/index.spec.ts
index 2f1d0b569e..1eda3344d2 100644
--- a/packages/engine-formula/src/functions/engineering/imreal/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/engineering/imreal/__tests__/index.spec.ts
@@ -40,6 +40,16 @@ describe('Test imreal function', () => {
expect(result3.getValue()).toBe(0);
});
+ it('Value is complex string with a sign inside the imaginary part', () => {
+ const inumber = StringValueObject.create('1+2-3i');
+ const result = testFunction.calculate(inumber);
+ expect(result.getValue()).toBe(1);
+
+ const inumber2 = StringValueObject.create('1-2+3i');
+ const result2 = testFunction.calculate(inumber2);
+ expect(result2.getValue()).toBe(ErrorType.NUM);
+ });
+
it('Value is large numbers', () => {
const inumber = NumberValueObject.create(25698432);
const result = testFunction.calculate(inumber);
diff --git a/packages/engine-formula/src/functions/engineering/imreal/index.ts b/packages/engine-formula/src/functions/engineering/imreal/index.ts
index 16279ef963..54a4492482 100644
--- a/packages/engine-formula/src/functions/engineering/imreal/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imreal/index.ts
@@ -38,7 +38,7 @@ export class Imreal extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsec/index.ts b/packages/engine-formula/src/functions/engineering/imsec/index.ts
index 73a9a5cb28..4e755e7e1f 100644
--- a/packages/engine-formula/src/functions/engineering/imsec/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsec/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imsec extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsech/index.ts b/packages/engine-formula/src/functions/engineering/imsech/index.ts
index bd37452e80..215529cf87 100644
--- a/packages/engine-formula/src/functions/engineering/imsech/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsech/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imsech extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsin/index.ts b/packages/engine-formula/src/functions/engineering/imsin/index.ts
index 87b637f78f..17d8d93ec5 100644
--- a/packages/engine-formula/src/functions/engineering/imsin/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsin/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imsin extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsinh/index.ts b/packages/engine-formula/src/functions/engineering/imsinh/index.ts
index 6b401f837c..963a1edb86 100644
--- a/packages/engine-formula/src/functions/engineering/imsinh/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsinh/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imsinh extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsqrt/index.ts b/packages/engine-formula/src/functions/engineering/imsqrt/index.ts
index 37ae532f1a..aa9aed21c3 100644
--- a/packages/engine-formula/src/functions/engineering/imsqrt/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsqrt/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imsqrt extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsub/index.ts b/packages/engine-formula/src/functions/engineering/imsub/index.ts
index bee50b9e6c..3a6d2974b1 100644
--- a/packages/engine-formula/src/functions/engineering/imsub/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsub/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -44,8 +44,8 @@ export class Imsub extends BaseFunction {
const inumber1Value = `${inumber1Object.getValue()}`;
const inumber2Value = `${inumber2Object.getValue()}`;
- const complex1 = new Complex(inumber1Value);
- const complex2 = new Complex(inumber2Value);
+ const complex1 = new Complex(inumber1Value, this.getDateSystem());
+ const complex2 = new Complex(inumber2Value, this.getDateSystem());
if (complex1.isError() || complex2.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imsum/index.ts b/packages/engine-formula/src/functions/engineering/imsum/index.ts
index 746442895e..67681a971b 100644
--- a/packages/engine-formula/src/functions/engineering/imsum/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imsum/index.ts
@@ -15,9 +15,9 @@
*/
import type { Nullable } from '@univerjs/core';
+import { isRealNum } from '@univerjs/core';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
@@ -86,7 +86,7 @@ export class Imsum extends BaseFunction {
const value = `${variant.getValue()}`;
if (typeof result !== 'number' && !result) {
- const complex = new Complex(value);
+ const complex = new Complex(value, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
@@ -94,8 +94,8 @@ export class Imsum extends BaseFunction {
_result = complex.toString();
} else {
- const complex1: Complex = new Complex(result);
- const complex2 = new Complex(value);
+ const complex1: Complex = new Complex(result, this.getDateSystem());
+ const complex2 = new Complex(value, this.getDateSystem());
if (complex1.isError() || complex2.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imtan/index.ts b/packages/engine-formula/src/functions/engineering/imtan/index.ts
index 8b52b75634..ccb3c02766 100644
--- a/packages/engine-formula/src/functions/engineering/imtan/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imtan/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imtan extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);
diff --git a/packages/engine-formula/src/functions/engineering/imtanh/index.ts b/packages/engine-formula/src/functions/engineering/imtanh/index.ts
index ccfb0a3dec..5902a38cdd 100644
--- a/packages/engine-formula/src/functions/engineering/imtanh/index.ts
+++ b/packages/engine-formula/src/functions/engineering/imtanh/index.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { Complex } from '../../../basics/complex';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArrayOrBoolean } from '../../../engine/utils/check-variant-error';
@@ -39,7 +39,7 @@ export class Imtanh extends BaseFunction {
const inumberValue = `${inumberObject.getValue()}`;
- const complex = new Complex(inumberValue);
+ const complex = new Complex(inumberValue, this.getDateSystem());
if (complex.isError()) {
return ErrorValueObject.create(ErrorType.NUM);