| 1 |
// Package v4l, a facade to the Video4Linux video capture interface |
| 2 |
// Copyright (C) 2016 Zoltán Korándi <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
//go:build linux |
| 18 |
// +build linux |
| 19 |
|
| 20 |
package v4l |
| 21 |
|
| 22 |
// A Frac represents the fractional number N/D. |
| 23 |
type Frac struct { |
| 24 |
N uint32 |
| 25 |
D uint32 |
| 26 |
} |
| 27 |
|
| 28 |
// Cmp returns -1, 0, or 1 if f is less then, equal to, or greater than g, |
| 29 |
// respectively. |
| 30 |
func (f Frac) Cmp(g Frac) int { |
| 31 |
F := uint64(f.N) * uint64(g.D) |
| 32 |
G := uint64(g.N) * uint64(f.D) |
| 33 |
switch { |
| 34 |
case F < G: |
| 35 |
return -1 |
| 36 |
case F > G: |
| 37 |
return 1 |
| 38 |
default: |
| 39 |
return 0 |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Reduce returns f reduced to lowest terms. |
| 44 |
func (f Frac) Reduce() Frac { |
| 45 |
gcd, r := f.N, f.D |
| 46 |
for r != 0 { |
| 47 |
gcd, r = r, gcd%r |
| 48 |
} |
| 49 |
if gcd == 0 { |
| 50 |
gcd = 1 |
| 51 |
} |
| 52 |
return Frac{f.N / gcd, f.D / gcd} |
| 53 |
} |