-
Notifications
You must be signed in to change notification settings - Fork 9
/
float.go
50 lines (40 loc) · 1.01 KB
/
float.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
// 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 floatCheck(PyObject *o) { return PyFloat_Check(o); }
import "C"
import (
"fmt"
"unsafe"
)
type Float struct {
AbstractObject
NumberProtocol
o C.PyFloatObject
}
// FloatType is the Type object that represents the Float type.
var FloatType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyFloat_Type)))
func floatCheck(obj Object) bool {
return C.floatCheck(c(obj)) != 0
}
func newFloat(obj *C.PyObject) *Float {
return (*Float)(unsafe.Pointer(obj))
}
func NewFloat(v float64) (*Float, error) {
ret := C.PyFloat_FromDouble(C.double(v))
if ret == nil {
return nil, exception()
}
return newFloat(ret), nil
}
func (f *Float) Float64() float64 {
return float64(C.PyFloat_AsDouble(c(f)))
}
func (f *Float) String() string {
if f == nil {
return "<nil>"
}
return fmt.Sprintf("%v", f.Float64())
}