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


Golang math.Ceil函数代码示例

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


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

示例1: handleKeyEvent

func (p *Pager) handleKeyEvent(ev termbox.Event) bool {
	p.width, p.height = termbox.Size()
	switch ev.Key {
	case termbox.KeyCtrlN, termbox.KeyArrowDown, termbox.KeyEnter:
		p.scrollDown(1)
	case termbox.KeyCtrlP, termbox.KeyArrowUp:
		p.scrollUp(1)
	case termbox.KeyCtrlU:
		p.scrollUp(int(math.Ceil(float64(p.height) / 2)))
	case termbox.KeyCtrlD:
		p.scrollDown(int(math.Ceil(float64(p.height) / 2)))
	case termbox.KeyCtrlF, termbox.KeySpace, termbox.KeyPgdn:
		p.scrollDown(max(0, p.height-3))
	case termbox.KeyCtrlB, termbox.KeyPgup:
		p.scrollUp(max(0, p.height-3))
	default:
		switch ev.Ch {
		case 'j':
			p.scrollDown(1)
		case 'k':
			p.scrollUp(1)
		case 'g':
			p.viewX = 0
		case 'G':
			p.viewX = max(0, len(p.lines)-p.height)
		case 'q':
			return false
		}
	}
	p.Redraw()
	return true
}
开发者ID:yosisa,项目名称:least,代码行数:32,代码来源:main.go

示例2: Round

// Round return rounded version of x with prec precision.
//
// Special cases are:
//	Round(±0) = ±0
//	Round(±Inf) = ±Inf
//	Round(NaN) = NaN
func Round(x float64, prec int, method string) float64 {
	var rounder float64
	maxPrec := 7 // define a max precison to cut float errors
	if maxPrec < prec {
		maxPrec = prec
	}
	pow := math.Pow(10, float64(prec))
	intermed := x * pow
	_, frac := math.Modf(intermed)

	switch method {
	case ROUNDING_UP:
		if frac >= math.Pow10(-maxPrec) { // Max precision we go, rest is float chaos
			rounder = math.Ceil(intermed)
		} else {
			rounder = math.Floor(intermed)
		}
	case ROUNDING_DOWN:
		rounder = math.Floor(intermed)
	case ROUNDING_MIDDLE:
		if frac >= 0.5 {
			rounder = math.Ceil(intermed)
		} else {
			rounder = math.Floor(intermed)
		}
	default:
		rounder = intermed
	}

	return rounder / pow
}
开发者ID:cgrates,项目名称:cgrates,代码行数:37,代码来源:coreutils.go

示例3: Ceil

func (v3 *Vector3) Ceil() *Vector3 {
	v3.X = math.Ceil(v3.X)
	v3.Y = math.Ceil(v3.Y)
	v3.Z = math.Ceil(v3.Z)

	return v3
}
开发者ID:uzudil,项目名称:three.go,代码行数:7,代码来源:Vector3.go

示例4: GridScale

// GridScale determines what scale should be used on the graph, for
// both the X and Y axes
func (n *New) GridScale(column string, gridCount float64) float64 {
	xsort := make([]float64, len(n.Xvalues))
	ysort := make([]float64, len(n.Yvalues))
	copy(xsort, n.Xvalues)
	copy(ysort, n.Yvalues)
	sort.Float64s(xsort)
	sort.Float64s(ysort)
	// Courtesy: Stack Overflow
	microAdjust := func(span, grids float64) float64 {
		width := (span / (grids - 1))
		x := math.Ceil(math.Log10(width) - 1)
		p := math.Pow(10, x)
		rspace := math.Ceil(width/p) * p
		return rspace
	}

	switch column {
	case "X":
		rangeX := n.DataRange("X")
		return microAdjust(rangeX, gridCount)
	case "Y":
		rangeY := n.DataRange("Y")
		return microAdjust(rangeY, gridCount)
	default:
		return 0
	}
}
开发者ID:enodev0,项目名称:rat,代码行数:29,代码来源:reg.go

示例5: TestLimiterRedis

// TestLimiterRedis tests Limiter with Redis store.
func TestLimiterRedis(t *testing.T) {
	rate, err := NewRateFromFormatted("3-M")
	assert.Nil(t, err)

	store, err := NewRedisStoreWithOptions(
		newRedisPool(),
		StoreOptions{Prefix: "limitertests:redis", MaxRetry: 3})

	assert.Nil(t, err)

	limiter := NewLimiter(store, rate)

	i := 1
	for i <= 5 {
		ctx, err := limiter.Get("boo")
		assert.Nil(t, err)

		if i <= 3 {
			assert.Equal(t, int64(3), ctx.Limit)
			assert.Equal(t, int64(3-i), ctx.Remaining)
			assert.True(t, math.Ceil(time.Since(time.Unix(ctx.Reset, 0)).Seconds()) <= 60)

		} else {
			assert.Equal(t, int64(3), ctx.Limit)
			assert.True(t, ctx.Remaining == 0)
			assert.True(t, math.Ceil(time.Since(time.Unix(ctx.Reset, 0)).Seconds()) <= 60)
		}

		i++
	}
}
开发者ID:wppurking,项目名称:limiter,代码行数:32,代码来源:limiter_test.go

示例6: Crop

func (i *Image) Crop(dst, format string, w, h, q int, optimize bool) error {
	cw, ch := w, h
	kc := float64(w) / float64(h)
	ki := float64(i.Width) / float64(i.Height)

	if ki > kc {
		ch = i.Height
		cw = int(math.Ceil(float64(i.Height) * kc))
	} else {
		cw = i.Width
		ch = int(math.Ceil(float64(i.Width) / kc))
	}

	crop := fmt.Sprintf("%dx%d+0+0", cw, ch)
	cmd := exec.Command("convert", i.Filename, "-gravity", "Center", "-crop", crop, dst)

	res, err := cmd.CombinedOutput()
	if err != nil {
		e := parseError(string(res))
		if e != "" {
			err = fmt.Errorf("%s", e)
		}
		return err
	}
	i.Filename = dst

	err = i.Resize(dst, format, w, h, q, optimize)
	if err != nil {
		return err
	}
	i.Width = w
	i.Height = h
	return nil
}
开发者ID:cheggaaa,项目名称:Anteater,代码行数:34,代码来源:image.go

示例7: updateBounds

func (g *group) updateBounds(move bool) {
	if g.model == nil {
		return
	}
	var w, h float64
	x0, y0, x1, y1 := detectBounds(g.model.Items())
	if !g.folded {
		w, h = (x1-x0)+2*GroupMargin, (y1-y0)+2*GroupMargin
		k := math.Ceil(w / GridDefaultGap)
		w = k * GridDefaultGap
		k = math.Ceil(h / GridDefaultGap)
		h = k * GridDefaultGap
	}
	fw, fh := g.getFoldedSize()
	if w < fw {
		w = fw
	}
	if h < fh {
		h = fh
	}
	if move {
		x, y := x0-GroupMargin, y0-GroupMargin
		g.Rect = geometry.NewRect(x, y, w, h)
	} else {
		g.Rect.Resize(w, h)
	}
}
开发者ID:LonelyPale,项目名称:teg-workshop,代码行数:27,代码来源:model.go

示例8: calcDrops

// If k == 2
// Solves d + (d - 1) + (d - 2) + (d - 3) + (d - 4) + ... + 1 = n
// This comes to d(d + 1) / 2 = n and then to
// d ** 2 + d - 2 * n = 0, so we solve this quadratic equoation to get d
// If k > 2, we check if it's large enough to do a binary search
//   If yes, we return the number of drops a binary search requires
//   If not, we use nFloors. d is the smalles number such that nFloors(d, k) >= n
func calcDrops(k, n int, cache *map[complex128]int) (d int) {
	need2binarySearch := int(math.Ceil(math.Log2(float64(n))))
	if k > need2binarySearch {
		return need2binarySearch
	}

	if k == 2 {
		//fmt.Println(">>>", k - 2, n, k)
		delta := 1 + 4*2*n
		floatD := (-1 + math.Sqrt(float64(delta))) / 2
		d = int(math.Ceil(floatD)) + (k - 2)
		return d
	}

	// If none the above, search for d such that nFloors(d, k) >= n
	//fmt.Println("Recurrence")
	d = 1
	for {
		nf := nFloors(d, k, cache)
		if nf >= n {
			return d
		}
		d += 1
	}
}
开发者ID:JeevFresno,项目名称:codeeval,代码行数:32,代码来源:cracking_eggs.go

示例9: NewSketchForEpsilonDelta

/*
NewSketchForEpsilonDelta ...
*/
func NewSketchForEpsilonDelta(epsilon, delta float64) (*Sketch, error) {
	var (
		width = uint(math.Ceil(math.E / epsilon))
		depth = uint(math.Ceil(math.Log(1 / delta)))
	)
	return NewSketch(width, depth, true, 1.00026, true, true, 16)
}
开发者ID:leoliuzcl,项目名称:skizze,代码行数:10,代码来源:log.go

示例10: Round

// Round returns a point inside the box, making an effort to round to minimal
// precision.
func (b Box) Round() (lat, lng float64) {
	x := maxDecimalPower(b.MaxLat - b.MinLat)
	lat = math.Ceil(b.MinLat/x) * x
	x = maxDecimalPower(b.MaxLng - b.MinLng)
	lng = math.Ceil(b.MinLng/x) * x
	return
}
开发者ID:mmcloughlin,项目名称:geohash,代码行数:9,代码来源:geohash.go

示例11: ComputeCrop

func (this *ThumbFile) ComputeCrop(original *UploadedFile) (int, int, error) {
	re := regexp.MustCompile("(.*):(.*)")
	matches := re.FindStringSubmatch(this.CropRatio)
	if len(matches) != 3 {
		return 0, 0, errors.New("Invalid crop_ratio")
	}

	wRatio, werr := strconv.ParseFloat(matches[1], 64)
	hRatio, herr := strconv.ParseFloat(matches[2], 64)
	if werr != nil || herr != nil {
		return 0, 0, errors.New("Invalid crop_ratio")
	}

	var cropWidth, cropHeight float64

	if wRatio >= hRatio {
		wRatio = wRatio / hRatio
		hRatio = 1
		cropWidth = math.Ceil(float64(this.ComputeHeight(original)) * wRatio)
		cropHeight = math.Ceil(float64(this.ComputeHeight(original)) * hRatio)
	} else {
		hRatio = hRatio / wRatio
		wRatio = 1
		cropWidth = math.Ceil(float64(this.ComputeWidth(original)) * wRatio)
		cropHeight = math.Ceil(float64(this.ComputeWidth(original)) * hRatio)
	}

	return int(cropWidth), int(cropHeight), nil
}
开发者ID:rizzles,项目名称:convert,代码行数:29,代码来源:thumbfile.go

示例12: CreateRankDirectory

/**
  Used to build a rank directory from the given input string.

  @param data A javascript string containing the data, as readable using the
  BitString object.

  @param numBits The number of bits to index.

  @param l1Size The number of bits that each entry in the Level 1 table
  summarizes. This should be a multiple of l2Size.

  @param l2Size The number of bits that each entry in the Level 2 table
  summarizes.
*/
func CreateRankDirectory(data string, numBits, l1Size, l2Size uint) RankDirectory {
	bits := BitString{}
	bits.Init(data)
	var p, i uint = 0, 0
	var count1, count2 uint = 0, 0
	l1bits := uint(math.Ceil(math.Log2(float64(numBits))))
	l2bits := uint(math.Ceil(math.Log2(float64(l1Size))))

	directory := BitWriter{}

	for p+l2Size <= numBits {
		count2 += bits.Count(p, l2Size)
		i += l2Size
		p += l2Size
		if i == l1Size {
			count1 += count2
			directory.Write(count1, l1bits)
			count2 = 0
			i = 0
		} else {
			directory.Write(count2, l2bits)
		}
	}

	rd := RankDirectory{}
	rd.Init(directory.GetData(), data, numBits, l1Size, l2Size)
	return rd
}
开发者ID:siongui,项目名称:go-succinct-data-structure-trie,代码行数:42,代码来源:rankdirectory.go

示例13: drawDistribution

func (c PngController) drawDistribution(img *image.RGBA, lat, lng, scale float32, zipCodes int) {
	y := int((180 - (lat + 90)) * scale)
	x := int((lng + 180) * scale)
	p := image.Rect(x, y, x+int(math.Ceil(float64(scale))), y+int(math.Ceil(float64(scale))))

	var zipColor color.RGBA
	if zipCodes > 1000 {
		zipColor = color.RGBA{255, 0, 0, 255}
	} else if zipCodes > 800 {
		zipColor = color.RGBA{255, 87, 0, 255}
	} else if zipCodes > 600 {
		zipColor = color.RGBA{255, 153, 0, 255}
	} else if zipCodes > 400 {
		zipColor = color.RGBA{255, 204, 51, 255}
	} else if zipCodes > 200 {
		zipColor = color.RGBA{255, 255, 102, 255}
	} else if zipCodes > 100 {
		zipColor = color.RGBA{153, 255, 153, 255}
	} else if zipCodes > 50 {
		zipColor = color.RGBA{255, 255, 255, 255}
	} else {
		zipColor = color.RGBA{204, 204, 204, 255}
	}
	draw.Draw(img, p, &image.Uniform{zipColor}, image.ZP, draw.Src)
}
开发者ID:rchargel,项目名称:zilch,代码行数:25,代码来源:png_controller.go

示例14: main

func main() {
	var N, N1, N2, N3, i, sum float64
	N = 1000
	N1 = math.Ceil(N / 3.0)
	N2 = math.Ceil(N / 5.0)
	N3 = math.Ceil(N / 15.0)
	sum = 0
	i = 1
	for i < N1 {
		sum += 3 * i
		i++
	}
	i = 1
	for i < N2 {
		sum += 5 * i
		i++
	}
	// Subtracting double counted numbers (multiples of 15)
	i = 1
	for i < N3 {
		sum -= 15 * i
		i++
	}
	fmt.Println(sum)
}
开发者ID:phoenix-opensource-marathon,项目名称:project-euler,代码行数:25,代码来源:euler_problem_1.go

示例15: testLimiter

func testLimiter(t *testing.T, store Store, rate Rate) {
	limiter := NewLimiter(store, rate)

	i := 1
	for i <= 5 {
		if i <= 3 {
			ctx, err := limiter.Peek("boo")
			assert.NoError(t, err)
			assert.Equal(t, int64(3-(i-1)), ctx.Remaining)
		}

		ctx, err := limiter.Get("boo")
		assert.NoError(t, err)

		if i <= 3 {
			assert.Equal(t, int64(3), ctx.Limit)
			assert.Equal(t, int64(3-i), ctx.Remaining)
			assert.True(t, math.Ceil(time.Since(time.Unix(ctx.Reset, 0)).Seconds()) <= 60)
			ctx, err := limiter.Peek("boo")
			assert.NoError(t, err)
			assert.Equal(t, int64(3-i), ctx.Remaining)
		} else {
			assert.Equal(t, int64(3), ctx.Limit)
			assert.True(t, ctx.Remaining == 0)
			assert.True(t, math.Ceil(time.Since(time.Unix(ctx.Reset, 0)).Seconds()) <= 60)
		}

		i++
	}

}
开发者ID:ulule,项目名称:limiter,代码行数:31,代码来源:limiter_test.go


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