forked from asticode/go-astiav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rational.go
52 lines (42 loc) · 927 Bytes
/
rational.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
package astiav
//#cgo pkg-config: libavutil
//#include <libavutil/rational.h>
import "C"
import "strconv"
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavutil/rational.h#L58
type Rational struct {
c C.struct_AVRational
}
func newRationalFromC(c C.struct_AVRational) Rational {
return Rational{c: c}
}
func NewRational(num, den int) Rational {
var r Rational
r.SetNum(num)
r.SetDen(den)
return r
}
func (r Rational) Num() int {
return int(r.c.num)
}
func (r *Rational) SetNum(num int) {
r.c.num = C.int(num)
}
func (r Rational) Den() int {
return int(r.c.den)
}
func (r *Rational) SetDen(den int) {
r.c.den = C.int(den)
}
func (r Rational) ToDouble() float64 {
if r.Num() == 0 || r.Den() == 0 {
return 0
}
return float64(r.Num()) / float64(r.Den())
}
func (r Rational) String() string {
if r.Num() == 0 || r.Den() == 0 {
return "0"
}
return strconv.Itoa(r.Num()) + "/" + strconv.Itoa(r.Den())
}