Skip to content

Commit

Permalink
テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Aug 5, 2023
1 parent 754130d commit 38d3875
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
25 changes: 11 additions & 14 deletions packages/common/src/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import {DetailedError} from './detailed-error'
import {Format} from './format'
import {Util} from './util'

Expand Down Expand Up @@ -42,24 +41,17 @@ export class Complex {
return v.real
}

static polar(magnitude: number, phase: number): Complex {
const [cos, sin] = Util.snappedCosSin(phase)
return new Complex(magnitude * cos, magnitude * sin)
}

static imagPartOf(v: number | Complex): number {
if (v instanceof Complex) {
return v.imag
}
if (typeof v === 'number') {
return 0
return v
}
throw new DetailedError('Unrecognized value type.', {v})

return v.imag
}

constructor(real: number, imag: number) {
this.real = real
this.imag = imag
static polar(magnitude: number, phase: number): Complex {
const [cos, sin] = Util.snappedCosSin(phase)
return new Complex(magnitude * cos, magnitude * sin)
}

static rootsOfQuadratic(a: number | Complex, b: number | Complex, c: number | Complex): Complex[] {
Expand All @@ -83,6 +75,11 @@ export class Complex {
return difs.map(d => mid.minus(d).dividedBy(denom))
}

constructor(real: number, imag: number) {
this.real = real
this.imag = imag
}

isEqualTo(other: unknown): boolean {
if (other instanceof Complex) {
return this.real === other.real && this.imag === other.imag
Expand Down
38 changes: 38 additions & 0 deletions packages/common/test/complex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ describe('Complex', () => {
})
})

describe('imagPartOf', () => {
describe('1', () => {
test('equal 0', () => {
expect(equate(Complex.imagPartOf(1), 0))
})
})

describe('1.5', () => {
test('equal 0', () => {
expect(equate(Complex.imagPartOf(1.5), 0))
})
})

describe('-2', () => {
test('equal 0', () => {
expect(equate(Complex.imagPartOf(-2), 0))
})
})

describe('3+0i', () => {
test('equal 0', () => {
expect(equate(Complex.imagPartOf(new Complex(3, 0)), 0))
})
})

describe('3+i', () => {
test('equal 1', () => {
expect(equate(Complex.imagPartOf(new Complex(3, 1)), 1))
})
})

describe('5-2i', () => {
test('equal -2', () => {
expect(equate(Complex.imagPartOf(new Complex(5, -2)), -2))
})
})
})

describe('polar', () => {
describe('0,0', () => {
beforeEach(() => {
Expand Down

0 comments on commit 38d3875

Please sign in to comment.