package bignum import ( "math/big" "testing" "github.com/stretchr/testify/require" ) func TestComplexMultiplierQuo(t *testing.T) { const prec = 128 newComplex := func(re, im float64) *Complex { return &Complex{ new(big.Float).SetPrec(prec).SetFloat64(re), new(big.Float).SetPrec(prec).SetFloat64(im), } } f := func(v float64) *big.Float { return new(big.Float).SetPrec(prec).SetFloat64(v) } cm := NewComplexMultiplier() for _, tc := range []struct { name string a, b *Complex wantRe, wantIm float64 }{ // Real numerator, non-real denominator: the previously broken branch. {"1/(1+i)", newComplex(1, 0), newComplex(1, 1), 0.5, -0.5}, {"1/i", newComplex(1, 0), newComplex(0, 1), 0, -1}, {"0/i", newComplex(0, 0), newComplex(0, 1), 0, 0}, // must not panic // General complex division (regression guard for the other branch). {"(2+3i)/(1+i)", newComplex(2, 3), newComplex(1, 1), 2.5, 0.5}, } { c := newComplex(0, 0) cm.Quo(tc.a, tc.b, c) require.Zerof(t, c[0].Cmp(f(tc.wantRe)), "%s real: got %v, want %v", tc.name, c[0], tc.wantRe) require.Zerof(t, c[1].Cmp(f(tc.wantIm)), "%s imag: got %v, want %v", tc.name, c[1], tc.wantIm) } }