本文整理汇总了Golang中github.com/cockroachdb/cockroach/util/decimal.SetFromFloat函数的典型用法代码示例。如果您正苦于以下问题:Golang SetFromFloat函数的具体用法?Golang SetFromFloat怎么用?Golang SetFromFloat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetFromFloat函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: floatOrDecimalBuiltin1
func floatOrDecimalBuiltin1(f func(float64) (Datum, error)) []builtin {
return []builtin{
{
types: argTypes{floatType},
returnType: typeFloat,
fn: func(_ EvalContext, args DTuple) (Datum, error) {
return f(float64(args[0].(DFloat)))
},
}, {
types: argTypes{decimalType},
returnType: typeDecimal,
fn: func(_ EvalContext, args DTuple) (Datum, error) {
dec := args[0].(*DDecimal)
v, err := decimal.Float64FromDec(&dec.Dec)
if err != nil {
return nil, err
}
r, err := f(v)
if err != nil {
return r, err
}
rf := float64(r.(DFloat))
if math.IsNaN(rf) || math.IsInf(rf, 0) {
// TODO(nvanbenschoten) NaN semmantics should be introduced
// into the decimal library to support it here.
return nil, fmt.Errorf("decimal does not support NaN")
}
dd := &DDecimal{}
decimal.SetFromFloat(&dd.Dec, rf)
return dd, nil
},
},
}
}
示例2: ddecimal
func ddecimal(f float64) copyableExpr {
return func() Expr {
dd := &DDecimal{}
decimal.SetFromFloat(&dd.Dec, f)
return dd
}
}
示例3: makeDecimalTestDatum
func makeDecimalTestDatum(count int) []parser.Datum {
rng, _ := randutil.NewPseudoRand()
vals := make([]parser.Datum, count)
for i := range vals {
dd := &parser.DDecimal{}
decimal.SetFromFloat(&dd.Dec, rng.Float64())
vals[i] = dd
}
return vals
}
示例4: Eval
//.........这里部分代码省略.........
return DFloat(1), nil
}
return DFloat(0), nil
case DInt:
return DFloat(v), nil
case DFloat:
return d, nil
case *DDecimal:
f, err := decimal.Float64FromDec(&v.Dec)
if err != nil {
return nil, errFloatOutOfRange
}
return DFloat(f), nil
case DString:
f, err := strconv.ParseFloat(string(v), 64)
if err != nil {
return nil, err
}
return DFloat(f), nil
}
case *DecimalType:
dd := &DDecimal{}
switch v := d.(type) {
case DBool:
if v {
dd.SetUnscaled(1)
}
return dd, nil
case DInt:
dd.SetUnscaled(int64(v))
return dd, nil
case DFloat:
decimal.SetFromFloat(&dd.Dec, float64(v))
return dd, nil
case *DDecimal:
return d, nil
case DString:
if _, ok := dd.SetString(string(v)); !ok {
return nil, fmt.Errorf("could not parse string %q as decimal", v)
}
return dd, nil
}
case *StringType:
var s DString
switch t := d.(type) {
case DBool, DInt, DFloat, *DDecimal, dNull:
s = DString(d.String())
case DString:
s = t
case DBytes:
if !utf8.ValidString(string(t)) {
return nil, fmt.Errorf("invalid utf8: %q", string(t))
}
s = DString(t)
}
if c, ok := expr.Type.(*StringType); ok {
// If the CHAR type specifies a limit we truncate to that limit:
// 'hello'::CHAR(2) -> 'he'
if c.N > 0 && c.N < len(s) {
s = s[:c.N]
}
}
return s, nil
示例5: TestNumericConstantAvailableTypes
func TestNumericConstantAvailableTypes(t *testing.T) {
wantInt := numValAvailIntFloatDec
wantFloatButCanBeInt := numValAvailFloatIntDec
wantFloat := numValAvailFloatDec
testCases := []struct {
str string
avail []Datum
}{
{"1", wantInt},
{"0", wantInt},
{"-1", wantInt},
{"9223372036854775807", wantInt},
{"1.0", wantFloatButCanBeInt},
{"-1234.0000", wantFloatButCanBeInt},
{"1e10", wantFloatButCanBeInt},
{"1E10", wantFloatButCanBeInt},
{"1.1", wantFloat},
{"1e-10", wantFloat},
{"1E-10", wantFloat},
{"-1231.131", wantFloat},
{"876543234567898765436787654321", wantFloat},
}
for i, test := range testCases {
tok := token.INT
if strings.ContainsAny(test.str, ".eE") {
tok = token.FLOAT
}
val := constant.MakeFromLiteral(test.str, tok, 0)
if val.Kind() == constant.Unknown {
t.Fatalf("%d: could not parse value string %q", i, test.str)
}
// Check available types.
c := &NumVal{Value: val}
avail := c.AvailableTypes()
if !reflect.DeepEqual(avail, test.avail) {
t.Errorf("%d: expected the available type set %v for %v, found %v",
i, test.avail, c.Value.ExactString(), avail)
}
// Make sure it can be resolved as each of those types.
for _, availType := range avail {
if res, err := c.ResolveAsType(availType); err != nil {
t.Errorf("%d: expected resolving %v as available type %s would succeed, found %v",
i, c.Value.ExactString(), availType.Type(), err)
} else {
resErr := func(parsed, resolved interface{}) {
t.Errorf("%d: expected resolving %v as available type %s would produce a Datum with the value %v, found %v",
i, c, availType.Type(), parsed, resolved)
}
switch typ := res.(type) {
case *DInt:
var i int64
var err error
if tok == token.INT {
if i, err = strconv.ParseInt(test.str, 10, 64); err != nil {
t.Fatal(err)
}
} else {
var f float64
if f, err = strconv.ParseFloat(test.str, 64); err != nil {
t.Fatal(err)
}
i = int64(f)
}
if resI := int64(*typ); i != resI {
resErr(i, resI)
}
case *DFloat:
f, err := strconv.ParseFloat(test.str, 64)
if err != nil {
t.Fatal(err)
}
if resF := float64(*typ); f != resF {
resErr(f, resF)
}
case *DDecimal:
d := new(inf.Dec)
if !strings.ContainsAny(test.str, "eE") {
if _, ok := d.SetString(test.str); !ok {
t.Fatal(fmt.Sprintf("could not set %q on decimal", test.str))
}
} else {
f, err := strconv.ParseFloat(test.str, 64)
if err != nil {
t.Fatal(err)
}
decimal.SetFromFloat(d, f)
}
if resD := &typ.Dec; d.Cmp(resD) != 0 {
resErr(d, resD)
}
}
}
}
}
}