本文整理汇总了Golang中math.Mod函数的典型用法代码示例。如果您正苦于以下问题:Golang Mod函数的具体用法?Golang Mod怎么用?Golang Mod使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Mod函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TurnGun
func (r *Robot) TurnGun(degrees float64) {
d := clamp(degrees, -20, 20)
r.State.GunHeading += d
r.State.RadarHeading += d
r.State.GunHeading = math.Mod(r.State.GunHeading, 360)
r.State.RadarHeading = math.Mod(r.State.RadarHeading, 360)
}
示例2: main
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var i float64
//'scanner.Text()' represents the test case, do something with it
bounds := strings.Split(scanner.Text(), " ")
fizz, _ := strconv.ParseFloat(bounds[0], 64)
buzz, _ := strconv.ParseFloat(bounds[1], 64)
stop, _ := strconv.ParseFloat(bounds[2], 64)
for i = 1; i <= stop; i++ {
var output string
if math.Mod(i, fizz) == 0 {
output = "F"
}
if math.Mod(i, buzz) == 0 {
output = output + "B"
}
if len(output) == 0 {
output = strconv.Itoa(int(i))
}
fmt.Print(output + " ")
}
fmt.Print("\n")
}
}
示例3: GetKeyOrRelation
func (d *DICT3) GetKeyOrRelation(tnH TripletAndHash, resp *Response) error {
if tnH.T.Key == "" {
rel := tnH.T.Relationship
for kr, val := range myDictionary {
rHash := hashIt(kr.Relationship, 2)
kHash := hashIt(kr.Key, 3)
krHash, _ := strconv.Atoi(strconv.Itoa(kHash) + strconv.Itoa(rHash))
krHash = int(math.Mod(float64(krHash), float64(32)))
if kr.Relationship == rel && tnH.H == krHash {
resp.Reply.Result = Triplet{kr.Key, kr.Relationship, val}
}
}
} else {
key := tnH.T.Key
for kr, val := range myDictionary {
rHash := hashIt(kr.Relationship, 2)
kHash := hashIt(kr.Key, 3)
krHash, _ := strconv.Atoi(strconv.Itoa(kHash) + strconv.Itoa(rHash))
krHash = int(math.Mod(float64(krHash), float64(32)))
if kr.Key == key && tnH.H == krHash {
resp.Reply.Result = Triplet{kr.Key, kr.Relationship, val}
}
}
}
return nil
}
示例4: init
func init() {
Core.Function("%", func(env *Env, args []Value) (result Value) {
CheckArgs("%", 1, -1, args)
result = args[0]
for _, arg := range args[1:] {
switch a := result.(type) {
case Int:
switch b := arg.(type) {
case Int:
result = Int(a % b)
case Float:
result = Float(math.Mod(float64(a), float64(b)))
default:
panic("bad-type")
}
case Float:
switch b := args[1].(type) {
case Int:
result = Float(math.Mod(float64(a), float64(b)))
case Float:
result = Float(math.Mod(float64(a), float64(b)))
default:
panic("bad-type")
}
default:
panic("bad-type")
}
}
return
})
}
示例5: isPrime
// This function will eventually get it's own module.
func isPrime(t int64) bool {
// math.Mod requires floats.
x := float64(t)
// 1 or less aren't primes.
if x <= 1 {
return false
}
// Solve half of the integer set directly
if math.Mod(x, 2) == 0 {
return x == 2
}
// Main loop. i needs to be float because of math.Mod.
for i := 3.0; i <= math.Floor(math.Sqrt(x)); i += 2.0 {
if math.Mod(x, i) == 0 {
return false
}
}
// It's a prime!
return true
}
示例6: Color
/**
*
* Set forground and background color.
*
* integer f Forground color value 0-15
* integer b background color value 0-7
*
*/
func Color(f int, b int) {
var tmp string
var colors = [8]int{0, 4, 2, 6, 1, 5, 3, 7}
// Prevent sending color codes if they haven't changed.
if curFg != f || curBg != b {
curFg = f
curBg = b
fg := colors[int(math.Mod(float64(f), 8))] + 30
bg := colors[int(math.Mod(float64(b), 8))] + 40
if b > 7 {
tmp += "5;"
} else {
tmp += "0;"
}
if f > 7 {
tmp += "1;"
}
fmt.Printf(ESC+"%s%d;%dm", tmp, fg, bg)
}
}
示例7: parseQPSChangeFunc
// parseQPSChangeFunc augments a recipe with the pointer to a Go function
// that actually implements the recipe function. If the function specified
// in --recipes is unknown or an incorrect number of arguments were passed,
// it returns false, otherwise not false.
func parseQPSChangeFunc(r *Recipe) bool {
switch r.name {
case "constant_increase":
checkArg(r, 1)
r.fun = func(w *WorkerState) {
w.CurrentQPS += w.Recipe.arg[0]
}
return true
case "random_change":
checkArg(r, 1)
r.fun = func(w *WorkerState) {
w.CurrentQPS = w.Recipe.baseQPS + w.Recipe.arg[0]*(1.0-2.0*rand.Float64())
}
return true
case "sin":
checkArg(r, 1)
r.fun = func(w *WorkerState) {
t := math.Mod(time.Now().Sub(startingTime).Seconds(), recipeReset.Seconds())
w.CurrentQPS = w.Recipe.arg[0] * math.Sin(t/recipeReset.Seconds()*math.Pi)
}
return true
case "inc_sin":
checkArg(r, 1)
r.fun = func(w *WorkerState) {
t := math.Mod(time.Now().Sub(startingTime).Seconds(), recipeReset.Seconds())
w.CurrentQPS = float64(w.resetCount) * w.Recipe.arg[0] * math.Sin(t/recipeReset.Seconds()*math.Pi)
}
return true
}
return false
}
示例8: ExamplePMod_integer
// Section "Trigonometric functions of large angles":
//
// Meeus makes his point, but an example with integer values is a bit unfair
// when trigonometric functions inherently work on floating point numbers.
func ExamplePMod_integer() {
const large = 36000030
// The direct function call loses precition as expected.
fmt.Println("Direct: ", math.Sin(large*math.Pi/180))
// When the value is manually reduced to the integer 30, the Go constant
// evaluaton does a good job of delivering a radian value to math.Sin that
// evaluates to .5 exactly.
fmt.Println("Integer 30:", math.Sin(30*math.Pi/180))
// Math.Mod takes float64s and returns float64s. The integer constants
// here however can be represented exactly as float64s, and the returned
// result is exact as well.
fmt.Println("Math.Mod: ", math.Mod(large, 360))
// But when math.Mod is substituted into the Sin function, float64s
// are multiplied instead of the high precision constants, and the result
// comes back slightly inexact.
fmt.Println("Sin Mod: ", math.Sin(math.Mod(large, 360)*math.Pi/180))
// Use of PMod on integer constants produces results identical to above.
fmt.Println("PMod int: ", math.Sin(base.PMod(large, 360)*math.Pi/180))
// As soon as the large integer is scaled to a non-integer value though,
// precision is lost and PMod is of no help recovering at this point.
fmt.Println("PMod float:", math.Sin(base.PMod(large*math.Pi/180, 2*math.Pi)))
// Output:
// Direct: 0.49999999995724154
// Integer 30: 0.5
// Math.Mod: 30
// Sin Mod: 0.49999999999999994
// PMod int: 0.49999999999999994
// PMod float: 0.49999999997845307
}
示例9: main
func main() {
var (
nbLigne uint
result [][]int
r *rand.Rand
i uint
)
nbLigne = Initialisation()
result = make([][]int, nbLigne)
r = rand.New(rand.NewSource(time.Now().UnixNano()))
for i = 0; i < nbLigne; i++ {
result[i] = make([]int, 3)
result[i][0] = int(math.Mod(float64(r.Int()), 200)) - 100
result[i][1] = int(math.Mod(float64(r.Int()), 200)) - 100
result[i][2] = int(math.Mod(float64(r.Int()), 9)) + 1
}
ecrireFichier("fichierTest", nbLigne, result)
}
示例10: disectUsageForCCR
// Returns reqType, requestNr and ccTime in seconds
func disectUsageForCCR(usage time.Duration, debitInterval time.Duration, callEnded bool) (reqType, reqNr, reqCCTime, usedCCTime int) {
usageSecs := usage.Seconds()
debitIntervalSecs := debitInterval.Seconds()
reqType = 1
if usage > 0 {
reqType = 2
}
if callEnded {
reqType = 3
}
reqNr = int(usageSecs / debitIntervalSecs)
if callEnded {
reqNr += 1
}
ccTimeFloat := debitInterval.Seconds()
if callEnded {
ccTimeFloat = math.Mod(usageSecs, debitIntervalSecs)
}
if reqType == 1 { // Initial does not have usedCCTime
reqCCTime = int(ccTimeFloat)
} else if reqType == 2 {
reqCCTime = int(ccTimeFloat)
usedCCTime = int(math.Mod(usageSecs, debitIntervalSecs))
} else if reqType == 3 {
usedCCTime = int(ccTimeFloat) // Termination does not have requestCCTime
}
return
}
示例11: main
func main() {
f, _ := os.Open(os.Args[1])
r := bufio.NewReader(f)
for {
l, err := r.ReadString('\n')
if err != nil {
break
}
l = strings.TrimSpace(l)
nums := strings.Split(l, " ")
a, _ := strconv.ParseInt(strings.TrimSpace(nums[0]), 0, 0)
b, _ := strconv.ParseInt(strings.TrimSpace(nums[1]), 0, 0)
n, _ := strconv.ParseInt(strings.TrimSpace(nums[2]), 0, 0)
str := ""
for j := int64(0); j < n; j++ {
modA := math.Mod(float64(j+1), float64(a))
modB := math.Mod(float64(j+1), float64(b))
if modA == 0 && modB == 0 {
str += "FB "
} else if modA == 0 {
str += "F "
} else if modB == 0 {
str += "B "
} else {
str += fmt.Sprintf("%d ", j+1)
}
}
fmt.Println(strings.TrimSpace(str))
}
}
示例12: RoundDuration
// returns a number equal or larger than the amount that exactly
// is divisible to whole
func RoundDuration(whole, amount time.Duration) time.Duration {
a, w := float64(amount), float64(whole)
if math.Mod(a, w) == 0 {
return amount
}
return time.Duration((w - math.Mod(a, w)) + a)
}
示例13: FindTexSize2D
func FindTexSize2D(maxSize, numTexels, minSize float64) (float64, float64) {
var wh float64
if math.Floor(numTexels) != numTexels {
log.Panicf("AAAAH %v", numTexels)
}
if numTexels <= maxSize {
return numTexels, 1
}
for h := 2.0; h < maxSize; h++ {
for w := 2.0; w < maxSize; w++ {
wh = w * h
if wh == numTexels {
if minSize > 0 {
for (math.Mod(w, 2) == 0) && (math.Mod(h, 2) == 0) && ((w / 2) >= minSize) && ((h / 2) >= minSize) {
w, h = w/2, h/2
}
}
for ((h * 2) < w) && (math.Mod(w, 2) == 0) {
w, h = w/2, h*2
}
if minSize > 0 {
for (math.Mod(w, 2) == 0) && (math.Mod(h, 2) == 0) && ((w / 2) >= minSize) && ((h / 2) >= minSize) {
w, h = w/2, h/2
}
}
return w, h
} else if wh > numTexels {
break
}
}
}
return 0, 0
}
示例14: move
func move(x, y int) {
if isHeld {
dx, dy := float64(x-ox), float64(y-oy)
s := 100.0
dt, dp := dy/s, dx/s
theta, phi = math.Mod(ot+dt, math.Pi), math.Mod(op+dp, math.Pi*2)
}
}
示例15: evalMod
func (o *BinaryOperation) evalMod(a interface{}, b interface{}) (interface{}, error) {
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return x % y, nil
case uint64:
if y == 0 {
return nil, nil
} else if x < 0 {
// TODO: check overflow
return -int64(uint64(-x) % y), nil
}
// TODO: check overflow
return uint64(x) % y, nil
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
} else if y < 0 {
// TODO: check overflow
return -int64(x % uint64(-y)), nil
}
// TODO: check overflow
return x % uint64(y), nil
case uint64:
if y == 0 {
return nil, nil
}
return x % y, nil
}
case float64:
switch y := b.(type) {
case float64:
if y == 0 {
return nil, nil
}
return math.Mod(x, y), nil
}
case mysql.Decimal:
switch y := b.(type) {
case mysql.Decimal:
xf, _ := x.Float64()
yf, _ := y.Float64()
if yf == 0 {
return nil, nil
}
return math.Mod(xf, yf), nil
}
}
return types.InvOp2(a, b, opcode.Mod)
}