github.com/gogf/gf/v2@v2.7.4/container/gtype/gtype_float64.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gtype
     8  
     9  import (
    10  	"math"
    11  	"strconv"
    12  	"sync/atomic"
    13  
    14  	"github.com/gogf/gf/v2/util/gconv"
    15  )
    16  
    17  // Float64 is a struct for concurrent-safe operation for type float64.
    18  type Float64 struct {
    19  	value uint64
    20  }
    21  
    22  // NewFloat64 creates and returns a concurrent-safe object for float64 type,
    23  // with given initial value `value`.
    24  func NewFloat64(value ...float64) *Float64 {
    25  	if len(value) > 0 {
    26  		return &Float64{
    27  			value: math.Float64bits(value[0]),
    28  		}
    29  	}
    30  	return &Float64{}
    31  }
    32  
    33  // Clone clones and returns a new concurrent-safe object for float64 type.
    34  func (v *Float64) Clone() *Float64 {
    35  	return NewFloat64(v.Val())
    36  }
    37  
    38  // Set atomically stores `value` into t.value and returns the previous value of t.value.
    39  func (v *Float64) Set(value float64) (old float64) {
    40  	return math.Float64frombits(atomic.SwapUint64(&v.value, math.Float64bits(value)))
    41  }
    42  
    43  // Val atomically loads and returns t.value.
    44  func (v *Float64) Val() float64 {
    45  	return math.Float64frombits(atomic.LoadUint64(&v.value))
    46  }
    47  
    48  // Add atomically adds `delta` to t.value and returns the new value.
    49  func (v *Float64) Add(delta float64) (new float64) {
    50  	for {
    51  		old := math.Float64frombits(v.value)
    52  		new = old + delta
    53  		if atomic.CompareAndSwapUint64(
    54  			&v.value,
    55  			math.Float64bits(old),
    56  			math.Float64bits(new),
    57  		) {
    58  			break
    59  		}
    60  	}
    61  	return
    62  }
    63  
    64  // Cas executes the compare-and-swap operation for value.
    65  func (v *Float64) Cas(old, new float64) (swapped bool) {
    66  	return atomic.CompareAndSwapUint64(&v.value, math.Float64bits(old), math.Float64bits(new))
    67  }
    68  
    69  // String implements String interface for string printing.
    70  func (v *Float64) String() string {
    71  	return strconv.FormatFloat(v.Val(), 'g', -1, 64)
    72  }
    73  
    74  // MarshalJSON implements the interface MarshalJSON for json.Marshal.
    75  func (v Float64) MarshalJSON() ([]byte, error) {
    76  	return []byte(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil
    77  }
    78  
    79  // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
    80  func (v *Float64) UnmarshalJSON(b []byte) error {
    81  	v.Set(gconv.Float64(string(b)))
    82  	return nil
    83  }
    84  
    85  // UnmarshalValue is an interface implement which sets any type of value for `v`.
    86  func (v *Float64) UnmarshalValue(value interface{}) error {
    87  	v.Set(gconv.Float64(value))
    88  	return nil
    89  }
    90  
    91  // DeepCopy implements interface for deep copy of current type.
    92  func (v *Float64) DeepCopy() interface{} {
    93  	if v == nil {
    94  		return nil
    95  	}
    96  	return NewFloat64(v.Val())
    97  }