forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_float.go
More file actions
61 lines (46 loc) · 1.09 KB
/
json_float.go
File metadata and controls
61 lines (46 loc) · 1.09 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
package gomavlib
// IMPORT REQUIRED PACKAGES.
import (
"encoding/json"
"math"
"strconv"
)
// DEFINE PUBLIC STRUCTURES.
// DEFINE PRIVATE STRUCTURES.
type JsonFloat32 struct {
F float32
}
type JsonFloat64 struct {
F float64
}
// DEFINE PUBLIC GLOBALS.
// DEFINE PRIVATE GLOBALS.
// DEFINE PUBLIC STATIC FUNCTIONS.
// DEFINE PRIVATE RECEIVER FUNCTIONS.
func (f JsonFloat32) String() string {
return strconv.FormatFloat(float64(f.F), 'f', 5, 32)
}
func (f JsonFloat32) MarshalJSON() ([]byte, error) {
if math.IsNaN(float64(f.F)) {
return json.Marshal("nan")
} else if math.IsInf(float64(f.F), 1) {
return json.Marshal("+inf")
} else if math.IsInf(float64(f.F), -1) {
return json.Marshal("-inf")
}
return json.Marshal(f.F)
}
func (f JsonFloat64) String() string {
return strconv.FormatFloat(f.F, 'f', 5, 64)
}
func (f JsonFloat64) MarshalJSON() ([]byte, error) {
if math.IsNaN(f.F) {
return json.Marshal("nan")
} else if math.IsInf(f.F, 1) {
return json.Marshal("+inf")
} else if math.IsInf(f.F, -1) {
return json.Marshal("-inf")
}
return json.Marshal(f.F)
}
// ALL DONE.