-
Notifications
You must be signed in to change notification settings - Fork 9
/
complex.go
52 lines (42 loc) · 1.15 KB
/
complex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
// static inline int complexCheck(PyObject *o) { return PyComplex_Check(o); }
import "C"
import (
"fmt"
"unsafe"
)
type Complex struct {
AbstractObject
NumberProtocol
o C.PyComplexObject
}
// ComplexType is the Type object that represents the Complex type.
var ComplexType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyComplex_Type)))
func complexCheck(obj Object) bool {
return C.complexCheck(c(obj)) != 0
}
func newComplex(obj *C.PyObject) *Complex {
return (*Complex)(unsafe.Pointer(obj))
}
func NewComplex(v complex128) (*Complex, error) {
ret := C.PyComplex_FromDoubles(C.double(real(v)), C.double(imag(v)))
if ret == nil {
return nil, exception()
}
return newComplex(ret), nil
}
func (o *Complex) Complex128() complex128 {
r := float64(C.PyComplex_RealAsDouble(c(o)))
i := float64(C.PyComplex_ImagAsDouble(c(o)))
return complex(r, i)
}
func (c *Complex) String() string {
if c == nil {
return "<nil>"
}
return fmt.Sprintf("%v", c.Complex128())
}