-
Notifications
You must be signed in to change notification settings - Fork 0
/
fct-impl.go
162 lines (133 loc) · 3.16 KB
/
fct-impl.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package fct
import (
"fmt"
"log"
"math/big"
"runtime/debug"
"github.com/shopspring/decimal"
)
func (slf *FCT) get(dval FCT) FCT {
if slf != nil {
return *slf
}
return dval
}
func (slf *FCT) float64(dval ...FCT) (float64, error) {
fv, err := getFCT(slf, dval...)
if err != nil {
return 0, err
}
return fv.deci.InexactFloat64(), nil
}
func (slf *FCT) ptrRound(places int, dval ...FCT) (*FCT, error) {
fv, err := getFCT(slf, dval...)
if err != nil {
return nil, err
}
return create(fv.deci.Round(int32(places))), nil
}
func (slf *FCT) ptrFloor(places int, dval ...FCT) (*FCT, error) {
fv, err := getFCT(slf, dval...)
if err != nil {
return nil, err
}
if places < 1 {
return create(fv.deci.Floor()), nil
}
exp := fv.deci.Exponent()
if exp < 0 {
exp *= -1
if exp > int32(places) {
var (
sub = int(exp) - places
div = "1"
thousandDivDecimal = big.NewInt(1)
)
for i := 0; i < sub; i++ {
div = fmt.Sprintf("%v0", div)
v, ok := new(big.Int).SetString(div, 10)
if !ok {
debug.PrintStack()
log.Panicf("error when converting to big.int, value: %v\n", div)
}
thousandDivDecimal = v
}
currentValue := fv.deci.Coefficient()
newValue := new(big.Int).Div(currentValue, thousandDivDecimal)
deci := decimal.NewFromBigInt(newValue, int32(places*-1))
return create(deci), nil
}
}
return slf, nil
}
func (slf *FCT) ptrCeil(places int, dval ...FCT) (*FCT, error) {
fv, err := getFCT(slf, dval...)
if err != nil {
return nil, err
}
if places < 1 {
return create(fv.deci.Ceil()), nil
}
exp := fv.deci.Exponent()
if exp < 0 {
exp *= -1
if exp > int32(places) {
var (
sub = int(exp) - places
div = "1"
thousandDivDecimal = big.NewInt(1)
)
for i := 0; i < sub; i++ {
div = fmt.Sprintf("%v0", div)
v, ok := new(big.Int).SetString(div, 10)
if !ok {
debug.PrintStack()
log.Panicf("error when converting to big.int, value: %v\n", div)
}
thousandDivDecimal = v
}
currentValue := fv.deci.Coefficient()
newValue := new(big.Int).Div(currentValue, thousandDivDecimal)
newValue = new(big.Int).Add(newValue, big.NewInt(1))
deci := decimal.NewFromBigInt(newValue, int32(places*-1))
return create(deci), nil
}
}
return slf, nil
}
func (slf *FCT) ptrTruncate(places int, dval ...FCT) (*FCT, error) {
fv, err := getFCT(slf, dval...)
if err != nil {
return nil, err
}
deci := fv.deci.Truncate(int32(places))
return create(deci), nil
}
func (slf *FCT) ptrPow(val any, dval ...FCT) (*FCT, error) {
v, err := New(val)
if err != nil {
return nil, err
}
fv, err := getFCT(slf, dval...)
if err != nil {
return nil, err
}
deci := fv.deci.Pow(v.deci)
return create(deci), nil
}
func (slf *FCT) toString() (string, error) {
fv, err := getFCT(slf)
if err != nil {
return "", err
}
v1, _ := getString(fv.deci)
return v1, nil
}