-
Notifications
You must be signed in to change notification settings - Fork 25
/
ex_real.go
99 lines (82 loc) · 1.9 KB
/
ex_real.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
package expreduce
import "fmt"
import "math/big"
import "bytes"
import "hash"
// Floating point numbers represented by big.Float
type Flt struct {
Val *big.Float
}
func (f *Flt) Eval(es *EvalState) Ex {
return f
}
func (f *Flt) StringForm(form string) string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%.6g", f.Val))
if bytes.IndexRune(buffer.Bytes(), '.') == -1 {
buffer.WriteString(".")
}
return buffer.String()
}
func (this *Flt) String() string {
return this.StringForm("InputForm")
}
func (this *Flt) IsEqual(other Ex, cl *CASLogger) string {
otherConv, ok := other.(*Flt)
if !ok {
otherInteger, ok := other.(*Integer)
if ok {
otherAsFlt := big.NewFloat(0)
otherAsFlt.SetInt(otherInteger.Val)
if otherAsFlt.Cmp(this.Val) == 0 {
return "EQUAL_TRUE"
}
}
return "EQUAL_UNK"
}
if this.Val.Cmp(otherConv.Val) != 0 {
return "EQUAL_FALSE"
}
return "EQUAL_TRUE"
}
func (this *Flt) DeepCopy() Ex {
tmp := big.NewFloat(0)
tmp.Copy(this.Val)
return &Flt{tmp}
}
func IntegerToFlt(i *Integer) (*Flt, bool) {
newfloat := big.NewFloat(0)
newfloat.SetInt(i.Val)
return &Flt{newfloat}, true
}
func RationalToFlt(r *Rational) (*Flt, bool) {
newfloat := big.NewFloat(0)
newfloat.SetRat(r.AsBigRat())
return &Flt{newfloat}, true
}
func (this *Flt) NeedsEval() bool {
return false
}
func (this *Flt) Hash(h *hash.Hash64) {
(*h).Write([]byte{195, 244, 76, 249, 227, 115, 88, 251})
bytes, _ := this.Val.MarshalText()
(*h).Write(bytes)
}
func (this *Flt) AddI(i *Integer) {
this.Val.Add(this.Val, i.AsBigFloat())
}
func (this *Flt) AddR(r *Rational) {
this.Val.Add(this.Val, r.AsBigFloat())
}
func (this *Flt) AddF(f *Flt) {
this.Val.Add(this.Val, f.Val)
}
func (this *Flt) MulI(i *Integer) {
this.Val.Mul(this.Val, i.AsBigFloat())
}
func (this *Flt) MulR(r *Rational) {
this.Val.Mul(this.Val, r.AsBigFloat())
}
func (this *Flt) MulF(f *Flt) {
this.Val.Mul(this.Val, f.Val)
}