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


Golang math.Fabs函数代码示例

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


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

示例1: stripColors

//strip everything that is not red
//this is probably more complicated than it needs to be - but it works
func stripColors(img image.Image) *image.RGBA {
	rgb := rgba(img)

	pix := rgb.Pix[0]
	var avg float64
	min_dist := 30.0
	for i := 0; i < len(rgb.Pix); i++ {
		pix = rgb.Pix[i]

		//first we need to kill everything that is some shade of grey
		avg = (float64(pix.R) + float64(pix.G) + float64(pix.B)) / 3.0

		dist_r := math.Fabs(float64(pix.R) - avg)
		dist_g := math.Fabs(float64(pix.G) - avg)
		dist_b := math.Fabs(float64(pix.B) - avg)

		if dist_r <= min_dist && dist_g <= min_dist && dist_b <= min_dist {
			rgb.Pix[i].R = 0
			rgb.Pix[i].G = 0
			rgb.Pix[i].B = 0
		}

		//now keep only the red data (which is the current one)
		if (pix.R > pix.G && pix.R > pix.B) &&
			(pix.R-pix.G) > 125 && (pix.R-pix.B) > 125 {
			//keep pixel
		} else {
			rgb.Pix[i].R = 0
			rgb.Pix[i].G = 0
			rgb.Pix[i].B = 0
		}
	}

	return rgb
}
开发者ID:kybernetyk,项目名称:Blidshack,代码行数:37,代码来源:main.go

示例2: nearEqual

func nearEqual(a, b, closeEnough, maxError float64) bool {
	absDiff := math.Fabs(a - b)
	if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
		return true
	}
	return absDiff/max(math.Fabs(a), math.Fabs(b)) < maxError
}
开发者ID:WXB506,项目名称:golang,代码行数:7,代码来源:rand_test.go

示例3: WeightedIterator

func (l Line) WeightedIterator() (pix []WeightedPoint) {

	max_delta := 2.0 * l.radius // will miss prob mass outside of 2 std dev
	normalizer := 1.0 / math.Sqrt(2.0*math.Pi*l.radius*l.radius)

	var p image.Point
	cur := l.left
	iter := int(math.Fmax(math.Fabs(l.Dx()), math.Fabs(l.Dy()))) + 1
	dx := l.Dx() / float64(iter)
	dy := l.Dy() / float64(iter)

	for i := 0; i < iter; i++ {
		for d := -max_delta; d < max_delta; d += 1.0 {

			if dx > dy { // vertical sweeps
				p = image.Point{int(cur.X), int(cur.Y + d)}
			} else { // horizontal sweeps
				p = image.Point{int(cur.X + d), int(cur.Y)}
			}

			fp := NewFloat64Point(p)
			dist := l.Distance(fp.X, fp.Y)
			dist = math.Fmin(dist, Distance(fp, l.left))
			dist = math.Fmin(dist, Distance(fp, l.right))
			weight := normalizer * math.Exp(-dist*dist/(2.0*l.radius*l.radius))

			pix = append(pix, WeightedPoint{p, weight})
		}
		cur.X += dx
		cur.Y += dy
	}
	return pix
}
开发者ID:twolfe18,项目名称:sudoku,代码行数:33,代码来源:line.go

示例4: Distance

// calculate the closest distance between to locations
func (m *Map) Distance(srcLoc, destLoc Location) float64 {
	row1, col1 := m.FromLocation(srcLoc)
	row2, col2 := m.FromLocation(destLoc)
	dCol := math.Fmin(math.Fabs(float64(col1-col2)), float64(m.Cols)-math.Fabs(float64(col1-col2)))
	dRow := math.Fmin(math.Fabs(float64(row1-row2)), float64(m.Rows)-math.Fabs(float64(row1-row2)))
	return dCol + dRow
}
开发者ID:border,项目名称:ants.go,代码行数:8,代码来源:map.go

示例5: moveEnt

func (w *World) moveEnt(ent *EntityDesc, vel *s3dm.V3) {
	// Compute new position vector
	old_pos, ok := w.pos[ent.Uid]
	if !ok { // Entity hasn't been added for some reason, bail
		log.Println("No position for", ent.Uid)
		return
	}
	new_pos := old_pos.Add(vel)
	hash := hashV3(new_pos)

	// See if destination cell is occupied
	if ent_ch, ok := w.ents[hash]; ok {
		// TODO: HACK remove following if block when spider AI uses time based move
		// Update position if movement is less than 1, this lets spider move slowly
		if math.Fabs(vel.X) < 1 && math.Fabs(vel.Y) < 1 {
			w.pos[ent.Uid] = new_pos
			Send(w, ent.Chan, MsgSetState{Position{new_pos}})
		}
		// Can't move there, attack instead
		Send(w, ent_ch, MsgRunAction{Attack{ent}, false})
		return
	}
	// If not, move the entity to the new pos
	w.setPos(ent, new_pos, old_pos)
	// Update entity position state
	Send(w, ent.Chan, MsgSetState{Position{new_pos}})

	// Spawn spiders as players move around
	if ent.Id == cmpId.Player {
		w.spawnSpiders(new_pos)
	}
}
开发者ID:archwyrm,项目名称:ghack,代码行数:32,代码来源:world.go

示例6: ComplexEqual

// returns true if a and b are very close, else false
func ComplexEqual(a, b complex128) bool {
	r_a := real(a)
	r_b := real(b)
	i_a := imag(a)
	i_b := imag(b)

	return ((math.Fabs(r_a-r_b) <= closeFactor || math.Fabs(1-r_a/r_b) <= closeFactor) &&
		(math.Fabs(i_a-i_b) <= closeFactor || math.Fabs(1-i_a/i_b) <= closeFactor))
}
开发者ID:valtyr,项目名称:go-dsp,代码行数:10,代码来源:fft_test.go

示例7: genString

func genString(stddev float64) string {
	n := int(math.Fabs(rand.NormFloat64()*stddev + stddev/2))
	c := make([]int, n)
	for i := range c {
		f := math.Fabs(rand.NormFloat64()*64 + 32)
		if f > 0x10ffff {
			f = 0x10ffff
		}
		c[i] = int(f)
	}
	return string(c)
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:12,代码来源:scanner_test.go

示例8: TestDotProduct

func TestDotProduct(T *testing.T) {
	var (
		p1 = Point{-1.23456, 7.890987}
		p2 = Point{7.890987, 1.23456}
	)
	if math.Fabs(p1.Dot(p2)-p2.Dot(p1)) > 1.0e-12 {
		T.Errorf("Symmetric dot product failure %e", math.Fabs(p1.Dot(p2)-p2.Dot(p1)))
	}
	if math.Fabs(p1.Dot(p2)) > 1.0e-12 {
		T.Errorf("Symmetric dot product failure %e", math.Fabs(p1.Dot(p2)-p2.Dot(p1)))
	}
}
开发者ID:newblue,项目名称:hexgrid,代码行数:12,代码来源:point2d_test.go

示例9: TestNorm

func TestNorm(T *testing.T) {
	var (
		e1 = Point{1, 0}
		e2 = Point{0, -1}
	)
	if math.Fabs(e1.Norm()-1) > 1.0e-15 {
		T.Errorf("Unit vector e1 norm is not one %f", e1.Norm())
	}
	if math.Fabs(e2.Norm()-1) > 1.0e-15 {
		T.Errorf("Unit vector -e2 norm is not one %f", e2.Norm())
	}
}
开发者ID:newblue,项目名称:hexgrid,代码行数:12,代码来源:point2d_test.go

示例10: AbsMin3

func AbsMin3(a, b, c float64) float64 {
	fabsa := math.Fabs(a)
	fabsb := math.Fabs(b)
	fabsc := math.Fabs(c)

	if fabsa < fabsb && fabsa < fabsc {
		return a
	}
	if fabsb < fabsa && fabsb < fabsc {
		return b
	}
	return c
}
开发者ID:eadf,项目名称:math3d,代码行数:13,代码来源:util.go

示例11: MouseDrag

func (this *DragTracker) MouseDrag(e *MouseEvent) {
	this.DefaultTool.MouseDrag(e)
	this.hasMoved = math.Fabs(float64(e.X-this.anchorX)) > 3 ||
		math.Fabs(float64(e.Y-this.anchorY)) > 3
	if this.hasMoved {
		selectedFigures := this.editor.GetView().GetSelection()
		for i := 0; i < selectedFigures.Len(); i++ {
			selectedFigure := selectedFigures.At(i).(Figure)
			selectedFigure.MoveBy(selectedFigure, e.X-this.lastX, e.Y-this.lastY)
		}
	}
	this.lastX = e.X
	this.lastY = e.Y
}
开发者ID:drankfrabbin,项目名称:gohotdraw,代码行数:14,代码来源:tools.go

示例12: ShortStartTime

func (v *Subtitle) ShortStartTime() string {

	seconds := v.StartTimeInSecondsFixed()

	sign := ""
	if seconds < 0 {
		sign = "-"
	}

	m := int(math.Fabs(seconds) / 60)
	s := int(math.Fmod(math.Fabs(seconds), 60))

	return sign + fmt.Sprintf("%02d:%02d", m, s)
}
开发者ID:sunfmin,项目名称:perapera,代码行数:14,代码来源:database.go

示例13: analyze

func analyze(s *source) {
	fmt.Fprintf(os.Stderr, "starting file %s\n", s.filename)
	//		tracks = tracks[:1+len(tracks)]
	// loop over bands
	for index := 0; index < len(s.beats); index++ {
		s.beats[index].buckets = make([]bucket, bands)
	}

	for band := uint(0); band < bands; band++ {
		i := uint(0)
		var bi uint
		samplelength, datachan := opensrcband(s.filename, band)
		beatlength := samplelength / uint(len(s.beats))
		fmt.Fprint(os.Stderr, "got channels\n")
		fmt.Fprintf(os.Stderr, "beatlength %d, band %d / %d, beats %d\n", beatlength, band, bands, len(s.beats))
		for {
			f := <-datachan
			if f.empty() && closed(datachan) {
				break
			}
			bi = 0
			//			fmt.Println(samplelength, len(s.beats))
			dex := i / beatlength
			defer func() {
				e := recover()
				if e != nil {
					fmt.Println(bi, beatlength, dex, len(s.beats), len(f.left), len(f.right))
					panic(e)
				}
			}()
			for ; (dex < uint(len(s.beats)) && bi < beatlength) && bi < uint(len(f.left)); bi++ {
				b := bucket{float64(f.left[bi]), float64(f.right[bi])}
				if dex >= uint(len(s.beats)) {
					dex = uint(len(s.beats)) - 1
					// rolloff
					b.left = b.left * float64(dex/uint(len(s.beats)))
					b.right = b.right * float64(dex/uint(len(s.beats)))
				}
				s.beats[dex].buckets[band].left += math.Fabs(b.left)
				s.beats[dex].buckets[band].right += math.Fabs(b.right)
				//				if i%10000 == 0 {
				fmt.Fprintf(os.Stderr, "%d %d %f %d %f %f %f\n", i, f.left[bi], float64(f.left[bi]), f.right[bi], float64(f.right[bi]), s.beats[dex].buckets[band].left, s.beats[dex].buckets[band].right)
				//				}

				i++
			}
		}
	}
}
开发者ID:mkb218,项目名称:remixoscope,代码行数:49,代码来源:remixoscope.go

示例14: Solve

func (m Matrix) Solve(rhs Matrix) *Matrix {
	if m.M != m.N || rhs.M != m.N || rhs.N != 1 {
		fmt.Fprintf(os.Stderr, "Illegal matrix dimensions.")
		os.Exit(1)
	}

	a := NewMatrixFromMatrix(m)
	b := NewMatrixFromMatrix(rhs)

	for i := 0; i < m.N; i++ {
		max := i
		for j := i + 1; j < m.N; j++ {
			if math.Fabs(a.Data[j][i]) > math.Fabs(a.Data[max][i]) {
				max = j
			}
		}

		a.Swap(i, max)
		b.Swap(i, max)

		if a.Data[i][i] == 0.0 {
			fmt.Fprintf(os.Stderr, "Matrix is singular.")
			os.Exit(1)
		}

		for j := i + 1; j < m.N; j++ {
			b.Data[j][0] -= b.Data[i][0] * a.Data[j][i] / a.Data[i][i]
		}

		for j := i + 1; j < m.N; j++ {
			temp := a.Data[j][i] / a.Data[i][i]
			for k := i + 1; k < m.N; k++ {
				a.Data[j][k] -= a.Data[i][k] * temp
			}
			a.Data[j][i] = 0.0
		}
	}

	x := NewMatrixFromMandN(m.N, 1)
	for j := m.N - 1; j >= 0; j-- {
		t := 0.0
		for k := j + 1; k < m.N; k++ {
			t += a.Data[j][k] * x.Data[k][0]
		}
		x.Data[j][0] = (b.Data[j][0] - t) / a.Data[j][j]
	}
	return x
}
开发者ID:shatterednirvana,项目名称:dfsp-go,代码行数:48,代码来源:matrix.go

示例15: notNearEqual

func notNearEqual(a, b, closeEnough float64) bool {
	absDiff := math.Fabs(a - b)
	if absDiff < closeEnough {
		return false
	}
	return true
}
开发者ID:Arrow,项目名称:GoGCM,代码行数:7,代码来源:solar_test.go


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