-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfrac.go
161 lines (141 loc) · 3.45 KB
/
frac.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
//Copyright 2010 Anschel Schaffer-Cohen
//Under go's BSD-style license
//at $GOROOT/LICENSE
//The frac package implements fractions.
package frac
import (
"fmt"
"errors"
)
//A Frac is a fraction.
type Frac struct {
num, den uint64
positive bool
}
var DivByZero = errors.New("Attempt to divide by zero.")
func abs(i int64) uint64 {
if i >= 0 {
return uint64(i)
}
return uint64(-i)
}
func gcd(x, y uint64) uint64 {
for y != 0 {
x, y = y, x%y
}
return x
}
//New() returns a *Frac unless den==0
func New(num, den int64) (*Frac, error) {
if den == 0 {
return nil, DivByZero
}
f := &Frac{
num: abs(num),
den: abs(den),
positive: num == 0 || ((num > 0) == (den > 0)),
}
f.simplify()
return f, nil
}
//f.String() satisfies fmt.Stringer
func (f *Frac) String() string {
return fmt.Sprintf("%d/%d", f.Numerator(), f.Denominator())
}
func (f *Frac) simplify() {
if f.num == 0 { //This is not strictly necessary, but it prevents
f.den = 1 //f.String() from outputing "-0"
return
}
common := gcd(f.num, f.den)
f.num /= common
f.den /= common
}
//f.Positive() returns true if f>0 and false if f<0. Behavior at f==0 is undefined.
func (f *Frac) Positive() bool {
return f.positive
}
//f.Plus(other) returns f + other, leaving both unchanged.
func (f *Frac) Plus(other *Frac) *Frac {
if f.den == other.den {
ret := new(Frac)
ret.den = f.den
switch {
case f.positive && other.positive:
ret.positive = true
ret.num = f.num + other.num
case !f.positive && !other.positive:
ret.positive = false
ret.num = f.num + other.num
case f.num == other.num:
ret.num = 0
case f.num > other.num:
ret.positive = f.positive
ret.num = f.num - other.num
case f.num < other.num:
ret.positive = other.positive
ret.num = other.num - f.num
}
ret.simplify()
return ret
}
return (&Frac{f.num * other.den, f.den * other.den, f.positive}).Plus(&Frac{other.num * f.den, other.den * f.den, other.positive})
}
//f.Negative() returns -f.
func (f *Frac) Negative() *Frac {
return &Frac{f.num, f.den, !f.positive}
}
//f.Minus(other) returns f - other, leaving both unchanged.
func (f *Frac) Minus(other *Frac) *Frac {
return f.Plus(other.Negative())
}
//f.Inverse() returns f^-1.
func (f *Frac) Inverse() *Frac {
return &Frac{f.den, f.num, f.positive}
}
//f.Times(other) returns f * other, leaving both unchanged.
func (f *Frac) Times(other *Frac) *Frac {
ret := &Frac{f.num * other.num, f.den * other.den, f.positive == other.positive}
ret.simplify()
return ret
}
//f.Divided(other) returns f ÷ other, leaving both unchanged.
func (f *Frac) Divided(other *Frac) (*Frac, error) {
if other.num == 0 {
return nil, DivByZero
}
return f.Times(other.Inverse()), nil
}
//f.Numerator() returns the (signed) numerator of f.
func (f *Frac) Numerator() int64 {
ret := int64(f.num)
if !f.positive {
ret *= -1
}
return ret
}
//f.Denominator() returns the (always positive) denominator of f.
func (f *Frac) Denominator() int64 {
return int64(f.den)
}
//f.Float64() returns a Float64 approximation of f.
func (f *Frac) Float64() float64 {
ret := float64(f.num) / float64(f.den)
if !f.positive {
ret *= -1
}
return ret
}
//f.Mixed() returns a string like "2 1/3".
func (f *Frac) Mixed() string {
if f.num < f.den {
return f.String()
}
if f.den == 1 {
return fmt.Sprint(f.Numerator())
}
if f.positive {
return fmt.Sprintf("%d %d/%d", f.num/f.den, f.num%f.den, f.den)
}
return fmt.Sprintf("-%d %d/%d", f.num/f.den, f.num%f.den, f.den)
}