Esempio n. 1
0
func (self *client) Zrangebyscore(key string, start float64, end float64) ([][]byte, os.Error) {
	res, err := self.sendCommand("ZRANGEBYSCORE", key, strconv.Ftoa64(start, 'f', -1), strconv.Ftoa64(end, 'f', -1))
	if err != nil {
		return nil, err
	}

	return res.([][]byte), nil
}
Esempio n. 2
0
// Sell opens a new order to sell BTC.
func Sell(c appengine.Context, login xgen.Credentials, price float64, amount float64) (x xgen.OpenOrders, err os.Error) {
	defer func() {
		if e, ok := recover().(os.Error); ok {
			err = e
		}
	}()
	var openOrders OpenOrders
	err = restapi.PostJson(c, JsonSell, map[string][]string{"name": {login.Username}, "pass": {login.Password},
		"price": {strconv.Ftoa64(price, 'f', -1)}, "amount": {strconv.Ftoa64(amount, 'f', -1)}}, &openOrders)
	check(err)
	x = openOrders.convert()
	return
}
Esempio n. 3
0
func (s *Splitter) SaveState() os.Error {
	buf := bytes.NewBuffer(nil)

	count := s.children.Len()
	layout := s.Layout().(*splitterLayout)

	for i := 0; i < count; i += 2 {
		if i > 0 {
			buf.WriteString(" ")
		}

		buf.WriteString(strconv.Ftoa64(layout.fractions[i/2], 'f', -1))
	}

	s.putState(buf.String())

	for _, widget := range s.children.items {
		if persistable, ok := widget.(Persistable); ok {
			if err := persistable.SaveState(); err != nil {
				return err
			}
		}
	}

	return nil
}
Esempio n. 4
0
// any to string
func atos(i interface{}) (s string) {
	switch t := i.(type) {
	case int64:
		s = strconv.Itoa64(t)
	case uint64:
		s = strconv.Uitoa64(t)
	case float32:
		s = strconv.Ftoa32(t, 'f', -1)
	case float64:
		s = strconv.Ftoa64(t, 'f', -1)
	case []byte:
		s = string(t)
	case Date:
		return t.String()
	case Time:
		return t.String()
	case DateTime:
		return t.String()
	case string:
		return t
	default:
		panic("Not a string or compatible type")
	}
	return
}
Esempio n. 5
0
func enc(val reflect.Value, typ reflect.Type, buffer *bytes.Buffer, req bool) (encoded bool, err os.Error) {
	switch typ.(type) {
	case *reflect.PtrType:
		// log.Println("pointer")
		pt := typ.(*reflect.PtrType).Elem()
		pv := reflect.Indirect(val)
		// log.Println("Type is: ", pt.Name())
		if pv == nil {
			if !req {
				return false, nil
			} else {
				return false, os.NewError("Required field is nil")
			}
		}
		return enc(pv, pt, buffer, req)
	case *reflect.BoolType:
		if val.(*reflect.BoolValue).Get() {
			buffer.WriteString("true")
		} else {
			buffer.WriteString("false")
		}
	case *reflect.IntType:
		buffer.WriteString(strconv.Itoa64(val.(*reflect.IntValue).Get()))
	case *reflect.UintType:
		buffer.WriteString(strconv.Uitoa64(val.(*reflect.UintValue).Get()))
	case *reflect.StringType:
		buffer.WriteString("\"")
		// TODO: encode
		buffer.WriteString(val.(*reflect.StringValue).Get())
		buffer.WriteString("\"")
	case *reflect.FloatType:
		buffer.WriteString(strconv.Ftoa64(val.(*reflect.FloatValue).Get(), 'e', -1))
	case *reflect.StructType:
		enc_struct(val.(*reflect.StructValue), typ.(*reflect.StructType), buffer)
	case *reflect.SliceType:
		st := typ.(*reflect.SliceType).Elem()
		sv := val.(*reflect.SliceValue)
		if sv.IsNil() {
			if req {
				return false, os.NewError("Required field is nil")
			} else {
				return false, nil
			}
		}
		buffer.WriteString("[")
		for i := 0; i < sv.Len(); i++ {
			if i > 0 {
				buffer.WriteString(",")
			}
			_, err := enc(sv.Elem(i), st, buffer, true)
			if err != nil {
				return false, err
			}
		}
		buffer.WriteString("]")
	default:
		return false, os.NewError("Unsupported type")
	}
	return true, nil
}
Esempio n. 6
0
func AsString(v interface{}) (string, os.Error) {
	switch value := v.(type) {
	default:
		return "", os.NewError(fmt.Sprintf("unexpected type: %T", value))
	case int:
		return strconv.Itoa(value), nil
	case int8:
		return strconv.Itoa(int(value)), nil
	case int16:
		return strconv.Itoa(int(value)), nil
	case int32:
		return strconv.Itoa(int(value)), nil
	case int64:
		return strconv.Itoa64(value), nil
	case uint:
		return strconv.Uitoa(value), nil
	case uint8:
		return strconv.Uitoa(uint(value)), nil
	case uint16:
		return strconv.Uitoa(uint(value)), nil
	case uint32:
		return strconv.Uitoa(uint(value)), nil
	case uint64:
		return strconv.Uitoa64(value), nil
	case float32:
		return strconv.Ftoa32(value, 'g', -1), nil
	case float64:
		return strconv.Ftoa64(value, 'g', -1), nil
	case string:
		return value, nil
	}
	panic(fmt.Sprintf("unsupported type: %s", reflect.ValueOf(v).Type().Name()))
}
Esempio n. 7
0
func main() {
	ir, err := gothic.NewInterpreter()
	if err != nil {
		panic(err)
	}

	feet := ir.NewStringVar("feet")
	meters := ir.NewStringVar("meters")
	ir.RegisterCallback("calculate", func() {
		f, _ := strconv.Atof64(feet.Get())
		meters.Set(strconv.Ftoa64(f*0.3048, 'f', 3))
	})

	ir.Eval(`
wm title . "Feet to Meters"
grid [ttk::frame .c -padding "3 3 12 12"] -column 0 -row 0 -sticky nwes
grid columnconfigure . 0 -weight 1; grid rowconfigure . 0 -weight 1

grid [ttk::entry .c.feet -width 7 -textvariable feet] -column 2 -row 1 -sticky we
grid [ttk::label .c.meters -textvariable meters] -column 2 -row 2 -sticky we
grid [ttk::button .c.calc -text "Calculate" -command calculate] -column 3 -row 3 -sticky w

grid [ttk::label .c.flbl -text "feet"] -column 3 -row 1 -sticky w
grid [ttk::label .c.islbl -text "is equivalent to"] -column 1 -row 2 -sticky e
grid [ttk::label .c.mlbl -text "meters"] -column 3 -row 2 -sticky w

foreach w [winfo children .c] {grid configure $w -padx 5 -pady 5}
focus .c.feet
bind . <Return> {calculate}
	`)
	ir.MainLoop()
}
Esempio n. 8
0
// Base to read float numbers.
func (q *Question) _baseReadFloat(prompt string, defaultAnswer float64, def hasDefault) (answer float64, err os.Error) {
	line := q.getLine(
		prompt,
		strconv.Ftoa64(defaultAnswer, QuestionFloatFmt, QuestionFloatPrec),
		def,
	)

	for {
		input, err := line.Read()
		if err != nil {
			return 0.0, err
		}
		if input == "" && def != _DEFAULT_NO {
			return defaultAnswer, nil
		}

		answer, err = strconv.Atof64(input)
		if err != nil {
			fmt.Fprintf(output, "%s%q: the value has to be a float\r\n",
				QuestionErrPrefix, input)
			continue
		} else {
			return answer, nil
		}
	}
	return
}
Esempio n. 9
0
func valueToString(v reflect.Value) (string, os.Error) {
	if v == nil {
		return "null", nil
	}

	switch v := v.(type) {
	case *reflect.PtrValue:
		return valueToString(reflect.Indirect(v))
	case *reflect.InterfaceValue:
		return valueToString(v.Elem())
	case *reflect.BoolValue:
		x := v.Get()
		if x {
			return "true", nil
		} else {
			return "false", nil
		}

	case *reflect.IntValue:
		return strconv.Itoa(v.Get()), nil
	case *reflect.Int8Value:
		return strconv.Itoa(int(v.Get())), nil
	case *reflect.Int16Value:
		return strconv.Itoa(int(v.Get())), nil
	case *reflect.Int32Value:
		return strconv.Itoa(int(v.Get())), nil
	case *reflect.Int64Value:
		return strconv.Itoa64(v.Get()), nil

	case *reflect.UintValue:
		return strconv.Uitoa(v.Get()), nil
	case *reflect.Uint8Value:
		return strconv.Uitoa(uint(v.Get())), nil
	case *reflect.Uint16Value:
		return strconv.Uitoa(uint(v.Get())), nil
	case *reflect.Uint32Value:
		return strconv.Uitoa(uint(v.Get())), nil
	case *reflect.Uint64Value:
		return strconv.Uitoa64(v.Get()), nil
	case *reflect.UintptrValue:
		return strconv.Uitoa64(uint64(v.Get())), nil

	case *reflect.FloatValue:
		return strconv.Ftoa(v.Get(), 'g', -1), nil
	case *reflect.Float32Value:
		return strconv.Ftoa32(v.Get(), 'g', -1), nil
	case *reflect.Float64Value:
		return strconv.Ftoa64(v.Get(), 'g', -1), nil

	case *reflect.StringValue:
		return v.Get(), nil
	case *reflect.SliceValue:
		typ := v.Type().(*reflect.SliceType)
		if _, ok := typ.Elem().(*reflect.Uint8Type); ok {
			return string(v.Interface().([]byte)), nil
		}
	}
	return "", os.NewError("Unsupported type")
}
Esempio n. 10
0
func (client *Client) Zadd(key string, value []byte, score float64) (bool, os.Error) {
	res, err := client.sendCommand("ZADD", key, strconv.Ftoa64(score, 'f', -1), string(value))
	if err != nil {
		return false, err
	}

	return res.(int64) == 1, nil
}
Esempio n. 11
0
func (client *Client) Zremrangebyscore(key string, start float64, end float64) (int, os.Error) {
	res, err := client.sendCommand("ZREMRANGEBYSCORE", key, strconv.Ftoa64(start, 'f', -1), strconv.Ftoa64(end, 'f', -1))
	if err != nil {
		return -1, err
	}

	return int(res.(int64)), nil
}
Esempio n. 12
0
func (ne *NumberEdit) SetValue(value float64) os.Error {
	text := strconv.Ftoa64(value, 'f', ne.Decimals())

	if err := ne.edit.SetText(text); err != nil {
		return err
	}

	return nil
}
Esempio n. 13
0
func (client *Client) Zincrby(key string, value []byte, score float64) (float64, os.Error) {
	res, err := client.sendCommand("ZINCRBY", key, strconv.Ftoa64(score, 'f', -1), string(value))
	if err != nil {
		return 0, err
	}

	data := string(res.([]byte))
	f, _ := strconv.Atof64(data)
	return f, nil
}
Esempio n. 14
0
func main() {
	ok := true
	for i := 0; i < len(tests); i++ {
		t := tests[i]
		v := strconv.Ftoa64(t.f, 'g', -1)
		if v != t.out {
			println("Bad float64 const:", t.in, "want", t.out, "got", v)
			x, err := strconv.Atof64(t.out)
			if err != nil {
				panicln("bug120: strconv.Atof64", t.out)
			}
			println("\twant exact:", strconv.Ftoa64(x, 'g', 1000))
			println("\tgot exact: ", strconv.Ftoa64(t.f, 'g', 1000))
			ok = false
		}
	}
	if !ok {
		os.Exit(1)
	}
}
Esempio n. 15
0
func (m *Mat3) String() string {
	s := "["
	for i := 0; i < 9; i += 1 {
		s += strconv.Ftoa64(m.matrix[i], 'e', 2)
		if i < 8 {
			s += ", "
		}
	}
	s += "]"
	return s
}
Esempio n. 16
0
func (mt *MediaType) String() string {
	pieces := []string{
		mt.Type + "/" + mt.SubType,
	}
	for k, v := range mt.Params {
		pieces = append(pieces, k+"="+v)
	}
	if mt.Quality != 1.0 {
		pieces = append(pieces, "q="+strconv.Ftoa64(mt.Quality, 'g', -1))
	}
	return strings.Join(pieces, ";")
}
Esempio n. 17
0
func formatReflectValue(x reflect.Value) (string, os.Error) {
	/*
	   if !x.CanSet() {
	       return "", ErrorCantSet
	   }
	*/
	var (
		errc os.Error
		kind = x.Kind()
		//vintstr   string
	)
	switch kind {
	// Format pointers to standard types.
	case reflect.String:
		return x.Interface().(string), nil
	case reflect.Int:
		return strconv.Itoa(x.Interface().(int)), nil
	case reflect.Int8:
		return strconv.Itoa64(int64(x.Interface().(int8))), nil
	case reflect.Int16:
		return strconv.Itoa64(int64(x.Interface().(int16))), nil
	case reflect.Int32:
		return strconv.Itoa64(int64(x.Interface().(int32))), nil
	case reflect.Int64:
		return strconv.Itoa64(x.Interface().(int64)), nil
	case reflect.Uint:
		return strconv.Uitoa(x.Interface().(uint)), nil
	case reflect.Uint8:
		return strconv.Uitoa64(uint64(x.Interface().(uint8))), nil
	case reflect.Uint16:
		return strconv.Uitoa64(uint64(x.Interface().(uint16))), nil
	case reflect.Uint32:
		return strconv.Uitoa64(uint64(x.Interface().(uint32))), nil
	case reflect.Uint64:
		return strconv.Uitoa64(x.Interface().(uint64)), nil
	case reflect.Float32:
		return strconv.Ftoa32(x.Interface().(float32), FloatFmt, FloatPrec), nil
	case reflect.Float64:
		return strconv.Ftoa64(x.Interface().(float64), FloatFmt, FloatPrec), nil
	case reflect.Complex64:
		fallthrough
	case reflect.Complex128:
		errc = ErrorUnimplemented
	case reflect.Bool:
		return strconv.Btoa(x.Interface().(bool)), nil
	default:
		errc = ErrorFieldType
	}
	return "", errc
}
Esempio n. 18
0
/*
const mapping = map[string]string {
	"VARCHAR": "string",
	"TINYINT": "byte",
	"SMALLINT": "int16",
	"INT": "int",
	"FLOAT": "float32",
	"DOUBLE": "float64"
}
*/
func maptype(data map[string]interface{}) (result map[string]string) {
	result = make(map[string]string)
	v := r.ValueOf(data)
	for _, k := range v.MapKeys() {
		switch r.ValueOf(v.MapIndex(k).Interface()).Kind() {
		case r.String:
			result[k.Interface().(string)] = "'" + v.MapIndex(k).Interface().(string) + "'"
		case r.Int:
			result[k.Interface().(string)] = strconv.Itoa(v.MapIndex(k).Interface().(int))
		case r.Float64:
			result[k.Interface().(string)] = strconv.Ftoa64(v.MapIndex(k).Interface().(float64), 'f', -1)
		}
	}
	return
}
Esempio n. 19
0
func NewNumericFromDouble(dValue float64) Numeric {
	if math.IsInf(dValue, 1) {
		return INFINITY
	}
	if math.IsInf(dValue, -1) {
		return NEGATIVE_INFINITY
	}
	if math.IsNaN(dValue) {
		return NAN
	}
	iValue := int64(dValue)
	sValue := strconv.Ftoa64(dValue, 'g', 10)
	isNil := false
	return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
}
Esempio n. 20
0
File: main.go Progetto: ajees/NIL
func test_atomaton() {
	counters := make([]float64, 4)
	numruns := 10000.0

	for i := 0; i < len(counters); i++ {
		counters[i] = 0.0
	}

	for i := 0.0; i < numruns; i++ {
		counters[hmm.Getnextstate()]++
	}

	for i := 0; i < len(counters); i++ {
		fmt.Printf("state " + strconv.Itoa(i) + " percentage = " + strconv.Ftoa64((counters[i]*100)/numruns, 'f', 5) + "\n")
	}
}
Esempio n. 21
0
func handler(ws *websocket.Conn) {
	x := 0.
	for {
		if x >= 2*math.Pi {
			x = 0
		} else {
			x += 0.05
		}

		time.Sleep(500 * 1000 * 1000) // sleep for 500ms (Sleep takes nanoseconds)

		msg := strconv.Ftoa64(math.Sin(x), 'g', 10)
		log.Printf("%v sending: %v\n", ws, msg)
		ws.Write([]byte(msg))
	}
}
Esempio n. 22
0
File: hello.go Progetto: wtaysom/tau
func sineServer(w http.ResponseWriter, req *http.Request) {
	x := 0.
	for {
		if x >= 2*math.Pi {
			x = 0
		} else {
			x += 0.05
		}

		time.Sleep(500 * 1000 * 1000) // sleep for 500ms (Sleep takes nanoseconds)

		msg := strconv.Ftoa64(math.Sin(x), 'g', 10)
		fmt.Printf("%v sending: %v\n", w, msg)
		w.Write([]byte(msg))
	}
}
Esempio n. 23
0
func valueToString(v reflect.Value) (string, os.Error) {
	if !v.IsValid() {
		return "null", nil
	}

	switch v.Kind() {
	case reflect.Ptr:
		return valueToString(reflect.Indirect(v))
	case reflect.Interface:
		return valueToString(v.Elem())
	case reflect.Bool:
		x := v.Bool()
		if x {
			return "true", nil
		} else {
			return "false", nil
		}

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return strconv.Itoa64(v.Int()), nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return strconv.Uitoa64(v.Uint()), nil
	case reflect.UnsafePointer:
		return strconv.Uitoa64(uint64(v.Pointer())), nil

	case reflect.Float32, reflect.Float64:
		return strconv.Ftoa64(v.Float(), 'g', -1), nil

	case reflect.String:
		return v.String(), nil

	//This is kind of a rough hack to replace the old []byte
	//detection with reflect.Uint8Type, it doesn't catch
	//zero-length byte slices
	case reflect.Slice:
		typ := v.Type()
		if typ.Elem().Kind() == reflect.Uint || typ.Elem().Kind() == reflect.Uint8 || typ.Elem().Kind() == reflect.Uint16 || typ.Elem().Kind() == reflect.Uint32 || typ.Elem().Kind() == reflect.Uint64 || typ.Elem().Kind() == reflect.Uintptr {
			if v.Len() > 0 {
				if v.Index(1).OverflowUint(257) {
					return string(v.Interface().([]byte)), nil
				}
			}
		}
	}
	return "", os.NewError("Unsupported type")
}
// Encode values fro each field
func encodeParamValues(a []interface{}) ([]byte, int) {
	var b []byte
	for i := 0; i < len(a); i++ {
		f := a[i]
		switch t := f.(type) {
		case string:
			b = append(b, packString(string(t))...)
		case int:
			b = append(b, packString(strconv.Itoa(int(t)))...)
		case float32:
			b = append(b, packString(strconv.Ftoa32(float32(t), 'f', -1))...)
		case float64:
			b = append(b, packString(strconv.Ftoa64(float64(t), 'f', -1))...)
		}
	}
	return b, len(b)
}
Esempio n. 25
0
func valueToString(v reflect.Value) (string, os.Error) {
	if v == nil {
		return "null", nil
	}

	switch v := v.(type) {
	case *reflect.PtrValue:
		return valueToString(reflect.Indirect(v))
	case *reflect.InterfaceValue:
		return valueToString(v.Elem())
	case *reflect.BoolValue:
		x := v.Get()
		if x {
			return "true", nil
		} else {
			return "false", nil
		}

	case *reflect.IntValue:
		return strconv.Itoa64(v.Get()), nil
	case *reflect.UintValue:
		return strconv.Uitoa64(v.Get()), nil
	case *reflect.UnsafePointerValue:
		return strconv.Uitoa64(uint64(v.Get())), nil

	case *reflect.FloatValue:
		return strconv.Ftoa64(v.Get(), 'g', -1), nil

	case *reflect.StringValue:
		return v.Get(), nil

	//This is kind of a rough hack to replace the old []byte
	//detection with reflect.Uint8Type, it doesn't catch
	//zero-length byte slices
	case *reflect.SliceValue:
		typ := v.Type().(*reflect.SliceType)
		if _, ok := typ.Elem().(*reflect.UintType); ok {
			if v.Len() > 0 {
				if v.Elem(1).(*reflect.UintValue).Overflow(257) {
					return string(v.Interface().([]byte)), nil
				}
			}
		}
	}
	return "", os.NewError("Unsupported type")
}
Esempio n. 26
0
func (m *SelectManager) Project(any interface{}) *SelectManager {
	switch val := any.(type) {
	case ast.Node:
		m.project(val)
	case string:
		m.project(ast.NewSqlLiteral(any.(string)))
	case bool:
		m.project(ast.NewSqlLiteral(strconv.Btoa(any.(bool))))
	case int:
		m.project(ast.NewSqlLiteral(strconv.Itoa(any.(int))))
	case int64:
		m.project(ast.NewSqlLiteral(strconv.Itoa64(any.(int64))))
	case float32:
		m.project(ast.NewSqlLiteral(strconv.Ftoa32(any.(float32), 'f', 0)))
	case float64:
		m.project(ast.NewSqlLiteral(strconv.Ftoa64(any.(float64), 'f', 0)))
	}
	return m
}
Esempio n. 27
0
func primitiveToString(v reflect.Value) string {

	switch v.Kind() {

	case reflect.Bool:
		if v.Bool() {
			return "1"
		}
		return "0"
	case reflect.Int:
		fallthrough
	case reflect.Int8:
		fallthrough
	case reflect.Int16:
		fallthrough
	case reflect.Int32:
		fallthrough
	case reflect.Int64:
		return strconv.Itoa64(v.Int())

	case reflect.Uint:
		fallthrough
	case reflect.Uint8:
		fallthrough
	case reflect.Uint16:
		fallthrough
	case reflect.Uint32:
		fallthrough
	case reflect.Uint64:
		return strconv.Uitoa64(v.Uint())

	case reflect.Float32:
		fallthrough
	case reflect.Float64:
		return strconv.Ftoa64(v.Float(), 'g', 5)
	case reflect.String:
		return v.String()
	}

	return "(unknown type " + string(v.Kind()) + ")"
}
Esempio n. 28
0
func JSONValueToString(value interface{}) string {
	switch v := value.(type) {
	case nil:
		return ""
	case string:
		return v
	case int:
		return strconv.Itoa(v)
	case int64:
		return strconv.Itoa64(v)
	case float64:
		return strconv.Ftoa64(v, 'g', -1)
	case bool:
		if v {
			return "true"
		}
		return "false"
	}
	bytes, _ := json.Marshal(value)
	return string(bytes)
}
Esempio n. 29
0
func (nv *NumberValidator) Validate(s string) ValidationStatus {
	num, err := strconv.Atof64(s)
	if err != nil {
		return Invalid
	}

	if num < nv.minValue {
		return Partial
	}

	if num > nv.maxValue {
		return Invalid
	}

	str := strconv.Ftoa64(num, 'f', nv.decimals)

	if s != str {
		return Invalid
	}

	return Valid
}
Esempio n. 30
0
func (c *ToSql) Convert(unknown interface{}) (s string) {
	switch val := unknown.(type) {
	case string:
		s = strconv.Quote(val)
	case bool:
		s = strconv.Btoa(val)
	case int:
		s = strconv.Itoa(val)
	case int64:
		s = strconv.Itoa64(val)
	case uint:
		s = strconv.Uitoa(val)
	case uint64:
		s = strconv.Uitoa64(val)
	case float32:
		s = strconv.Ftoa32(val, 'f', -1)
	case float64:
		s = strconv.Ftoa64(val, 'f', -1)
	default:
		s = c.ConvertUnknown(unknown)
	}
	return
}