-
Notifications
You must be signed in to change notification settings - Fork 25
/
ex_integer.go
77 lines (64 loc) · 1.42 KB
/
ex_integer.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
package expreduce
import "fmt"
import "math/big"
import "hash/fnv"
// Integer numbers represented by big.Int
type Integer struct {
Val *big.Int
}
func (f *Integer) Eval(es *EvalState) Ex {
return f
}
func (f *Integer) StringForm(form string) string {
return fmt.Sprintf("%d", f.Val)
}
func (this *Integer) String() string {
return this.StringForm("InputForm")
}
func (this *Integer) IsEqual(other Ex, cl *CASLogger) string {
otherConv, ok := other.(*Integer)
if !ok {
otherFlt, ok := other.(*Flt)
if ok {
thisAsFlt := big.NewFloat(0)
thisAsFlt.SetInt(this.Val)
if thisAsFlt.Cmp(otherFlt.Val) == 0 {
return "EQUAL_TRUE"
}
}
return "EQUAL_UNK"
}
if this.Val.Cmp(otherConv.Val) != 0 {
return "EQUAL_FALSE"
}
return "EQUAL_TRUE"
}
func (this *Integer) DeepCopy() Ex {
tmp := big.NewInt(0)
tmp.Set(this.Val)
return &Integer{tmp}
}
func (this *Integer) NeedsEval() bool {
return false
}
func NewInt(i int64) *Integer {
return &Integer{big.NewInt(i)}
}
func (this *Integer) Hash() uint64 {
h := fnv.New64a()
h.Write([]byte{242, 99, 84, 113, 102, 46, 118, 94})
bytes, _ := this.Val.MarshalText()
h.Write(bytes)
return h.Sum64()
}
func (this *Integer) AsBigFloat() *big.Float {
newfloat := big.NewFloat(0)
newfloat.SetInt(this.Val)
return newfloat
}
func (this *Integer) AddI(i *Integer) {
this.Val.Add(this.Val, i.Val)
}
func (this *Integer) MulI(i *Integer) {
this.Val.Mul(this.Val, i.Val)
}