Exemplo n.º 1
0
Arquivo: merges.go Projeto: baeeq/god
func floatDiv(oldValues [][]byte, newValues [][]byte, w float64) (result [][]byte) {
	var sum float64
	var tmp float64
	var err error
	if oldValues != nil {
		if tmp, err = common.DecodeFloat64(oldValues[0]); err == nil {
			sum = tmp
		}
		for _, b := range newValues {
			if tmp, err = common.DecodeFloat64(b); err == nil {
				sum /= (tmp * w)
			}
		}
	} else {
		if tmp, err = common.DecodeFloat64(newValues[0]); err == nil {
			sum = (tmp * w)
		}
		for _, b := range newValues[1:] {
			if tmp, err = common.DecodeFloat64(b); err == nil {
				sum /= (tmp * w)
			}
		}
	}
	res := new(bytes.Buffer)
	binary.Write(res, binary.BigEndian, sum)
	return [][]byte{res.Bytes()}
}
Exemplo n.º 2
0
Arquivo: merges.go Projeto: baeeq/god
func floatMul(oldValues [][]byte, newValues [][]byte, w float64) (result [][]byte) {
	var sum float64 = 1
	var tmp float64
	var err error
	for _, b := range oldValues {
		if tmp, err = common.DecodeFloat64(b); err == nil {
			sum *= tmp
		}
	}
	for _, b := range newValues {
		if tmp, err = common.DecodeFloat64(b); err == nil {
			sum *= (tmp * w)
		}
	}
	res := new(bytes.Buffer)
	binary.Write(res, binary.BigEndian, sum)
	return [][]byte{res.Bytes()}
}
Exemplo n.º 3
0
func decode(b []byte) string {
	switch *enc {
	case stringFormat:
		return string(b)
	case floatFormat:
		res, err := common.DecodeFloat64(b)
		if err != nil {
			return fmt.Sprint(b)
		}
		return fmt.Sprint(res)
	case intFormat:
		res, err := common.DecodeInt64(b)
		if err != nil {
			return fmt.Sprint(b)
		}
		return fmt.Sprint(res)
	case bigFormat:
		return fmt.Sprint(common.DecodeBigInt(b))
	}
	panic(fmt.Errorf("Unknown encoding: %v", *enc))
}
Exemplo n.º 4
0
func TestFloatDiv(t *testing.T) {
	found := floatDiv([][]byte{common.EncodeFloat64(48)}, [][]byte{common.EncodeFloat64(2), common.EncodeFloat64(3), common.EncodeFloat64(4)}, 1)
	expected := [][]byte{common.EncodeFloat64(2)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeFloat64(found[0])), fmt.Sprint(common.DecodeFloat64(expected[0])))
	}
	found = floatDiv(nil, [][]byte{common.EncodeFloat64(48), common.EncodeFloat64(2), common.EncodeFloat64(3), common.EncodeFloat64(4)}, 1)
	expected = [][]byte{common.EncodeFloat64(2)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeFloat64(found[0])), fmt.Sprint(common.DecodeFloat64(expected[0])))
	}
	found = floatDiv([][]byte{common.EncodeFloat64(48)}, [][]byte{common.EncodeFloat64(2)}, 2)
	expected = [][]byte{common.EncodeFloat64(12)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeFloat64(found[0])), fmt.Sprint(common.DecodeFloat64(expected[0])))
	}
}