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


Golang Value.Complex方法代码示例

本文整理汇总了Golang中reflect.Value.Complex方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Complex方法的具体用法?Golang Value.Complex怎么用?Golang Value.Complex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Value的用法示例。


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

示例1: checkAgainstComplex64

func checkAgainstComplex64(e complex64, c reflect.Value) (err error) {
	err = errors.New("")
	realPart := real(e)
	imaginaryPart := imag(e)

	switch {
	case isInteger(c) || isFloat(c):
		// If we have no imaginary part, then we should just compare against the
		// real part. Otherwise, we can't be equal.
		if imaginaryPart != 0 {
			return
		}

		return checkAgainstFloat32(realPart, c)

	case isComplex(c):
		// Compare using complex64 to avoid a false sense of precision; otherwise
		// e.g. Equals(0.1 + 0i) won't match float32(0.1).
		if complex64(c.Complex()) == e {
			err = nil
		}

	default:
		err = NewFatalError("which is not numeric")
	}

	return
}
开发者ID:Xetius,项目名称:grafana,代码行数:28,代码来源:equals.go

示例2: checkAgainstComplex128

func checkAgainstComplex128(e complex128, c reflect.Value) (err error) {
	err = errors.New("")
	realPart := real(e)
	imaginaryPart := imag(e)

	switch {
	case isInteger(c) || isFloat(c):
		// If we have no imaginary part, then we should just compare against the
		// real part. Otherwise, we can't be equal.
		if imaginaryPart != 0 {
			return
		}

		return checkAgainstFloat64(realPart, c)

	case isComplex(c):
		if c.Complex() == e {
			err = nil
		}

	default:
		err = NewFatalError("which is not numeric")
	}

	return
}
开发者ID:Xetius,项目名称:grafana,代码行数:26,代码来源:equals.go

示例3: nonzero

func nonzero(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.Bool:
		return v.Bool()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int() != 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return v.Uint() != 0
	case reflect.Float32, reflect.Float64:
		return v.Float() != 0
	case reflect.Complex64, reflect.Complex128:
		return v.Complex() != complex(0, 0)
	case reflect.String:
		return v.String() != ""
	case reflect.Struct:
		for i := 0; i < v.NumField(); i++ {
			if nonzero(getField(v, i)) {
				return true
			}
		}
		return false
	case reflect.Array:
		for i := 0; i < v.Len(); i++ {
			if nonzero(v.Index(i)) {
				return true
			}
		}
		return false
	case reflect.Map, reflect.Interface, reflect.Slice, reflect.Ptr, reflect.Chan, reflect.Func:
		return !v.IsNil()
	case reflect.UnsafePointer:
		return v.Pointer() != 0
	}
	return true
}
开发者ID:tgzzzH,项目名称:go-swagger,代码行数:35,代码来源:zero.go

示例4: builtinImag

func builtinImag(cplx reflect.Value) reflect.Value {
	if cplx.Type() == c128 {
		return reflect.ValueOf(float64(imag(cplx.Complex())))
	} else {
		return reflect.ValueOf(float32(imag(cplx.Complex())))
	}
}
开发者ID:rocky,项目名称:go-eval,代码行数:7,代码来源:builtins.go

示例5: encodeBasic

func encodeBasic(v reflect.Value) string {
	t := v.Type()
	switch k := t.Kind(); k {
	case reflect.Bool:
		return strconv.FormatBool(v.Bool())
	case reflect.Int,
		reflect.Int8,
		reflect.Int16,
		reflect.Int32,
		reflect.Int64:
		return strconv.FormatInt(v.Int(), 10)
	case reflect.Uint,
		reflect.Uint8,
		reflect.Uint16,
		reflect.Uint32,
		reflect.Uint64:
		return strconv.FormatUint(v.Uint(), 10)
	case reflect.Float32:
		return strconv.FormatFloat(v.Float(), 'g', -1, 32)
	case reflect.Float64:
		return strconv.FormatFloat(v.Float(), 'g', -1, 64)
	case reflect.Complex64, reflect.Complex128:
		s := fmt.Sprintf("%g", v.Complex())
		return strings.TrimSuffix(strings.TrimPrefix(s, "("), ")")
	case reflect.String:
		return v.String()
	}
	panic(t.String() + " has unsupported kind " + t.Kind().String())
}
开发者ID:henryren,项目名称:form,代码行数:29,代码来源:encode.go

示例6: encodeCol

// Returns the string representation of the field value
func (enc *encoder) encodeCol(fv reflect.Value, st reflect.StructTag) string {
	switch fv.Kind() {
	case reflect.String:
		return fv.String()
	case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
		return fmt.Sprintf("%v", fv.Int())
	case reflect.Float32:
		return encodeFloat(32, fv)
	case reflect.Float64:
		return encodeFloat(64, fv)
	case reflect.Bool:
		return encodeBool(fv.Bool(), st)
	case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
		return fmt.Sprintf("%v", fv.Uint())
	case reflect.Complex64, reflect.Complex128:
		return fmt.Sprintf("%+.3g", fv.Complex())
	case reflect.Interface:
		return encodeInterface(fv, st)
	case reflect.Struct:
		return encodeInterface(fv, st)
	default:
		panic(fmt.Sprintf("Unsupported type %s", fv.Kind()))
	}

	return ""
}
开发者ID:toophy,项目名称:csv,代码行数:27,代码来源:encode.go

示例7: evalBinaryComplexExpr

func evalBinaryComplexExpr(x reflect.Value, op token.Token, y reflect.Value) (reflect.Value, error) {
	var r complex128
	var is_bool bool
	var b bool

	xx, yy := x.Complex(), y.Complex()
	switch op {
	case token.ADD:
		r = xx + yy
	case token.SUB:
		r = xx - yy
	case token.MUL:
		r = xx * yy
	case token.QUO:
		if yy == 0 {
			return reflect.Value{}, PanicDivideByZero{}
		}
		r = xx / yy
	case token.EQL:
		b = xx == yy
		is_bool = true
	case token.NEQ:
		b = xx != yy
		is_bool = true
	default:
		panic(dytc("bad binary op"))
	}
	if is_bool {
		return reflect.ValueOf(b), nil
	} else {
		return reflect.ValueOf(r).Convert(x.Type()), nil
	}
}
开发者ID:philipmulcahy,项目名称:godebug,代码行数:33,代码来源:evalbinaryexpr.go

示例8: isZero

// isZero reports whether the value is the zero of its type.
func isZero(val reflect.Value) bool {
	switch val.Kind() {
	case reflect.Array:
		for i := 0; i < val.Len(); i++ {
			if !isZero(val.Index(i)) {
				return false
			}
		}
		return true
	case reflect.Map, reflect.Slice, reflect.String:
		return val.Len() == 0
	case reflect.Bool:
		return !val.Bool()
	case reflect.Complex64, reflect.Complex128:
		return val.Complex() == 0
	case reflect.Chan, reflect.Func, reflect.Ptr:
		return val.IsNil()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return val.Int() == 0
	case reflect.Float32, reflect.Float64:
		return val.Float() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return val.Uint() == 0
	case reflect.Struct:
		for i := 0; i < val.NumField(); i++ {
			if !isZero(val.Field(i)) {
				return false
			}
		}
		return true
	}
	panic("unknown type in isZero " + val.Type().String())
}
开发者ID:ds2dev,项目名称:gcc,代码行数:34,代码来源:encode.go

示例9: formatValue

func formatValue(value reflect.Value, indentation uint) string {
	if indentation > MaxDepth {
		return "..."
	}

	if isNilValue(value) {
		return "nil"
	}

	if UseStringerRepresentation {
		if value.CanInterface() {
			obj := value.Interface()
			switch x := obj.(type) {
			case fmt.GoStringer:
				return x.GoString()
			case fmt.Stringer:
				return x.String()
			}
		}
	}

	switch value.Kind() {
	case reflect.Bool:
		return fmt.Sprintf("%v", value.Bool())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return fmt.Sprintf("%v", value.Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return fmt.Sprintf("%v", value.Uint())
	case reflect.Uintptr:
		return fmt.Sprintf("0x%x", value.Uint())
	case reflect.Float32, reflect.Float64:
		return fmt.Sprintf("%v", value.Float())
	case reflect.Complex64, reflect.Complex128:
		return fmt.Sprintf("%v", value.Complex())
	case reflect.Chan:
		return fmt.Sprintf("0x%x", value.Pointer())
	case reflect.Func:
		return fmt.Sprintf("0x%x", value.Pointer())
	case reflect.Ptr:
		return formatValue(value.Elem(), indentation)
	case reflect.Slice:
		return formatSlice(value, indentation)
	case reflect.String:
		return formatString(value.String(), indentation)
	case reflect.Array:
		return formatSlice(value, indentation)
	case reflect.Map:
		return formatMap(value, indentation)
	case reflect.Struct:
		return formatStruct(value, indentation)
	case reflect.Interface:
		return formatValue(value.Elem(), indentation)
	default:
		if value.CanInterface() {
			return fmt.Sprintf("%#v", value.Interface())
		} else {
			return fmt.Sprintf("%#v", value)
		}
	}
}
开发者ID:cloudfoundry-incubator,项目名称:cf-mysql-bootstrap,代码行数:60,代码来源:format.go

示例10: isZeroValue

func isZeroValue(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16,
		reflect.Int32, reflect.Int64:
		return 0 == v.Int()

	case reflect.Uint, reflect.Uint8, reflect.Uint16,
		reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return 0 == v.Uint()

	case reflect.String:
		return "" == v.String()

	case reflect.Bool:
		return !v.Bool()

	case reflect.Float32, reflect.Float64:
		return 0 == v.Float()

	case reflect.Complex64, reflect.Complex128:
		return 0 == real(v.Complex()) && 0 == imag(v.Complex())

	}
	return false
}
开发者ID:ysohta,项目名称:gopl-ex,代码行数:25,代码来源:encode.go

示例11: isTrue

// isTrue reports whether the value is 'true', in the sense of not the zero of its type,
// and whether the value has a meaningful truth value.
func isTrue(val reflect.Value) (truth, ok bool) {
	if !val.IsValid() {
		// Something like var x interface{}, never set. It's a form of nil.
		return false, true
	}
	switch val.Kind() {
	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
		truth = val.Len() > 0
	case reflect.Bool:
		truth = val.Bool()
	case reflect.Complex64, reflect.Complex128:
		truth = val.Complex() != 0
	case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Interface:
		truth = !val.IsNil()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		truth = val.Int() != 0
	case reflect.Float32, reflect.Float64:
		truth = val.Float() != 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		truth = val.Uint() != 0
	case reflect.Struct:
		truth = true // Struct values are always true.
	default:
		return
	}
	return truth, true
}
开发者ID:bibbyflyaway,项目名称:go,代码行数:29,代码来源:exec.go

示例12: reflectValue

func reflectValue(f *reflect.StructField, v reflect.Value) *Ident {
	ident := new(Ident)

	// Evaluate primitive types.
	switch f.Type.Kind() {
	case reflect.String:
		ident.Name = v.String()

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		ident.Name = fmt.Sprint(v.Int())

	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		ident.Name = fmt.Sprint(v.Uint())

	case reflect.Float32, reflect.Float64:
		ident.Name = fmt.Sprint(v.Float())

	case reflect.Bool:
		ident.Name = fmt.Sprint(v.Bool())

	case reflect.Complex64, reflect.Complex128:
		ident.Name = fmt.Sprint(v.Complex())

	case reflect.Struct:
		// Check for interface and custom types.
		switch x := v.Interface().(type) {
		case Identifier:
			ident = x.Ident()

		case time.Time:
			ident.Name = chrono.Format(x)

		case fmt.Stringer:
			ident.Name = x.String()
		}

	case reflect.Interface, reflect.Ptr:
		if v.IsNil() {
			return nil
		}

		// Check for interface and custom types.
		switch x := v.Interface().(type) {
		case Identifier:
			ident = x.Ident()

		case time.Time:
			ident.Name = chrono.Format(x)

		case fmt.Stringer:
			ident.Name = x.String()
		}

	default:
		logrus.Debugf("origins: skipping unsupported field %s (%s type)", f.Name, f.Type.Kind())
		return nil
	}

	return ident
}
开发者ID:l-k-,项目名称:origins,代码行数:60,代码来源:reflect.go

示例13: refToVal

func refToVal(ref reflect.Value) Value {
	switch ref.Kind() {
	case reflect.Bool:
		return boolValue(ref.Bool())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return intValue(ref.Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
		reflect.Uint64, reflect.Uintptr:
		return intValue(ref.Uint())
	case reflect.Float32, reflect.Float64:
		return floatValue(ref.Float())
	case reflect.Complex64, reflect.Complex128:
		return complexValue(ref.Complex())
	case reflect.Array, reflect.Slice:
		return arrayValue{reflectValue(ref)}
	case reflect.Chan:
		return chanValue{reflectValue(ref)}
	case reflect.Map:
		return mapValue{reflectValue(ref)}
	case reflect.Ptr:
		return pointerValue{reflectValue(ref)}
	case reflect.String:
		return stringValue(ref.String())
	case reflect.Struct:
		return structValue{reflectValue(ref)}
	}
	return nilValue(0)
}
开发者ID:edsrzf,项目名称:template,代码行数:28,代码来源:value.go

示例14: complex64Encoder

func complex64Encoder(enc *encoder, v reflect.Value) error {
	bs := enc.buf[:8]
	x := v.Complex()
	enc.order.PutUint32(bs, math.Float32bits(float32(real(x))))
	enc.order.PutUint32(bs[4:], math.Float32bits(float32(imag(x))))
	_, err := enc.Write(bs)
	return err
}
开发者ID:rainycape,项目名称:gondola,代码行数:8,代码来源:encoder.go

示例15: putReflect

func putReflect(b []byte, v reflect.Value) int {
	v = reflect.Indirect(v)
	switch v.Kind() {
	case reflect.String:
		s := v.String()
		PutString(b, s)
		return len(s) + 1
	case reflect.Bool:
		PutBool(b, v.Bool())
		return 1
	case reflect.Int:
		PutInt(b, int(v.Int()))
		return 8
	case reflect.Uint:
		PutUint(b, uint(v.Uint()))
		return 8
	case reflect.Int8:
		PutInt8(b, int8(v.Int()))
	case reflect.Uint8:
		PutUint8(b, uint8(v.Uint()))
	case reflect.Int16:
		PutInt16(b, int16(v.Int()))
	case reflect.Uint16:
		PutUint16(b, uint16(v.Uint()))
	case reflect.Int32:
		PutInt32(b, int32(v.Int()))
	case reflect.Uint32:
		PutUint32(b, uint32(v.Uint()))
	case reflect.Int64:
		PutInt64(b, v.Int())
	case reflect.Uint64:
		PutUint64(b, v.Uint())
	case reflect.Float32:
		PutFloat32(b, float32(v.Float()))
	case reflect.Float64:
		PutFloat64(b, v.Float())
	case reflect.Complex64:
		PutComplex64(b, complex64(v.Complex()))
	case reflect.Complex128:
		PutComplex128(b, v.Complex())
	case reflect.Struct:
		sum := 0
		for i, n := 0, v.NumField(); i < n; i++ {
			s := putReflect(b[sum:], v.Field(i))
			if s < 0 {
				return -1
			}
			sum += s
		}
		if sum == 0 {
			return -1
		}
		return sum
	default:
		return -1
	}
	return int(v.Type().Size())
}
开发者ID:pennydb,项目名称:lex,代码行数:58,代码来源:lex.go


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