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(formula): read complex numbers whose parts use a signed exponent (#8167)
kevin9897a430c33e
diff --git a/packages/engine-formula/src/basics/__tests__/complex.spec.ts b/packages/engine-formula/src/basics/__tests__/complex.spec.tsnew file mode 100644index 0000000000..2639cbfe03--- /dev/null+++ b/packages/engine-formula/src/basics/__tests__/complex.spec.ts@@ -0,0 +1,50 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { describe, expect, it } from 'vitest';+import { Complex } from '../complex';++describe('Test complex', () => {+ it('Parses a real part written with an exponent', () => {+ const complex = new Complex('1e-7-i');++ expect(complex.isError()).toBe(false);+ expect(complex.getRealNum()).toBe(1e-7);+ expect(complex.getINum()).toBe(-1);+ });++ it('Parses exponents with an explicit sign in both parts', () => {+ const complex = new Complex('-1E+3-2e-3j');++ expect(complex.isError()).toBe(false);+ expect(complex.getRealNum()).toBe(-1000);+ expect(complex.getINum()).toBe(-0.002);+ expect(complex.getSuffix()).toBe('j');+ });++ it('Reads back what getComplex writes for a tiny real part', () => {+ const complex = Complex.createByComplexStr(1e-7, -1, 'i');++ expect(complex.isError()).toBe(false);+ expect(complex.getRealNum()).toBe(1e-7);+ expect(complex.getINum()).toBe(-1);+ });++ it('Keeps rejecting two separators', () => {+ expect(new Complex('1+2-3i').isError()).toBe(true);+ expect(new Complex('1-2+3i').isError()).toBe(true);+ });+});diff --git a/packages/engine-formula/src/basics/complex.ts b/packages/engine-formula/src/basics/complex.tsindex 6eb34dac9c..2875fe242c 100644--- a/packages/engine-formula/src/basics/complex.ts+++ b/packages/engine-formula/src/basics/complex.ts@@ -22,6 +22,21 @@ Decimal.prototype.cos = function (): Decimal {return new Decimal(Math.cos(num));};+/**+ * 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.+ */+function findSignIndex(inumberStr: string, sign: '+' | '-'): number {+ for (let i = 1; i < inumberStr.length; i++) {+ if (inumberStr[i] === sign && inumberStr[i - 1] !== 'e' && inumberStr[i - 1] !== 'E') {+ return i;+ }+ }++ return -1;+}+export class Complex {static getComplex(realNum: number, iNum: number, suffix: string): number | string {const _realNum = new Decimal(realNum).toSignificantDigits(15).toNumber();@@ -86,16 +101,8 @@ export class Complex {return;}- let plus = inumberStr.indexOf('+');- let minus = inumberStr.indexOf('-');-- if (plus === 0) {- plus = inumberStr.indexOf('+', 1);- }-- if (minus === 0) {- minus = inumberStr.indexOf('-', 1);- }+ const plus = findSignIndex(inumberStr, '+');+ const minus = findSignIndex(inumberStr, '-');const last = inumberStr.substring(inumberStr.length - 1, inumberStr.length);const unit = last === 'i' || last === 'j';@@ -155,16 +162,8 @@ export class Complex {inumberStr = inumberStr.replace('+i', '+1i').replace('-i', '-1i').replace('+j', '+1j').replace('-j', '-1j');- let plus = inumberStr.indexOf('+');- let minus = inumberStr.indexOf('-');-- if (plus === 0) {- plus = inumberStr.indexOf('+', 1);- }-- if (minus === 0) {- minus = inumberStr.indexOf('-', 1);- }+ const plus = findSignIndex(inumberStr, '+');+ const minus = findSignIndex(inumberStr, '-');const last = inumberStr.substring(inumberStr.length - 1, inumberStr.length);const unit = last === 'i' || last === 'j';