-
Notifications
You must be signed in to change notification settings - Fork 24
/
reading.go
49 lines (40 loc) · 983 Bytes
/
reading.go
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
44
45
46
47
48
49
package runtime
import (
"math"
)
var _ Reading = (*reading)(nil)
// Reading is the interface that reading implements which wraps a float64 value which can be valid or invalid.
type Reading interface {
Value() float64
Valid() bool
}
type reading struct {
value float64
}
// NewReading creates a reading with the given float64 value.
func NewReading(value float64) Reading {
return &reading{
value: value,
}
}
// NewBoolReading creates a reading with the given bool value.
func NewBoolReading(value bool) Reading {
if value {
return NewReading(1)
}
return NewReading(0)
}
// InvalidReading creates a reading with 'value' set to math.NaN(), invalid value.
func InvalidReading() Reading {
return &reading{
value: math.NaN(),
}
}
// Value returns the value of the reading.
func (r *reading) Value() float64 {
return r.value
}
// Valid checks whether the value of the reading is valid or invalid.
func (r *reading) Valid() bool {
return !math.IsNaN(r.value)
}