-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathratio.go
More file actions
138 lines (119 loc) · 3.45 KB
/
ratio.go
File metadata and controls
138 lines (119 loc) · 3.45 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
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
// Copyright (c) 2022, Peter Ohler, All rights reserved.
package slip
import (
"math/big"
"strconv"
)
// RatioSymbol is the symbol with a value of "ratio".
const RatioSymbol = Symbol("ratio")
// Ratio is a numerator and denominator pair.
type Ratio big.Rat
// NewRatio creates a new Ratio.
func NewRatio(num, denom int64) *Ratio {
if denom == 0 {
ArithmeticPanic(NewScope(), 0, Symbol("/"), List{Fixnum(num), Fixnum(denom)}, "division by zero")
}
return (*Ratio)(big.NewRat(num, denom))
}
// NewBigRatio creates a new Ratio.
func NewBigRatio(num, denom *big.Int) *Ratio {
if denom.Sign() == 0 {
ArithmeticPanic(NewScope(), 0, Symbol("/"), List{(*Bignum)(num), (*Bignum)(denom)}, "division by zero")
}
var rat big.Rat
return (*Ratio)(rat.SetFrac(num, denom))
}
// String representation of the Object.
func (obj *Ratio) String() string {
return string(obj.Readably(nil, &printer))
}
// Append a buffer with a representation of the Object.
func (obj *Ratio) Append(b []byte) []byte {
return obj.Readably(b, &printer)
}
// Readably appends the object to a byte slice. If p.Readbly is true the
// objects is appended in a readable format otherwise a simple append which
// may or may not be readable.
func (obj *Ratio) Readably(b []byte, p *Printer) []byte {
if (*big.Rat)(obj).IsInt() {
return (*Bignum)((*big.Rat)(obj).Num()).Readably(b, p)
}
if p.Radix {
switch p.Base {
case 2:
b = append(b, "#b"...)
case 8:
b = append(b, "#o"...)
case 16:
b = append(b, "#x"...)
default:
b = append(b, '#')
b = strconv.AppendInt(b, int64(p.Base), 10)
b = append(b, 'r')
}
}
b = (*big.Rat)(obj).Num().Append(b, int(p.Base))
b = append(b, '/')
b = (*big.Rat)(obj).Denom().Append(b, int(p.Base))
return b
}
// Simplify the Object into an int64.
func (obj *Ratio) Simplify() any {
if f, exact := (*big.Rat)(obj).Float64(); exact {
return f
}
return string(printer.Append([]byte{}, obj, 0))
}
// Equal returns true if this Object and the other are equal in value.
func (obj *Ratio) Equal(other Object) (eq bool) {
switch to := other.(type) {
case Fixnum:
rat := (*big.Rat)(obj)
eq = rat.IsInt() && rat.Num().IsInt64() && rat.Num().Int64() == int64(to)
case SingleFloat:
f, exact := (*big.Rat)(obj).Float64()
eq = exact && f == float64(to)
case DoubleFloat:
f, exact := (*big.Rat)(obj).Float64()
eq = exact && f == float64(to)
case *LongFloat:
f, exact := (*big.Rat)(obj).Float64()
f2, accuracy := (*big.Float)(to).Float64()
eq = exact && accuracy == big.Exact && f == f2
case *Ratio:
eq = (*big.Rat)(obj).Cmp((*big.Rat)(to)) == 0
case *Bignum:
rat := (*big.Rat)(obj)
eq = rat.IsInt() && (*big.Int)(to).Cmp(rat.Num()) == 0
}
return
}
// Hierarchy returns the class hierarchy as symbols for the instance.
func (obj *Ratio) Hierarchy() []Symbol {
return []Symbol{RatioSymbol, RationalSymbol, RealSymbol, NumberSymbol, TrueSymbol}
}
// RationalType returns 'ratio.
func (obj *Ratio) RationalType() Symbol {
return RatioSymbol
}
// RealType returns 'ratio.
func (obj *Ratio) RealType() Symbol {
return RatioSymbol
}
// NumberType returns 'ratio.
func (obj *Ratio) NumberType() Symbol {
return RatioSymbol
}
// Eval returns self.
func (obj *Ratio) Eval(s *Scope, depth int) Object {
return obj
}
// RealValue of the number as a float64.
func (obj *Ratio) RealValue() float64 {
f, _ := (*big.Rat)(obj).Float64()
return f
}
// LoadForm returns a form that can be evaluated to create the object.
func (obj *Ratio) LoadForm() Object {
return obj
}