-
Notifications
You must be signed in to change notification settings - Fork 11
/
mod.go
73 lines (65 loc) · 1.59 KB
/
mod.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package math
import (
"math"
"math/bits"
)
func Mod(a, b int) int {
if a < 0 {
return b - 1 - ^a%b
}
return a % b
}
func Div(a, b int) int {
if a < 0 {
return ^(^a / b)
}
return a / b
}
func Rint(f float64) int {
return int(math.RoundToEven(f))
}
// mulFracInt64 returns a*b/d rounded to even.
func mulFracInt64(a, b, d int64) int64 {
sign := int64(1)
if a < 0 {
sign, a = -sign, -a
}
if b < 0 {
sign, b = -sign, -b
}
if d < 0 {
sign, d = -sign, -d
}
q, r := mulFracModUint64(uint64(a), uint64(b), uint64(d))
q = roundToEvenUint64(uint64(d), uint64(q), uint64(r))
return int64(q) * sign
}
// mulFracModUint64 returns a*b/d rounded to even.
func mulFracModUint64(a, b, d uint64) (uint64, uint64) {
ch, cl := bits.Mul64(a, b)
return bits.Div64(ch, cl, d)
}
// roundToEvenUint64 rounds q with remainder r at divisor d towards nearest, and towards even on a tie.
func roundToEvenUint64(d, q, r uint64) uint64 {
rcut := d / 2
if q%2 == 0 && d%2 == 0 {
rcut++
}
if r >= rcut {
q++
}
return q
}