本文整理汇总了Golang中go/constant.Float64Val函数的典型用法代码示例。如果您正苦于以下问题:Golang Float64Val函数的具体用法?Golang Float64Val怎么用?Golang Float64Val使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Float64Val函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setValue
func (v *Variable) setValue(y *Variable) error {
var err error
switch v.Kind {
case reflect.Float32, reflect.Float64:
f, _ := constant.Float64Val(y.Value)
err = v.writeFloatRaw(f, v.RealType.Size())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, _ := constant.Int64Val(y.Value)
err = v.writeUint(uint64(n), v.RealType.Size())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, _ := constant.Uint64Val(y.Value)
err = v.writeUint(n, v.RealType.Size())
case reflect.Bool:
err = v.writeBool(constant.BoolVal(y.Value))
case reflect.Complex64, reflect.Complex128:
real, _ := constant.Float64Val(constant.Real(y.Value))
imag, _ := constant.Float64Val(constant.Imag(y.Value))
err = v.writeComplex(real, imag, v.RealType.Size())
default:
fmt.Printf("default\n")
if t, isptr := v.RealType.(*dwarf.PtrType); isptr {
err = v.writeUint(uint64(y.Children[0].Addr), int64(t.ByteSize))
} else {
return fmt.Errorf("can not set variables of type %s (not implemented)", v.Kind.String())
}
}
return err
}
示例2: genConst
func (g *javaGen) genConst(o *types.Const) {
// TODO(hyangah): should const names use upper cases + "_"?
// TODO(hyangah): check invalid names.
jType := g.javaType(o.Type())
val := o.Val().String()
switch b := o.Type().(*types.Basic); b.Kind() {
case types.Int64, types.UntypedInt:
i, exact := constant.Int64Val(o.Val())
if !exact {
g.errorf("const value %s for %s cannot be represented as %s", val, o.Name(), jType)
return
}
val = fmt.Sprintf("%dL", i)
case types.Float32:
f, _ := constant.Float32Val(o.Val())
val = fmt.Sprintf("%gf", f)
case types.Float64, types.UntypedFloat:
f, _ := constant.Float64Val(o.Val())
if math.IsInf(f, 0) || math.Abs(f) > math.MaxFloat64 {
g.errorf("const value %s for %s cannot be represented as %s", val, o.Name(), jType)
return
}
val = fmt.Sprintf("%g", f)
}
g.Printf("public static final %s %s = %s;\n", g.javaType(o.Type()), o.Name(), val)
}
示例3: goVal
// goVal returns the Go value for val, or nil.
func goVal(val constant.Value) interface{} {
// val should exist, but be conservative and check
if val == nil {
return nil
}
// Match implementation restriction of other compilers.
// gc only checks duplicates for integer, floating-point
// and string values, so only create Go values for these
// types.
switch val.Kind() {
case constant.Int:
if x, ok := constant.Int64Val(val); ok {
return x
}
if x, ok := constant.Uint64Val(val); ok {
return x
}
case constant.Float:
if x, ok := constant.Float64Val(val); ok {
return x
}
case constant.String:
return constant.StringVal(val)
}
return nil
}
示例4: roundFloat64
func roundFloat64(x exact.Value) exact.Value {
f, _ := exact.Float64Val(x)
if !math.IsInf(f, 0) {
return exact.MakeFloat64(f)
}
return nil
}
示例5: constValString
// constValString emulates Go 1.6's go/constant.ExactString well enough
// to make the tests pass. This is just a stopgap until we throw away
// all the *15.go files.
func constValString(v exact.Value) string {
if v.Kind() == exact.Float {
f, _ := exact.Float64Val(v)
return fmt.Sprintf("%g", f)
}
return v.String()
}
示例6: roundFloat64
func roundFloat64(x constant.Value) constant.Value {
f, _ := constant.Float64Val(x)
if !math.IsInf(f, 0) {
return constant.MakeFloat64(f)
}
return nil
}
示例7: constValString
// Helper function to adjust go1.5 numeric go/constant formatting.
// Can be removed once we give up compatibility with go1.5.
func constValString(v exact.Value) string {
if v.Kind() == exact.Float {
// In go1.5, go/constant floating-point values are printed
// as fractions. Make them appear as floating-point numbers.
f, _ := exact.Float64Val(v)
return fmt.Sprintf("%g", f)
}
return v.String()
}
示例8: ConvertVar
func ConvertVar(v *proc.Variable) *Variable {
r := Variable{
Addr: v.Addr,
Name: v.Name,
Kind: v.Kind,
Len: v.Len,
Cap: v.Cap,
}
if v.DwarfType != nil {
r.Type = v.DwarfType.String()
}
if v.RealType != nil {
r.RealType = v.RealType.String()
}
if v.Unreadable != nil {
r.Unreadable = v.Unreadable.Error()
}
if v.Value != nil {
switch v.Kind {
case reflect.Float32:
f, _ := constant.Float64Val(v.Value)
r.Value = strconv.FormatFloat(f, 'f', -1, 32)
case reflect.Float64:
f, _ := constant.Float64Val(v.Value)
r.Value = strconv.FormatFloat(f, 'f', -1, 64)
case reflect.String, reflect.Func:
r.Value = constant.StringVal(v.Value)
default:
r.Value = v.Value.String()
}
}
r.Children = make([]Variable, len(v.Children))
for i := range v.Children {
r.Children[i] = *ConvertVar(&v.Children[i])
}
return &r
}
示例9: Uint64
// Uint64 returns the numeric value of this constant truncated to fit
// an unsigned 64-bit integer.
//
func (c *Const) Uint64() uint64 {
switch x := c.Value; x.Kind() {
case exact.Int:
if u, ok := exact.Uint64Val(x); ok {
return u
}
return 0
case exact.Float:
f, _ := exact.Float64Val(x)
return uint64(f)
}
panic(fmt.Sprintf("unexpected constant value: %T", c.Value))
}
示例10: Int64
// Int64 returns the numeric value of this constant truncated to fit
// a signed 64-bit integer.
//
func (c *Const) Int64() int64 {
switch x := exact.ToInt(c.Value); x.Kind() {
case exact.Int:
if i, ok := exact.Int64Val(x); ok {
return i
}
return 0
case exact.Float:
f, _ := exact.Float64Val(x)
return int64(f)
}
panic(fmt.Sprintf("unexpected constant value: %T", c.Value))
}
示例11: genConstM
func (g *ObjcGen) genConstM(o *types.Const) {
if _, ok := o.Type().(*types.Basic); !ok {
g.Printf("// skipped const %s with unsupported type: %T\n\n", o.Name(), o)
return
}
cName := fmt.Sprintf("%s%s", g.namePrefix, o.Name())
objcType := g.objcType(o.Type())
switch b := o.Type().(*types.Basic); b.Kind() {
case types.Bool, types.UntypedBool:
v := "NO"
if constant.BoolVal(o.Val()) {
v = "YES"
}
g.Printf("const BOOL %s = %s;\n", cName, v)
case types.String, types.UntypedString:
g.Printf("NSString* const %s = @%s;\n", cName, constExactString(o))
case types.Int, types.Int8, types.Int16, types.Int32:
g.Printf("const %s %s = %s;\n", objcType, cName, o.Val())
case types.Int64, types.UntypedInt:
i, exact := constant.Int64Val(o.Val())
if !exact {
g.errorf("const value %s for %s cannot be represented as %s", o.Val(), o.Name(), objcType)
return
}
if i == math.MinInt64 {
// -9223372036854775808LL does not work because 922337203685477508 is
// larger than max int64.
g.Printf("const int64_t %s = %dLL-1;\n", cName, i+1)
} else {
g.Printf("const int64_t %s = %dLL;\n", cName, i)
}
case types.Float32, types.Float64, types.UntypedFloat:
f, _ := constant.Float64Val(o.Val())
if math.IsInf(f, 0) || math.Abs(f) > math.MaxFloat64 {
g.errorf("const value %s for %s cannot be represented as double", o.Val(), o.Name())
return
}
g.Printf("const %s %s = %g;\n", objcType, cName, f)
default:
g.errorf("unsupported const type %s for %s", b, o.Name())
}
}
示例12: float
func (p *exporter) float(x constant.Value) {
if x.Kind() != constant.Float {
log.Fatalf("gcimporter: unexpected constant %v, want float", x)
}
// extract sign (there is no -0)
sign := constant.Sign(x)
if sign == 0 {
// x == 0
p.int(0)
return
}
// x != 0
var f big.Float
if v, exact := constant.Float64Val(x); exact {
// float64
f.SetFloat64(v)
} else if num, denom := constant.Num(x), constant.Denom(x); num.Kind() == constant.Int {
// TODO(gri): add big.Rat accessor to constant.Value.
r := valueToRat(num)
f.SetRat(r.Quo(r, valueToRat(denom)))
} else {
// Value too large to represent as a fraction => inaccessible.
// TODO(gri): add big.Float accessor to constant.Value.
f.SetFloat64(math.MaxFloat64) // FIXME
}
// extract exponent such that 0.5 <= m < 1.0
var m big.Float
exp := f.MantExp(&m)
// extract mantissa as *big.Int
// - set exponent large enough so mant satisfies mant.IsInt()
// - get *big.Int from mant
m.SetMantExp(&m, int(m.MinPrec()))
mant, acc := m.Int(nil)
if acc != big.Exact {
log.Fatalf("gcimporter: internal error")
}
p.int(sign)
p.int(exp)
p.string(string(mant.Bytes()))
}
示例13: ResolveAsType
// ResolveAsType implements the Constant interface.
func (expr *NumVal) ResolveAsType(typ Datum) (Datum, error) {
switch {
case typ.TypeEqual(TypeInt):
i, exact := constant.Int64Val(constant.ToInt(expr.Value))
if !exact {
return nil, fmt.Errorf("integer value out of range: %v", expr.Value)
}
return NewDInt(DInt(i)), nil
case typ.TypeEqual(TypeFloat):
f, _ := constant.Float64Val(constant.ToFloat(expr.Value))
return NewDFloat(DFloat(f)), nil
case typ.TypeEqual(TypeDecimal):
dd := &DDecimal{}
s := expr.ExactString()
if idx := strings.IndexRune(s, '/'); idx != -1 {
// Handle constant.ratVal, which will return a rational string
// like 6/7. If only we could call big.Rat.FloatString() on it...
num, den := s[:idx], s[idx+1:]
if _, ok := dd.SetString(num); !ok {
return nil, fmt.Errorf("could not evaluate numerator of %v as Datum type DDecimal "+
"from string %q", expr, num)
}
denDec := new(inf.Dec)
if _, ok := denDec.SetString(den); !ok {
return nil, fmt.Errorf("could not evaluate denominator %v as Datum type DDecimal "+
"from string %q", expr, den)
}
dd.QuoRound(&dd.Dec, denDec, decimal.Precision, inf.RoundHalfUp)
} else {
if _, ok := dd.SetString(s); !ok {
return nil, fmt.Errorf("could not evaluate %v as Datum type DDecimal from "+
"string %q", expr, s)
}
}
return dd, nil
default:
return nil, fmt.Errorf("could not resolve %T %v into a %T", expr, expr, typ)
}
}
示例14: Float64
// Float64 returns the numeric value of this constant truncated to fit
// a float64.
//
func (c *Const) Float64() float64 {
f, _ := exact.Float64Val(c.Value)
return f
}
示例15: ConvertVar
// ConvertVar converts from proc.Variable to api.Variable.
func ConvertVar(v *proc.Variable) *Variable {
r := Variable{
Addr: v.Addr,
OnlyAddr: v.OnlyAddr,
Name: v.Name,
Kind: v.Kind,
Len: v.Len,
Cap: v.Cap,
}
r.Type = prettyTypeName(v.DwarfType)
r.RealType = prettyTypeName(v.RealType)
if v.Unreadable != nil {
r.Unreadable = v.Unreadable.Error()
}
if v.Value != nil {
switch v.Kind {
case reflect.Float32:
f, _ := constant.Float64Val(v.Value)
r.Value = strconv.FormatFloat(f, 'f', -1, 32)
case reflect.Float64:
f, _ := constant.Float64Val(v.Value)
r.Value = strconv.FormatFloat(f, 'f', -1, 64)
case reflect.String, reflect.Func:
r.Value = constant.StringVal(v.Value)
default:
r.Value = v.Value.String()
}
}
switch v.Kind {
case reflect.Complex64:
r.Children = make([]Variable, 2)
r.Len = 2
real, _ := constant.Float64Val(constant.Real(v.Value))
imag, _ := constant.Float64Val(constant.Imag(v.Value))
r.Children[0].Name = "real"
r.Children[0].Kind = reflect.Float32
r.Children[0].Value = strconv.FormatFloat(real, 'f', -1, 32)
r.Children[1].Name = "imaginary"
r.Children[1].Kind = reflect.Float32
r.Children[1].Value = strconv.FormatFloat(imag, 'f', -1, 32)
case reflect.Complex128:
r.Children = make([]Variable, 2)
r.Len = 2
real, _ := constant.Float64Val(constant.Real(v.Value))
imag, _ := constant.Float64Val(constant.Imag(v.Value))
r.Children[0].Name = "real"
r.Children[0].Kind = reflect.Float64
r.Children[0].Value = strconv.FormatFloat(real, 'f', -1, 64)
r.Children[1].Name = "imaginary"
r.Children[1].Kind = reflect.Float64
r.Children[1].Value = strconv.FormatFloat(imag, 'f', -1, 64)
default:
r.Children = make([]Variable, len(v.Children))
for i := range v.Children {
r.Children[i] = *ConvertVar(&v.Children[i])
}
}
return &r
}