本文整理汇总了Golang中math.Inf函数的典型用法代码示例。如果您正苦于以下问题:Golang Inf函数的具体用法?Golang Inf怎么用?Golang Inf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Inf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewHeatMap
// NewHeatMap creates as new heat map plotter for the given data,
// using the provided palette. If g has Min and Max methods that return
// a float, those returned values are used to set the respective HeatMap
// fields.
func NewHeatMap(g GridXYZ, p palette.Palette) *HeatMap {
var min, max float64
type minMaxer interface {
Min() float64
Max() float64
}
switch g := g.(type) {
case minMaxer:
min, max = g.Min(), g.Max()
default:
min, max = math.Inf(1), math.Inf(-1)
c, r := g.Dims()
for i := 0; i < c; i++ {
for j := 0; j < r; j++ {
v := g.Z(i, j)
if math.IsNaN(v) {
continue
}
min = math.Min(min, v)
max = math.Max(max, v)
}
}
}
return &HeatMap{
GridXYZ: g,
Palette: p,
Min: min,
Max: max,
}
}
示例2: TestFloatCmpSpecialValues
// TestFloatCmpSpecialValues tests that Cmp produces the correct results for
// combinations of zero (±0), finite (±1 and ±2.71828), and infinite (±Inf)
// operands.
func TestFloatCmpSpecialValues(t *testing.T) {
zero := 0.0
args := []float64{math.Inf(-1), -2.71828, -1, -zero, zero, 1, 2.71828, math.Inf(1)}
xx := new(Float)
yy := new(Float)
for i := 0; i < 4; i++ {
for _, x := range args {
xx.SetFloat64(x)
// check conversion is correct
// (no need to do this for y, since we see exactly the
// same values there)
if got, acc := xx.Float64(); got != x || acc != Exact {
t.Errorf("Float(%g) == %g (%s)", x, got, acc)
}
for _, y := range args {
yy.SetFloat64(y)
got := xx.Cmp(yy)
want := 0
switch {
case x < y:
want = -1
case x > y:
want = +1
}
if got != want {
t.Errorf("(%g).Cmp(%g) = %s; want %s", x, y, got, want)
}
}
}
}
}
示例3: Train
// Train computes and stores the bin values
// for the training instances.
func (b *BinningFilter) Train() error {
as := b.getAttributeSpecs()
// Set up the AttributeSpecs, and values
for attr := range b.attrs {
if !b.attrs[attr] {
continue
}
b.minVals[attr] = float64(math.Inf(1))
b.maxVals[attr] = float64(math.Inf(-1))
}
err := b.train.MapOverRows(as, func(row [][]byte, rowNo int) (bool, error) {
for i, a := range row {
attr := as[i].GetAttribute()
attrf := attr.(*base.FloatAttribute)
val := float64(attrf.GetFloatFromSysVal(a))
if val > b.maxVals[attr] {
b.maxVals[attr] = val
}
if val < b.minVals[attr] {
b.minVals[attr] = val
}
}
return true, nil
})
if err != nil {
return fmt.Errorf("Training error: %s", err)
}
b.trained = true
return nil
}
示例4: generateValidatedLengthExample
// generateValidatedLengthExample generates a random size array of examples based on what's given.
func (eg *exampleGenerator) generateValidatedLengthExample() interface{} {
minlength, maxlength := math.Inf(1), math.Inf(-1)
if eg.a.Validation != nil {
if eg.a.Validation.MinLength != nil {
minlength = float64(*eg.a.Validation.MinLength)
}
if eg.a.Validation.MaxLength != nil {
minlength = float64(*eg.a.Validation.MaxLength)
}
}
count := 0
if math.IsInf(minlength, 1) {
count = int(maxlength) - (eg.r.Int() % 3)
} else if math.IsInf(maxlength, -1) {
count = int(minlength) + (eg.r.Int() % 3)
} else if minlength < maxlength {
count = int(minlength) + (eg.r.Int() % int(maxlength-minlength))
} else if minlength == maxlength {
count = int(minlength)
} else {
panic("Validation: MinLength > MaxLength")
}
if !eg.a.Type.IsArray() {
return eg.r.faker.Characters(count)
}
res := make([]interface{}, count)
for i := 0; i < count; i++ {
res[i] = eg.a.Type.ToArray().ElemType.GenerateExample(eg.r)
}
return res
}
示例5: 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
}
示例6: TestAllSetDefaults
func TestAllSetDefaults(t *testing.T) {
// Exercise SetDefaults with all scalar field types.
m := &Defaults{
// NaN != NaN, so override that here.
F_Nan: Float32(1.7),
}
expected := &Defaults{
F_Bool: Bool(true),
F_Int32: Int32(32),
F_Int64: Int64(64),
F_Fixed32: Uint32(320),
F_Fixed64: Uint64(640),
F_Uint32: Uint32(3200),
F_Uint64: Uint64(6400),
F_Float: Float32(314159),
F_Double: Float64(271828),
F_String: String(`hello, "world!"` + "\n"),
F_Bytes: []byte("Bignose"),
F_Sint32: Int32(-32),
F_Sint64: Int64(-64),
F_Enum: NewDefaults_Color(Defaults_GREEN),
F_Pinf: Float32(float32(math.Inf(1))),
F_Ninf: Float32(float32(math.Inf(-1))),
F_Nan: Float32(1.7),
}
SetDefaults(m)
if !Equal(m, expected) {
t.Errorf(" got %v\nwant %v", m, expected)
}
}
示例7: TestNewFromFloat
func TestNewFromFloat(t *testing.T) {
var err float64
for f, s := range testTable {
d := NewFromFloat(f)
if d.String() != s {
err++
// t.Errorf("expected %s, got %s (%d, %d)",
// s, d.String(), d.compact, d.scale)
}
}
// Some margin of error is acceptable when converting from
// a float. On a table of roughly 9,000 entries an acceptable
// margin of error is around 450.
// Currently, using Gaussian/banker's rounding our margin
// of error is roughly 215 per 9,000 entries, for a rate of
// around 2.3%.
if err >= 0.05*float64(len(testTable)) {
t.Errorf("expected error rate to be < 0.05%% of table, got %.f", err)
}
shouldPanicOn := []float64{
math.NaN(),
math.Inf(1),
math.Inf(-1),
}
for _, n := range shouldPanicOn {
var d *Decimal
if !didPanic(func() { d = NewFromFloat(n) }) {
t.Fatalf("expected panic when creating a Decimal from %v, got %v instead", n, d.String())
}
}
}
示例8: Initialize
func (b *Button) Initialize() {
b.Foundation.Initialize()
b.DrawOp = draw.Over
b.Label = NewLabel(b.Size, LabelConfig{
Text: "",
FontSize: 12,
Color: color.Black,
})
b.AddBlock(&b.Label.Block)
b.Clickers = map[Clicker]bool{}
b.AddClicker = make(chan Clicker, 1)
b.RemoveClicker = make(chan Clicker, 1)
var cs geom.Coord
cs.X, cs.Y = b.Bounds().Size()
sh := uik.SizeHint{
MinSize: cs,
PreferredSize: cs,
MaxSize: geom.Coord{math.Inf(1), math.Inf(1)},
}
b.SetSizeHint(sh)
b.setConfig = make(chan ButtonConfig, 1)
b.getConfig = make(chan ButtonConfig, 1)
}
示例9: generateValidatedLengthExample
// generateValidatedLengthExample generates a random size array of examples based on what's given.
func (eg *exampleGenerator) generateValidatedLengthExample() interface{} {
minlength, maxlength := math.Inf(1), math.Inf(-1)
for _, v := range eg.a.Validations {
switch actual := v.(type) {
case *dslengine.MinLengthValidationDefinition:
minlength = math.Min(minlength, float64(actual.MinLength))
maxlength = math.Max(maxlength, float64(actual.MinLength))
case *dslengine.MaxLengthValidationDefinition:
minlength = math.Min(minlength, float64(actual.MaxLength))
maxlength = math.Max(maxlength, float64(actual.MaxLength))
}
}
count := 0
if math.IsInf(minlength, 1) {
count = int(maxlength) - (eg.r.Int() % 3)
} else if math.IsInf(maxlength, -1) {
count = int(minlength) + (eg.r.Int() % 3)
} else if minlength < maxlength {
count = int(minlength) + (eg.r.Int() % int(maxlength-minlength))
} else if minlength == maxlength {
count = int(minlength)
} else {
panic("Validation: MinLength > MaxLength")
}
if !eg.a.Type.IsArray() {
return eg.r.faker.Characters(count)
}
res := make([]interface{}, count)
for i := 0; i < count; i++ {
res[i] = eg.a.Type.ToArray().ElemType.GenerateExample(eg.r)
}
return res
}
示例10: Merge
// Merge merges the data of two Stats objects.
func (s Stats) Merge(t Stats) Stats {
if s.count == 0 {
s.max = math.Inf(-1)
s.min = math.Inf(+1)
}
delta := t.mean - s.mean
newcount := t.count + s.count
// max & min
s.max = math.Max(s.max, t.max)
s.min = math.Min(s.min, t.min)
// mean
s.mean += delta * (t.count / newcount)
// sum of squares
s.sumsq += t.sumsq
s.sumsq += delta * delta * (t.count * s.count / newcount)
// count
s.count = newcount
return s
}
示例11: TestRTT_getDatacenterDistance
func TestRTT_getDatacenterDistance(t *testing.T) {
s := newMockServer()
// The serfer's own DC is always 0 ms away.
if dist, err := getDatacenterDistance(s, "dc0"); err != nil || dist != 0.0 {
t.Fatalf("bad: %v err: %v", dist, err)
}
// Check a DC with no coordinates, which should give positive infinity.
if dist, err := getDatacenterDistance(s, "dcX"); err != nil || dist != math.Inf(1.0) {
t.Fatalf("bad: %v err: %v", dist, err)
}
// Similar for a totally unknown DC.
if dist, err := getDatacenterDistance(s, "acdc"); err != nil || dist != math.Inf(1.0) {
t.Fatalf("bad: %v err: %v", dist, err)
}
// Check the trivial median case (just one node).
if dist, err := getDatacenterDistance(s, "dc2"); err != nil || dist != 0.002 {
t.Fatalf("bad: %v err: %v", dist, err)
}
// Check the more interesting median case, note that there's a mystery
// node4 in there that should be excluded to make the distances sort
// like this:
//
// [0] node3 (0.005), [1] node1 (0.007), [2] node2 (0.008)
//
// So the median should be at index 3 / 2 = 1 -> 0.007.
if dist, err := getDatacenterDistance(s, "dc1"); err != nil || dist != 0.007 {
t.Fatalf("bad: %v err: %v", dist, err)
}
}
示例12: TestMinimalSurface
func TestMinimalSurface(t *testing.T) {
for _, size := range [][2]int{
{20, 30},
{30, 30},
{50, 40},
} {
f := NewMinimalSurface(size[0], size[1])
x0 := f.InitX()
grad := make([]float64, len(x0))
f.Grad(grad, x0)
fdGrad := fd.Gradient(nil, f.Func, x0, &fd.Settings{Formula: fd.Central})
// Test that the numerical and analytical gradients agree.
dist := floats.Distance(grad, fdGrad, math.Inf(1))
if dist > 1e-9 {
t.Errorf("grid %v x %v: numerical and analytical gradient do not match. |fdGrad - grad|_∞ = %v",
size[0], size[1], dist)
}
// Test that the gradient at the minimum is small enough.
// In some sense this test is not completely correct because ExactX
// returns the exact solution to the continuous problem projected on the
// grid, not the exact solution to the discrete problem which we are
// solving. This is the reason why a relatively loose tolerance 1e-4
// must be used.
xSol := f.ExactX()
f.Grad(grad, xSol)
norm := floats.Norm(grad, math.Inf(1))
if norm > 1e-4 {
t.Errorf("grid %v x %v: gradient at the minimum not small enough. |grad|_∞ = %v",
size[0], size[1], norm)
}
}
}
示例13: TestScrubValues
func TestScrubValues(t *testing.T) {
dummy := Converter{
tracker: new(tracker),
}
epoch := time.Unix(0, 0)
simple := []tsm1.Value{tsm1.NewValue(epoch, 1.0)}
for _, tt := range []struct {
input, expected []tsm1.Value
}{
{
input: simple,
expected: simple,
}, {
input: []tsm1.Value{simple[0], tsm1.NewValue(epoch, math.NaN())},
expected: simple,
}, {
input: []tsm1.Value{simple[0], tsm1.NewValue(epoch, math.Inf(-1))},
expected: simple,
}, {
input: []tsm1.Value{simple[0], tsm1.NewValue(epoch, math.Inf(1)), tsm1.NewValue(epoch, math.NaN())},
expected: simple,
},
} {
out := dummy.scrubValues(tt.input)
if !reflect.DeepEqual(out, tt.expected) {
t.Errorf("Failed to scrub '%s': Got '%s', Expected '%s'", pretty(tt.input), pretty(out), pretty(tt.expected))
}
}
}
示例14: Normalize
// Normalize Returns all the values of the given matrix normalized, the formula
// applied to all the elements is: (Xn - Avg) / (max - min) If all the elements
// in the slice have the same values, or the slice is empty, the slice can't be
// normalized, then returns false in the valid parameter
func Normalize(values []float64) (norm []float64, valid bool) {
avg := 0.0
max := math.Inf(-1)
min := math.Inf(1)
math.Inf(1)
for _, val := range values {
avg += val
if val < min {
min = val
}
if val > max {
max = val
}
}
if max == min || len(values) == 0 {
valid = false
return
}
valid = true
avg /= float64(len(values))
for _, val := range values {
norm = append(norm, (val-avg)/(max-min))
}
return
}
示例15: TestScaledUpHalfKStandardWeibullProb
func TestScaledUpHalfKStandardWeibullProb(t *testing.T) {
pts := []univariateProbPoint{
univariateProbPoint{
loc: 0,
prob: math.Inf(1),
cumProb: 0,
logProb: math.Inf(1),
},
univariateProbPoint{
loc: -1,
prob: 0,
cumProb: 0,
logProb: 0,
},
univariateProbPoint{
loc: 1,
prob: 0.180436508682207,
cumProb: 0.558022622759326,
logProb: -1.712376315541750,
},
univariateProbPoint{
loc: 20,
prob: 0.002369136850928,
cumProb: 0.974047406098605,
logProb: -6.045229588092130,
},
}
testDistributionProbs(t, Weibull{K: 0.5, Lambda: 1.5}, "0.5K 1.5λ Weibull", pts)
}