File: float.go

package info (click to toggle)
mtail 3.2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,384 kB
  • sloc: yacc: 647; makefile: 226; sh: 78; lisp: 77; awk: 17
file content (43 lines) | stat: -rw-r--r-- 957 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2017 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package datum

import (
	"encoding/json"
	"fmt"
	"math"
	"sync/atomic"
	"time"
)

// Float describes a floating point value at a given timestamp.
type Float struct {
	BaseDatum
	Valuebits uint64
}

// ValueString returns the value of the Float as a string.
func (d *Float) ValueString() string {
	return fmt.Sprintf("%g", d.Get())
}

// Set sets value of the Float at the timestamp ts.
func (d *Float) Set(v float64, ts time.Time) {
	atomic.StoreUint64(&d.Valuebits, math.Float64bits(v))
	d.stamp(ts)
}

// Get returns the floating-point value.
func (d *Float) Get() float64 {
	return math.Float64frombits(atomic.LoadUint64(&d.Valuebits))
}

// MarshalJSON returns a JSON encoding of the Float.
func (d *Float) MarshalJSON() ([]byte, error) {
	j := struct {
		Value float64
		Time  int64
	}{d.Get(), atomic.LoadInt64(&d.Time)}
	return json.Marshal(j)
}