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


Golang math.Nextafter函数代码示例

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


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

示例1: checkIsBestApprox

// checkIsBestApprox checks that f is the best possible float64
// approximation of r.
// Returns true on success.
func checkIsBestApprox(t *testing.T, f float64, r *Rat) bool {
	if math.Abs(f) >= math.MaxFloat64 {
		// Cannot check +Inf, -Inf, nor the float next to them (MaxFloat64).
		// But we have tests for these special cases.
		return true
	}

	// r must be strictly between f0 and f1, the floats bracketing f.
	f0 := math.Nextafter(f, math.Inf(-1))
	f1 := math.Nextafter(f, math.Inf(+1))

	// For f to be correct, r must be closer to f than to f0 or f1.
	df := delta(r, f)
	df0 := delta(r, f0)
	df1 := delta(r, f1)
	if df.Cmp(df0) > 0 {
		t.Errorf("Rat(%v).Float64() = %g (%b), but previous float64 %g (%b) is closer", r, f, f, f0, f0)
		return false
	}
	if df.Cmp(df1) > 0 {
		t.Errorf("Rat(%v).Float64() = %g (%b), but next float64 %g (%b) is closer", r, f, f, f1, f1)
		return false
	}
	if df.Cmp(df0) == 0 && !isEven(f) {
		t.Errorf("Rat(%v).Float64() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f0, f0)
		return false
	}
	if df.Cmp(df1) == 0 && !isEven(f) {
		t.Errorf("Rat(%v).Float64() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f1, f1)
		return false
	}
	return true
}
开发者ID:xorrbit,项目名称:golang,代码行数:36,代码来源:rat_test.go

示例2: TestVariance

func TestVariance(t *testing.T) {
	var ed EuclidDist
	_, dim := DATAPOINTS_D.GetSize()
	// Model D
	c := cluster{DATAPOINTS_D, CENTROIDS_D, dim, 0}
	v := variance(c, ed)

	E := 20.571429
	epsilon := .000001
	na := math.Nextafter(E, E+1)
	diff := math.Abs(v - na)

	if diff > epsilon {
		t.Errorf("TestVariance: for model D excpected %f but received %f.  The difference %f exceeds epsilon %f.", E, v, diff, epsilon)
	}

	// Variance a cluster with a perfectly centered centroids
	_, dim0 := DATAPOINTS_D0.GetSize()
	c0 := cluster{DATAPOINTS_D0, CENTROID_D0, dim0, 0}
	v0 := variance(c0, ed)

	E = 1.333333
	na = math.Nextafter(E, E+1)
	diff = math.Abs(v0 - na)

	if diff > epsilon {
		t.Errorf("TestVariance: for model D excpected %f but received %f.  The difference %f exceeds epsilon %f.", E, v0, diff, epsilon)
	}
}
开发者ID:neevor,项目名称:goxmeans,代码行数:29,代码来源:km_test.go

示例3: Ticks

// Ticks returns Ticks in a specified range
func (DefaultTicks) Ticks(min, max float64) (ticks []Tick) {
	const SuggestedTicks = 3
	if max < min {
		panic("illegal range")
	}
	tens := math.Pow10(int(math.Floor(math.Log10(max - min))))
	n := (max - min) / tens
	for n < SuggestedTicks {
		tens /= 10
		n = (max - min) / tens
	}

	majorMult := int(n / SuggestedTicks)
	switch majorMult {
	case 7:
		majorMult = 6
	case 9:
		majorMult = 8
	}
	majorDelta := float64(majorMult) * tens
	val := math.Floor(min/majorDelta) * majorDelta
	prec := maxInt(precisionOf(min), precisionOf(max))
	for val <= max {
		if val >= min && val <= max {
			ticks = append(ticks, Tick{Value: val, Label: formatFloatTick(val, prec)})
		}
		if math.Nextafter(val, val+majorDelta) == val {
			break
		}
		val += majorDelta
	}

	minorDelta := majorDelta / 2
	switch majorMult {
	case 3, 6:
		minorDelta = majorDelta / 3
	case 5:
		minorDelta = majorDelta / 5
	}

	val = math.Floor(min/minorDelta) * minorDelta
	for val <= max {
		found := false
		for _, t := range ticks {
			if t.Value == val {
				found = true
			}
		}
		if val >= min && val <= max && !found {
			ticks = append(ticks, Tick{Value: val})
		}
		if math.Nextafter(val, val+minorDelta) == val {
			break
		}
		val += minorDelta
	}
	return
}
开发者ID:skiesel,项目名称:plot,代码行数:59,代码来源:axis.go

示例4: init

func init() {
	onePlusEps := math.Nextafter(1, math.Inf(1))
	eps := (math.Nextafter(1, math.Inf(1)) - 1) * 0.5
	dlamchE = eps
	sfmin := math.SmallestNonzeroFloat64
	small := 1 / math.MaxFloat64
	if small >= sfmin {
		sfmin = small * onePlusEps
	}
	dlamchS = sfmin
	radix := 2.0
	dlamchP = radix * eps
}
开发者ID:RomainVabre,项目名称:origin,代码行数:13,代码来源:general.go

示例5: testPrefixBoundary

func testPrefixBoundary(t *testing.T, scales []float64, prefixes string, mode int, wrap func(string) string) {
	var str, str1, str2, pre string
	var flt, fabs, fnum float64
	var err error

	base := 1024.0
	if mode == SI {
		base = 1000.0
	}

	for _, sign := range []float64{-1, +1} {
		for i, f := range scales {
			// Round towards zero.
			str = FormatPrefix(math.Nextafter(sign*f, sign*ninf), mode, -1)
			flt, err = ParsePrefix(str, mode)
			fabs = math.Abs(flt)

			pre = string(prefixes[0])
			if i > 0 {
				pre = string(prefixes[i-1])
			}
			pre = wrap(pre)
			str1, str2 = split(str)
			fnum = math.Abs(atof(str1))
			if i == 0 {
				assert.True(t, 1.0*loThres <= fnum && fnum <= 1.0)
			} else {
				assert.True(t, base*loThres <= fnum && fnum <= base)
			}
			assert.Equal(t, pre, str2)
			assert.True(t, f*loThres <= fabs && fabs <= f)
			assert.Equal(t, math.Signbit(flt), math.Signbit(sign))
			assert.Equal(t, nil, err)

			// Round away from zero.
			str = FormatPrefix(math.Nextafter(sign*f, sign*pinf), mode, -1)
			flt, err = ParsePrefix(str, mode)
			fabs = math.Abs(flt)

			pre = wrap(string(prefixes[i]))
			str1, str2 = split(str)
			fnum = math.Abs(atof(str1))
			assert.True(t, 1.0 <= fnum && fnum <= 1.0*hiThres)
			assert.Equal(t, pre, str2)
			assert.True(t, f <= fabs && fabs <= f*hiThres)
			assert.Equal(t, math.Signbit(flt), math.Signbit(sign))
			assert.Equal(t, nil, err)
		}
	}
}
开发者ID:dsnet,项目名称:golib,代码行数:50,代码来源:strconv_test.go

示例6: main

func main() {

	fmt.Printf("Now you have %g problems.vv\n",
		math.Nextafter(2, 3))
	fmt.Printf("Hello World!\n")

}
开发者ID:ThassarianJohn,项目名称:Python,代码行数:7,代码来源:go.go

示例7: testVars

func testVars() {
	fmt.Println("The time is", time.Now().Unix())
	fmt.Printf("Now you have %g problems.\n", math.Nextafter(3, 50))
	fmt.Println(math.Pi)
	fmt.Println(add(42, 13))
	a, b := swap("ololo", "trololo")
	fmt.Println(a, b)
	fmt.Println(split(17))
	var i int
	fmt.Println(i, c, python, java)
	const f = "%T(%v)\n"
	fmt.Printf(f, ToBe, ToBe)
	fmt.Printf(f, MaxInt, MaxInt)
	fmt.Printf(f, z, z)
	fmt.Println("Pi = ", Pi+5)

	var ii int
	var ff float64
	var bb bool
	var ss string
	fmt.Printf("%v %v %v %q\n", ii, ff, bb, ss)

	var xxx, yyy int = 3, 4
	var fff float64 = math.Sqrt(float64(xxx*xxx + yyy*yyy))
	var zzz int = int(fff)
	fmt.Println(xxx, yyy, zzz)

	v := 42 // change me!
	fmt.Printf("v is of type %T\n", v)
}
开发者ID:eresid,项目名称:GoExperiments,代码行数:30,代码来源:Experiments.go

示例8: float64ToIntExp

// convert float64 to int64 where f == i / 2^exp
func float64ToIntExp(f float64) (i int64, exp int) {
	if f == 0 {
		return 0, 0
	}

	isNegative := math.Signbit(f)
	f = math.Abs(f)

	machineEpsilon := math.Nextafter(1, 2) - 1
	exp = 0
	// really large float, bring down to within MaxInt64
	for f > float64(math.MaxInt64) {
		f /= 2
		exp--
	}

	for !float64IsInt(f, machineEpsilon) {
		f *= 2
		exp++
	}
	if isNegative {
		f *= -1
	}
	return int64(f), exp
}
开发者ID:Richardphp,项目名称:noms,代码行数:26,代码来源:number_util.go

示例9: rayIntersectsSegment

func rayIntersectsSegment(p, a, b xy) bool {
	if a.y > b.y {
		a, b = b, a
	}
	for p.y == a.y || p.y == b.y {
		p.y = math.Nextafter(p.y, math.Inf(1))
	}
	if p.y < a.y || p.y > b.y {
		return false
	}
	if a.x > b.x {
		if p.x > a.x {
			return false
		}
		if p.x < b.x {
			return true
		}
	} else {
		if p.x > b.x {
			return false
		}
		if p.x < a.x {
			return true
		}
	}
	return (p.y-a.y)/(p.x-a.x) >= (b.y-a.y)/(b.x-a.x)
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:27,代码来源:ray-casting-algorithm-2.go

示例10: rayIntersectsSegment

func rayIntersectsSegment(p xy, s seg) bool {
	var a, b xy
	if s.p1.y < s.p2.y {
		a, b = s.p1, s.p2
	} else {
		a, b = s.p2, s.p1
	}
	for p.y == a.y || p.y == b.y {
		p.y = math.Nextafter(p.y, math.Inf(1))
	}
	if p.y < a.y || p.y > b.y {
		return false
	}
	if a.x > b.x {
		if p.x > a.x {
			return false
		}
		if p.x < b.x {
			return true
		}
	} else {
		if p.x > b.x {
			return false
		}
		if p.x < a.x {
			return true
		}
	}
	return (p.y-a.y)/(p.x-a.x) >= (b.y-a.y)/(b.x-a.x)
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:30,代码来源:ray-casting-algorithm-1.go

示例11: main

func main() {
	fmt.Println("Randome number without seed", rand.Intn(10))
	rand.Seed(time.Now().UnixNano())
	fmt.Println("My lucky number is", rand.Intn(10))
	fmt.Printf("Now you have %g problems.\n", math.Nextafter(2, 3))
	fmt.Println(math.Pi)
}
开发者ID:hanksudo,项目名称:go-101,代码行数:7,代码来源:random.go

示例12: main

func main() {
	fmt.Println("wlecome to the playground")
	fmt.Println("This time is ", time.Now())
	fmt.Println("My favorite number is ", rand.Intn(10))
	fmt.Printf("Now you have %g problems.", math.Nextafter(2, 3))
	fmt.Println("\n")
	fmt.Println(math.Pi)
	fmt.Println(add(2, 3))
	fmt.Println(minus(2, 3))
	fmt.Println(split(11))

	var i, j int = 1, 2
	k := 3
	c, python, java := true, false, "hogehoge"
	fmt.Println(i, j, k, c, python, java)

	const f = "%T(%v)\n"
	fmt.Printf(f, ToBe, ToBe)
	fmt.Printf(f, MaxInt, MaxInt)
	fmt.Printf(f, z, z)

	const Truth = true
	fmt.Println("Go rules?", Truth)

}
开发者ID:529,项目名称:golang,代码行数:25,代码来源:world.go

示例13: Decompose

// Decompose computes an m-by-n matrix M for an m-by-m covariance matrix Σ such
// that, for an n-element vector X with uncorrelated components, M * X is an
// m-element vector whose components are correlated according to Σ. The
// function reduces the number of dimensions from m to n such that a certain
// portion of the variance is preserved, which is controlled by λ ∈ (0, 1].
func Decompose(Σ []float64, m uint, λ float64) ([]float64, uint, error) {
	U, Λ, err := decomposition.CovPCA(Σ, m, math.Sqrt(math.Nextafter(1, 2)-1))
	if err != nil {
		return nil, 0, err
	}

	n := m

	// NOTE: Λ is in the descending order and nonnegative.
	var cum, sum float64
	for i := uint(0); i < m; i++ {
		sum += Λ[i]
	}
	for i := uint(0); i < m; i++ {
		cum += Λ[i]
		if cum/sum >= λ {
			n = i + 1
			break
		}
	}

	for i := uint(0); i < n; i++ {
		coef := math.Sqrt(Λ[i])
		for j := uint(0); j < m; j++ {
			U[i*m+j] *= coef
		}
	}

	return U[:m*n], n, nil
}
开发者ID:postfix,项目名称:statistics-1,代码行数:35,代码来源:main.go

示例14: cellIDFromFaceIJWrap

func cellIDFromFaceIJWrap(f, i, j int) CellID {
	// Convert i and j to the coordinates of a leaf cell just beyond the
	// boundary of this face.  This prevents 32-bit overflow in the case
	// of finding the neighbors of a face cell.
	i = clamp(i, -1, maxSize)
	j = clamp(j, -1, maxSize)

	// We want to wrap these coordinates onto the appropriate adjacent face.
	// The easiest way to do this is to convert the (i,j) coordinates to (x,y,z)
	// (which yields a point outside the normal face boundary), and then call
	// xyzToFaceUV to project back onto the correct face.
	//
	// The code below converts (i,j) to (si,ti), and then (si,ti) to (u,v) using
	// the linear projection (u=2*s-1 and v=2*t-1).  (The code further below
	// converts back using the inverse projection, s=0.5*(u+1) and t=0.5*(v+1).
	// Any projection would work here, so we use the simplest.)  We also clamp
	// the (u,v) coordinates so that the point is barely outside the
	// [-1,1]x[-1,1] face rectangle, since otherwise the reprojection step
	// (which divides by the new z coordinate) might change the other
	// coordinates enough so that we end up in the wrong leaf cell.
	const scale = 1.0 / maxSize
	limit := math.Nextafter(1, 2)
	u := math.Max(-limit, math.Min(limit, scale*float64((i<<1)+1-maxSize)))
	v := math.Max(-limit, math.Min(limit, scale*float64((j<<1)+1-maxSize)))

	// Find the leaf cell coordinates on the adjacent face, and convert
	// them to a cell id at the appropriate level.
	f, u, v = xyzToFaceUV(faceUVToXYZ(f, u, v))
	return cellIDFromFaceIJ(f, stToIJ(0.5*(u+1)), stToIJ(0.5*(v+1)))
}
开发者ID:atombender,项目名称:gos2,代码行数:30,代码来源:cellid.go

示例15: main

func main() {
	fmt.Println("Hello, Golang!")
	fmt.Println("rand number: ", rand.Intn(10))
	fmt.Printf("rand number: %g\n", math.Nextafter(2, 3))
	fmt.Println(add(2, 3))
	fmt.Println(swap("hello", "world"))
	fmt.Println(split(111))

	// local variable
	var i int = 1000

	// short declaration
	k := 3
	s := "hello"

	fmt.Println(i, c, python, java, k, s)

	const f = "%T(%v)\n"
	var (
		ToBe   bool       = false
		MaxInt uint64     = 1<<64 - 1
		z      complex128 = cmplx.Sqrt(-5 + 2i)
	)
	fmt.Printf(f, ToBe, ToBe)
	fmt.Printf(f, MaxInt, MaxInt)
	fmt.Printf(f, z, z)

	fmt.Printf(f, Pi, Pi)
	fmt.Printf(f, s, s)
}
开发者ID:WuXibin,项目名称:A-tour-of-Go,代码行数:30,代码来源:basic.go


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