github.com/neugram/ng@v0.0.0-20180309130942-d472ff93d872/internal/bigcplx/big.go (about) 1 // Copyright 2017 The Neugram Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package bigcplx 6 7 import "math/big" 8 9 type Complex struct { 10 Real *big.Float 11 Imag *big.Float 12 } 13 14 func New(v complex128) *Complex { 15 return &Complex{ 16 Real: big.NewFloat(real(v)), 17 Imag: big.NewFloat(imag(v)), 18 } 19 } 20 21 func (c *Complex) Set(x *Complex) *Complex { 22 c.Real.Set(x.Real) 23 c.Imag.Set(x.Imag) 24 return c 25 } 26 27 func (c *Complex) SetComplex128(x complex128) *Complex { 28 c.Real.SetFloat64(real(x)) 29 c.Imag.SetFloat64(imag(x)) 30 return c 31 } 32 33 func (c *Complex) String() string { 34 op := "+" 35 if c.Imag.Sign() < 0 { 36 op = "" // minus sign will be handled by c.Imag.String() 37 } 38 return "(" + c.Real.String() + op + c.Imag.String() + "i)" 39 }