github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/float.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package types 23 24 import ( 25 "context" 26 "strconv" 27 28 "github.com/dolthub/dolt/go/store/hash" 29 ) 30 31 // Float is a Noms Value wrapper around the primitive float64 type. 32 type Float float64 33 34 // Value interface 35 func (v Float) Value(ctx context.Context) (Value, error) { 36 return v, nil 37 } 38 39 func (v Float) Equals(other Value) bool { 40 return v == other 41 } 42 43 func (v Float) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { 44 if v2, ok := other.(Float); ok { 45 return v < v2, nil 46 } 47 return FloatKind < other.Kind(), nil 48 } 49 50 func (v Float) Hash(nbf *NomsBinFormat) (hash.Hash, error) { 51 return getHash(v, nbf) 52 } 53 54 func (v Float) isPrimitive() bool { 55 return true 56 } 57 58 func (v Float) WalkValues(ctx context.Context, cb ValueCallback) error { 59 return nil 60 } 61 62 func (v Float) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error { 63 return nil 64 } 65 66 func (v Float) typeOf() (*Type, error) { 67 return PrimitiveTypeMap[FloatKind], nil 68 } 69 70 func (v Float) Kind() NomsKind { 71 return FloatKind 72 } 73 74 func (v Float) valueReadWriter() ValueReadWriter { 75 return nil 76 } 77 78 func (v Float) writeTo(w nomsWriter, nbf *NomsBinFormat) error { 79 err := FloatKind.writeTo(w, nbf) 80 81 if err != nil { 82 return err 83 } 84 85 w.writeFloat(v, nbf) 86 return nil 87 } 88 89 func (v Float) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) { 90 return Float(b.ReadFloat(nbf)), nil 91 } 92 93 func (v Float) skip(nbf *NomsBinFormat, b *binaryNomsReader) { 94 b.skipFloat(nbf) 95 } 96 97 func (v Float) HumanReadableString() string { 98 return strconv.FormatFloat(float64(v), 'g', -1, 64) 99 }