当前位置: 首页>>代码示例>>Golang>>正文


Golang math.Trunc函数代码示例

本文整理汇总了Golang中math.Trunc函数的典型用法代码示例。如果您正苦于以下问题:Golang Trunc函数的具体用法?Golang Trunc怎么用?Golang Trunc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Trunc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestGeoLookup

func TestGeoLookup(t *testing.T) {
	na, err := NewNetAcuity("netacuity.sea1.office.priv:5400")
	if err != nil {
		t.Fatal(err)
	}

	na.SetTimeout(time.Second)

	geo, err := na.QueryGeo("74.125.239.110")
	if err != nil {
		t.Fatal(err)
	}

	if "usa" != geo.Country {
		t.Errorf("Expected usa == %s", geo.Country)
	}

	//Accuracy of 1 degree is good enough for lat/long
	lat := math.Trunc(geo.Latitude)
	if 37 != lat {
		t.Errorf("Expected 37 (37.389) == %v", lat)
	}

	long := math.Trunc(geo.Longitude)
	if -122 != long {
		t.Errorf("Expected -122 (122.075) == %v", long)
	}
}
开发者ID:TuneLab,项目名称:gonetacuity,代码行数:28,代码来源:netacuity_test.go

示例2: natural

func natural(n uint) uint {
	//
	randomNumber = productModM(randomNumber, a)
	randomNumber += c
	if randomNumber >= modulus {
		randomNumber -= modulus
	}
	if n == 0 {
		n = MaxNat
	}
	var r float64
	if n == MaxNat {
		r = maxNatF
	} else if n <= modulus {
		r = float64(n)
	} else {
		n -= modulus
		r = float64(n) + modulusF
	}
	r = (float64(randomNumber) / modulusF) * r
	if r <= modulusF {
		return uint(math.Trunc(r))
	}
	r -= modulusF
	return uint(math.Trunc(r)) + modulus
}
开发者ID:CaptainSoOmA,项目名称:Uni,代码行数:26,代码来源:imp.go

示例3: FloatPrecision

// FloatPrecision float指定精度; round为true时, 表示支持四舍五入
func FloatPrecision(f float64, prec int, round bool) float64 {
	pow10N := math.Pow10(prec)
	if round {
		return math.Trunc((f+0.5/pow10N)*pow10N) / pow10N
	}
	return math.Trunc((f)*pow10N) / pow10N
}
开发者ID:researchlab,项目名称:golearning,代码行数:8,代码来源:convert.go

示例4: next

func (p *pIterator) next() bool {
	if !(p.countToIdx < p.h.totalCount) {
		if p.seenLastValue {
			return false
		}

		p.seenLastValue = true
		p.percentile = 100

		return true
	}

	if p.subBucketIdx == -1 && !p.iterator.next() {
		return false
	}

	var done = false
	for !done {
		currentPercentile := (100.0 * float64(p.countToIdx)) / float64(p.h.totalCount)
		if p.countAtIdx != 0 && p.percentileToIteratorTo <= currentPercentile {
			p.percentile = p.percentileToIteratorTo
			halfDistance := math.Trunc(math.Pow(2, math.Trunc(math.Log2(100.0/(100.0-p.percentileToIteratorTo)))+1))
			percentileReportingTicks := float64(p.ticksPerHalfDistance) * halfDistance
			p.percentileToIteratorTo += 100.0 / percentileReportingTicks
			return true
		}
		done = !p.iterator.next()
	}

	return true
}
开发者ID:andradeandrey,项目名称:go-ipfs,代码行数:31,代码来源:hdr.go

示例5: fromLLtoPixel

func fromLLtoPixel(ll [2]float64, zoom uint64) [2]float64 {
	d := gp.zc[zoom]
	e := math.Trunc((d[0] + ll[0]*gp.Bc[zoom]) + 0.5)
	f := minmax(math.Sin(ll[1]*math.Pi/180.0), -0.9999, 0.9999)
	g := math.Trunc((d[1] + 0.5*math.Log((1+f)/(1-f))*-gp.Cc[zoom]) + 0.5)
	return [2]float64{e, g}
}
开发者ID:mikhailborodin,项目名称:gopnik,代码行数:7,代码来源:googleprojection.go

示例6: smoothedNoise

func smoothedNoise(x float64, y float64, seed int64) float64 {
	xint := int64(math.Trunc(x))
	yint := int64(math.Trunc(y))

	corners := (noise(xint-1, yint-1, seed) + noise(xint+1, yint-1, seed) + noise(xint-1, yint+1, seed) + noise(xint+1, yint+1, seed)) / 16
	sides := (noise(xint-1, yint, seed) + noise(xint+1, yint, seed) + noise(xint, yint-1, seed) + noise(xint, yint+1, seed)) / 8
	center := noise(xint, yint, seed) / 4
	return corners + sides + center
}
开发者ID:Xackery,项目名称:perlin,代码行数:9,代码来源:perlin.go

示例7: isPrime

func isPrime(nbr float64) bool {
	for i := math.Trunc(math.Sqrt(nbr)) + 1; i >= 2; i-- {
		var tmp float64 = nbr / i
		if tmp == math.Trunc(tmp) {
			return false
		}
	}
	return true
}
开发者ID:Mougatine,项目名称:Project_Euler_Golang,代码行数:9,代码来源:prob010.go

示例8: Val

func Val(a Any) uint {
	//
	var n uint = 1
	switch a.(type) {
	case Valuator:
		n = (a.(Valuator)).Val()
	case bool:
		if !a.(bool) {
			n = 0
		}
	case int8:
		i := a.(int8)
		if i < 0 {
			i = -i
		}
		n = uint(i)
	case int16:
		i := a.(int16)
		if i < 0 {
			i = -i
		}
		n = uint(i)
	case int32:
		i := a.(int32)
		if i < 0 {
			i = -i
		}
		n = uint(i)
	case int:
		i := a.(int)
		if i < 0 {
			i = -i
		}
		n = uint(i)
	case byte:
		n = uint(a.(byte))
	case uint16:
		n = uint(a.(uint16))
	case uint32:
		n = uint(a.(uint32))
	case uint:
		n = a.(uint)
	case float32:
		n = nat(math.Trunc(float64(a.(float32) + 0.5)))
	case float64:
		n = nat(math.Trunc(a.(float64) + 0.5))
	case complex64:
		c := a.(complex64)
		n = nat(math.Trunc(math.Sqrt(float64(real(c)*real(c)+imag(c)*imag(c))) + 0.5))
	case complex128:
		c := a.(complex128)
		n = nat(math.Trunc(math.Sqrt(real(c)*real(c)+imag(c)*imag(c)) + 0.5))
	case string:
		// TODO sum of bytes of the string ? Hash-Code ?
	}
	return n
}
开发者ID:CaptainSoOmA,项目名称:Uni,代码行数:57,代码来源:valuator.go

示例9: main

func main() {
	for k := math.Trunc(math.Sqrt(NBR)) + 1; k >= 2; k-- {
		var tmp float64 = NBR / k
		if tmp == math.Trunc(tmp) && isPrime(k) {
			fmt.Println("Largest prime factor = ", k)
			return
		}
	}

	fmt.Println("Error.")
}
开发者ID:Mougatine,项目名称:Project_Euler_Golang,代码行数:11,代码来源:prob003.go

示例10: durationToString

func durationToString(d time.Duration) string {
	hours := int(math.Trunc(d.Hours()))
	minutes := int(math.Trunc(d.Minutes())) - 60*hours
	seconds := int(d.Seconds()) % 60

	if hours > 0 {
		return fmt.Sprintf("%d:%d:%02d", hours, minutes, seconds)
	}

	return fmt.Sprintf("%d:%02d", minutes, seconds)
}
开发者ID:kalafut,项目名称:gmw,代码行数:11,代码来源:web.go

示例11: GetColor

func (c ColorCube) GetColor(x, y, z int) color.RGBA {
	if x > c.SideSize || y >= c.SideSize || z >= c.SideSize {
		panic("index out of range")
	}

	ratio := float64(MAX_SIDE_SIZE-1) / float64(c.SideSize-1)
	xIndex := uint8(math.Trunc(float64(x) * ratio))
	yIndex := uint8(math.Trunc(float64(y) * ratio))
	zIndex := uint8(math.Trunc(float64(z) * ratio))
	return color.RGBA{xIndex, yIndex, zIndex, 255}
}
开发者ID:nordsoyv,项目名称:colorDrawer,代码行数:11,代码来源:colorCube.go

示例12: scale

func scale(x, y float64) (int, int) {
	//
	rx, ry := int(math.Trunc(mX*(x-x0))+0.5), int(math.Trunc(mY*(y-y0))+0.5)
	ry = int(scr.NY()) - ry
	if scr.UnderX() {
		if minInt <= rx && rx < maxInt && minInt <= ry && ry < maxInt {
			return rx, ry
		}
	} else if x0 <= x && x < x0+width && y0 <= y && y < y0+height {
		return rx, ry
	}
	return maxInt, maxInt
}
开发者ID:CaptainSoOmA,项目名称:Uni,代码行数:13,代码来源:imp.go

示例13: interp2d

// interp2d returns noise for x,y interpolated from
// the given 2D noise function.
func interp2d(x, y float64, seed int64, interp func(a, b, x float64) float64) float64 {
	intx, fracx := int(x), x-math.Trunc(x)
	inty, fracy := int(y), y-math.Trunc(y)

	v1 := smooth2d(intx, inty, seed)
	v2 := smooth2d(intx+1, inty, seed)
	i1 := interp(v1, v2, fracx)

	v3 := smooth2d(intx, inty+1, seed)
	v4 := smooth2d(intx+1, inty+1, seed)
	i2 := interp(v3, v4, fracx)

	return interp(i1, i2, fracy)
}
开发者ID:vivounicorn,项目名称:eaburns,代码行数:16,代码来源:perlin.go

示例14: abc

func abc() {
	type T struct {
		a int
		b int
		c float64
	}

	type SliceHeader struct {
		addr uintptr
		len  int
		cap  int
	}

	t := &T{a: 1, b: 2, c: 3.2}
	p := unsafe.Sizeof(*t)
	println("t size:", int(p))
	fmt.Println("t value:", t)

	sl := &SliceHeader{
		addr: uintptr(unsafe.Pointer(t)),
		len:  int(p),
		cap:  int(p),
	}

	b := *(*[]byte)(unsafe.Pointer(sl))
	println("byte size: ", len(b))
	fmt.Println("byte content: ", b)

	b[0] = 12
	b[7] = 0
	b[8] = 24

	fmt.Println("last t value: ", t)
	fmt.Println("last byte content: ", b)

	ft := 10.1234567
	ftval := 0.0
	lastVal := math.Pow10(-4)
	addVal := math.Pow10(-5)
	fmt.Printf("add val: %f\n", addVal)
	tmp := math.Mod(math.Trunc(ft/addVal), 10)
	fmt.Printf("tmp val: %f\n", tmp)
	if tmp >= 5 {
		ftval = ft + lastVal
	} else {
		ftval = ft
	}
	fmt.Println(math.Trunc(ftval/lastVal) / math.Pow10(4))

}
开发者ID:sunvim,项目名称:codelab,代码行数:50,代码来源:byte.go

示例15: LeastSquares

// LeastSquares function
// A timeseries is anomalous if the average of the last three datapoints
// on a projected least squares model is greater than three sigma.
func LeastSquares(timeseries []TimePoint) bool {
	m, c := LinearRegressionLSE(timeseries)
	var errs []float64
	for _, val := range timeseries {
		projected := m*float64(val.GetTimestamp()) + c
		errs = append(errs, val.GetValue()-projected)
	}
	l := len(errs)
	if l < 3 {
		return false
	}
	stdDev := Std(errs)
	t := (errs[l-1] + errs[l-2] + errs[l-3]) / 3
	return math.Abs(t) > stdDev*3 && math.Trunc(stdDev) != 0 && math.Trunc(t) != 0
}
开发者ID:postfix,项目名称:skyline,代码行数:18,代码来源:algorithms.go


注:本文中的math.Trunc函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。