/
ex_integer.go
102 lines (86 loc) · 2.06 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
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
package expreduce
import "fmt"
import "math/big"
import "hash/fnv"
// Integer numbers represented by big.Int
type Integer struct {
Val *big.Int
cachedHash uint64
}
func (f *Integer) Eval(es *EvalState) Ex {
return f
}
/*func (f *Integer) StringForm(params ToStringParams) string {
return fmt.Sprintf("%d", f.Val)
}*/
func (i *Integer) StringForm(params ToStringParams) string {
if i.Val.Cmp(big.NewInt(0)) < 0 {
if needsParens("System`Times", params.previousHead) {
return fmt.Sprintf("(%d)", i.Val)
}
}
return fmt.Sprintf("%d", i.Val)
}
func (this *Integer) String() string {
context, contextPath := DefaultStringFormArgs()
return this.StringForm(ToStringParams{form: "InputForm", context: context, contextPath: contextPath})
}
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{Val: tmp, cachedHash: this.cachedHash}
}
func (this *Integer) Copy() Ex {
return this
}
func (this *Integer) NeedsEval() bool {
return false
}
func NewInteger(i *big.Int) *Integer {
return &Integer{Val: i}
}
func NewInt(i int64) *Integer {
return NewInteger(big.NewInt(i))
}
func (this *Integer) Hash() uint64 {
if this.cachedHash > 0 {
return this.cachedHash
}
h := fnv.New64a()
h.Write([]byte{242, 99, 84, 113, 102, 46, 118, 94})
bytes, _ := this.Val.MarshalText()
h.Write(bytes)
this.cachedHash = h.Sum64()
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)
this.cachedHash = 0
}
func (this *Integer) MulI(i *Integer) {
this.Val.Mul(this.Val, i.Val)
this.cachedHash = 0
}