本文整理汇总了Golang中sort.Float64sAreSorted函数的典型用法代码示例。如果您正苦于以下问题:Golang Float64sAreSorted函数的具体用法?Golang Float64sAreSorted怎么用?Golang Float64sAreSorted使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Float64sAreSorted函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestRunIncomes
func TestRunIncomes(t *testing.T) {
JsonObj := []byte(`{"Age":22, "Retirement_age":65, "Terminal_age":90, "Effective_tax_rate":0.3, "Returns_tax_rate":0.3, "N": 20000,
"Non_Taxable_contribution":17500, "Taxable_contribution": 0, "Non_Taxable_balance":0, "Taxable_balance": 0,
"Yearly_social_security_income":0, "Asset_volatility": 0.15, "Expected_rate_of_return": 0.07, "Inflation_rate":0.035}`)
rc, err := NewRetCalcFromJSON(JsonObj)
if err != nil {
t.Error(err)
}
runIncomes := rc.RunIncomes()
sort.Float64s(runIncomes)
incomePerRun := make([]float64, rc.N, rc.N)
for i := range incomePerRun {
incomePerRun[i] = rc.IncomeOnPath(i)
}
sort.Float64s(incomePerRun)
incomesOk := true
for i := range incomePerRun {
if incomePerRun[i] != runIncomes[i] {
incomesOk = false
fmt.Printf("RunIncomes: %f, IncomeOnPath: %f\n", runIncomes[i], incomePerRun[i])
}
}
if !incomesOk {
t.Errorf("Incomes do not calculate correctly for RunIncomes()")
}
if !sort.Float64sAreSorted(runIncomes) {
t.Errorf("Incomes from RetCalc.RunIncomes() should be sorted on return")
}
}
示例2: med
func (res *Results) med() {
slice := res.copyTook()
if !sort.Float64sAreSorted(slice) {
sort.Float64s(slice)
}
l := len(slice)
switch l {
case 0:
res.TookMed = float64(0)
case 1:
res.TookMed = slice[0]
case 2:
res.TookMed = slice[1]
default:
if math.Mod(float64(l), 2) == 0 {
index := int(math.Floor(float64(l)/2) - 1)
lower := slice[index]
upper := slice[index+1]
res.TookMed = (lower + upper) / 2
} else {
res.TookMed = slice[l/2]
}
}
}
示例3: TestFloat64s
func TestFloat64s(t *testing.T) {
data := float64s
Sort(sort.Float64Slice(data[0:]))
if !sort.Float64sAreSorted(data[0:]) {
t.Errorf("sorted %v", float64s)
t.Errorf(" got %v", data)
}
}
示例4: min
func (res *Results) min() {
slice := res.copyTook()
if !sort.Float64sAreSorted(slice) {
sort.Float64s(slice)
}
res.TookMin = slice[0]
}
示例5: max
func (res *Results) max() {
slice := res.copyTook()
if !sort.Float64sAreSorted(slice) {
sort.Float64s(slice)
}
res.TookMax = slice[len(slice)-1]
}
示例6: sortedCopyDif
// sortedCopyDif returns a sorted copy of float64s
// only if the original data isn't sorted.
// Only use this if returned slice won't be manipulated!
func sortedCopyDif(input Float64Data) (copy Float64Data) {
if sort.Float64sAreSorted(input) {
return input
}
copy = copyslice(input)
sort.Float64s(copy)
return
}
示例7: Max
func (sf64 *SliceFloat64) Max() (float64, error) {
if len(sf64.Elements) == 0 {
return 0, errors.New("List is empty")
}
if !sort.Float64sAreSorted(sf64.Elements) {
sort.Float64s(sf64.Elements)
}
return sf64.Elements[len(sf64.Elements)-1], nil
}
示例8: TestSort
func TestSort(t *testing.T) {
r := testData()
SortBYOB(r, make([]float64, len(r)))
if !sort.Float64sAreSorted(r) {
log.Printf("Should have sorted slice.\n")
log.Printf("Data was %v", r)
t.FailNow()
}
}
示例9: Median
func (sf64 *SliceFloat64) Median() (float64, error) {
if len(sf64.Elements) == 0 {
return 0, errors.New("List is empty")
}
if !sort.Float64sAreSorted(sf64.Elements) {
sort.Float64s(sf64.Elements)
}
mid := int64(float64(len(sf64.Elements)) / 2)
return sf64.Elements[mid], nil
}
示例10: TestSortRand
func TestSortRand(t *testing.T) {
test := func(r []float64) bool {
SortBYOB(r, make([]float64, len(r)))
return sort.Float64sAreSorted(r)
}
if err := quick.Check(test, nil); err != nil {
t.Error(err)
}
}
示例11: TestExtractXY
// extractXY should return one series with its x values sorted.
func TestExtractXY(t *testing.T) {
vals, _ := seriesTestDefaultData(5)
series := extractXY(vals, "X", "Y")
if len(series) != 1 {
t.Fatalf("extractXY returned > 1 series")
}
s := series[0]
if !sort.Float64sAreSorted(s.xs) {
t.Fatalf("extractXY returned incorrectly sorted series")
}
}
示例12: Sort
// Sort sorts the samples in place in s and returns s.
//
// A sorted sample improves the performance of some algorithms.
func (s *Sample) Sort() *Sample {
if s.Sorted || sort.Float64sAreSorted(s.Xs) {
// All set
} else if s.Weights == nil {
sort.Float64s(s.Xs)
} else {
sort.Sort(&sampleSorter{s.Xs, s.Weights})
}
s.Sorted = true
return s
}
示例13: TestFloat64s
func TestFloat64s(t *testing.T) {
data := float64s
for _, alg := range algorithms {
sorting.Float64s(data[:], alg)
if !sort.Float64sAreSorted(data[:]) {
t.Errorf("%v\n", util.GetFuncName(alg))
t.Errorf("sorted: %v\n", float64s)
t.Errorf(" got: %v\n", data)
}
}
}
示例14: TestAdjustLeftRight
func TestAdjustLeftRight(t *testing.T) {
keys := []float64{1, 2, 3, 4, 9, 5, 6, 7, 8}
counts := []uint32{1, 2, 3, 4, 9, 5, 6, 7, 8}
s := summary{keys: keys, counts: counts}
s.adjustRight(4)
if !sort.Float64sAreSorted(s.keys) || s.counts[4] != 5 {
t.Errorf("adjustRight should have fixed the keys/counts state. %v %v", s.keys, s.counts)
}
keys = []float64{1, 2, 3, 4, 0, 5, 6, 7, 8}
counts = []uint32{1, 2, 3, 4, 0, 5, 6, 7, 8}
s = summary{keys: keys, counts: counts}
s.adjustLeft(4)
if !sort.Float64sAreSorted(s.keys) || s.counts[4] != 4 {
t.Errorf("adjustLeft should have fixed the keys/counts state. %v %v", s.keys, s.counts)
}
}
示例15: Median
func Median(list []float64) (float64, error) {
if len(list) == 0 {
return 0.0, errors.New("Empty list given")
}
if !sort.Float64sAreSorted(list) {
sort.Float64s(list)
}
if len(list)%2 == 1 {
return list[(len(list) / 2)], nil
}
return (list[len(list)/2] + list[len(list)/2-1]) / 2, nil
}