-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfloat.go
More file actions
40 lines (34 loc) · 683 Bytes
/
float.go
File metadata and controls
40 lines (34 loc) · 683 Bytes
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
package py
// #include "utils.h"
import "C"
import (
"fmt"
)
func NewFloat[T ~float32 | ~float64](f T) (*Float, error) {
ret := C.PyFloat_FromDouble(C.double(float64(f)))
if ret == nil {
return nil, exception()
}
return newFloat(ret), nil
}
func NewFloatString(v string) (*Float, error) {
s, err := NewUnicode(v)
if err != nil {
return nil, err
}
defer s.Decref()
ret := C.PyFloat_FromString(c(s))
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())
}