fractify 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/fractify/fraction.rb +95 -58
- data/lib/fractify/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 815c6148fb1a168ecc92fc30a767b334d22b69bd01ec45d710528404a03962de
|
4
|
+
data.tar.gz: cf9d530bb686c30cfae00bd44ee24f22faff2cf800f11da8f67a08d0a030a140
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f986edd971a9e04fdb4f66ec950748789372f7931117d75193512e62665b9bc18de3cc459591e1ff3e3f4df2b66e016743686b46deea21c54be8611f708ad1f1
|
7
|
+
data.tar.gz: b46f33911ed19b88cb92f8bca462a5f53aea4758710ef324075dab77d47b887db33c56a2c85b8fa122a9e3c1cdd3fe9cd06e309279f9f165f42798f59ede6ac3
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -32,7 +32,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
32
|
|
33
33
|
## Contributing
|
34
34
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/Verseth/fractify.
|
36
36
|
|
37
37
|
## License
|
38
38
|
|
data/lib/fractify/fraction.rb
CHANGED
@@ -7,15 +7,15 @@ module Fractify
|
|
7
7
|
class Fraction
|
8
8
|
attr_accessor :whole_part, :numerator, :denominator, :negative
|
9
9
|
|
10
|
-
def initialize(whole_part: 0, numerator: 0, denominator: 1, string: '',
|
10
|
+
def initialize(whole_part: 0, numerator: 0, denominator: 1, string: '', numeric: false)
|
11
11
|
unless string.strip.empty?
|
12
12
|
to_zero!
|
13
13
|
parse_fraction_string!(string)
|
14
14
|
return
|
15
15
|
end
|
16
16
|
|
17
|
-
if
|
18
|
-
convert_number!(
|
17
|
+
if numeric.is_a? Numeric
|
18
|
+
convert_number!(numeric)
|
19
19
|
return
|
20
20
|
end
|
21
21
|
raise DividingByZeroError if denominator.zero? && numerator != 0
|
@@ -53,9 +53,11 @@ module Fractify
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def simplify!
|
56
|
-
return if integer?
|
56
|
+
return true if integer?
|
57
57
|
|
58
58
|
if zero?
|
59
|
+
@denominator = 1
|
60
|
+
@whole_part = 0
|
59
61
|
@negative = false
|
60
62
|
else
|
61
63
|
greatest_common_divisor = self.class.greatest_common_divisor(numerator, denominator)
|
@@ -63,10 +65,12 @@ module Fractify
|
|
63
65
|
@denominator /= greatest_common_divisor
|
64
66
|
@numerator /= greatest_common_divisor
|
65
67
|
end
|
68
|
+
|
69
|
+
true
|
66
70
|
end
|
67
71
|
|
68
72
|
def to_proper_fraction!
|
69
|
-
return if zero?
|
73
|
+
return true if zero?
|
70
74
|
|
71
75
|
if denominator == 1 && numerator != 1
|
72
76
|
@whole_part += numerator
|
@@ -76,6 +80,18 @@ module Fractify
|
|
76
80
|
@whole_part += new_whole_part
|
77
81
|
@numerator %= denominator
|
78
82
|
end
|
83
|
+
|
84
|
+
true
|
85
|
+
end
|
86
|
+
|
87
|
+
def to_improper!
|
88
|
+
return true if whole_part.zero?
|
89
|
+
|
90
|
+
@numerator -= 1 if numerator == 1 && denominator == 1
|
91
|
+
@numerator += whole_part * denominator
|
92
|
+
@whole_part = 0
|
93
|
+
|
94
|
+
true
|
79
95
|
end
|
80
96
|
|
81
97
|
def puts
|
@@ -87,6 +103,8 @@ module Fractify
|
|
87
103
|
@numerator = 0
|
88
104
|
@denominator = 1
|
89
105
|
@negative = false
|
106
|
+
|
107
|
+
true
|
90
108
|
end
|
91
109
|
|
92
110
|
def to_f
|
@@ -111,73 +129,77 @@ module Fractify
|
|
111
129
|
end
|
112
130
|
|
113
131
|
def to_s(improper = false)
|
114
|
-
|
132
|
+
fraction = dup
|
133
|
+
fraction.to_proper_fraction! unless improper
|
115
134
|
fraction_string = '('
|
116
135
|
fraction_string += '-' if negative?
|
117
136
|
|
118
|
-
if zero?
|
137
|
+
if fraction.zero?
|
119
138
|
fraction_string += '0'
|
120
|
-
elsif one?
|
139
|
+
elsif fraction.one?
|
121
140
|
fraction_string += '1'
|
122
141
|
else
|
123
|
-
fraction_string += whole_part.to_s if whole_part != 0
|
124
|
-
if numerator != 1 || denominator != 1
|
125
|
-
fraction_string += ' ' if whole_part != 0
|
126
|
-
fraction_string += numerator.to_s + '/' + denominator.to_s
|
142
|
+
fraction_string += fraction.whole_part.to_s if fraction.whole_part != 0
|
143
|
+
if fraction.numerator != 1 || fraction.denominator != 1
|
144
|
+
fraction_string += ' ' if fraction.whole_part != 0
|
145
|
+
fraction_string += fraction.numerator.to_s + '/' + fraction.denominator.to_s
|
127
146
|
end
|
128
147
|
end
|
129
148
|
|
130
|
-
to_improper!
|
131
149
|
fraction_string + ')'
|
132
150
|
end
|
133
151
|
|
134
152
|
def -(other)
|
153
|
+
check_argument(other)
|
154
|
+
|
135
155
|
fraction = dup
|
136
|
-
object = other.
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
else
|
143
|
-
fraction.to_common_denominator!(object) unless object.zero?
|
156
|
+
object = if other.is_a? Numeric
|
157
|
+
Fractify::Fraction.new(numeric: other)
|
158
|
+
else
|
159
|
+
other.dup
|
160
|
+
end
|
161
|
+
fraction.to_common_denominator!(object) unless object.zero?
|
144
162
|
|
145
|
-
|
146
|
-
|
163
|
+
fraction.minus_to_numerator!
|
164
|
+
object.minus_to_numerator!
|
147
165
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
end
|
166
|
+
fraction.numerator -= object.numerator
|
167
|
+
fraction.minus_to_negative_field!
|
168
|
+
fraction.simplify!
|
152
169
|
|
153
170
|
fraction
|
154
171
|
end
|
155
172
|
|
156
173
|
def +(other)
|
174
|
+
check_argument(other)
|
175
|
+
|
157
176
|
fraction = dup
|
158
|
-
object = other.
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
else
|
165
|
-
fraction.to_common_denominator!(object) unless object.zero?
|
177
|
+
object = if other.is_a? Numeric
|
178
|
+
Fractify::Fraction.new(numeric: other)
|
179
|
+
else
|
180
|
+
other.dup
|
181
|
+
end
|
182
|
+
fraction.to_common_denominator!(object) unless object.zero?
|
166
183
|
|
167
|
-
|
168
|
-
|
184
|
+
fraction.minus_to_numerator!
|
185
|
+
object.minus_to_numerator!
|
169
186
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
end
|
187
|
+
fraction.numerator += object.numerator
|
188
|
+
fraction.minus_to_negative_field!
|
189
|
+
fraction.simplify!
|
174
190
|
|
175
191
|
fraction
|
176
192
|
end
|
177
193
|
|
178
194
|
def *(other)
|
195
|
+
check_argument(other)
|
196
|
+
|
179
197
|
fraction = dup
|
180
|
-
object = other.
|
198
|
+
object = if other.is_a? Numeric
|
199
|
+
Fractify::Fraction.new(numeric: other)
|
200
|
+
else
|
201
|
+
other.dup
|
202
|
+
end
|
181
203
|
|
182
204
|
fraction.to_improper!
|
183
205
|
object.to_improper!
|
@@ -198,8 +220,15 @@ module Fractify
|
|
198
220
|
end
|
199
221
|
|
200
222
|
def /(other)
|
223
|
+
check_argument(other)
|
224
|
+
raise DividingByZeroError if other.zero?
|
225
|
+
|
201
226
|
fraction = dup
|
202
|
-
object = other.
|
227
|
+
object = if other.is_a? Numeric
|
228
|
+
Fractify::Fraction.new(numeric: other)
|
229
|
+
else
|
230
|
+
other.dup
|
231
|
+
end
|
203
232
|
|
204
233
|
object.to_reciprocal!
|
205
234
|
fraction *= object
|
@@ -208,13 +237,12 @@ module Fractify
|
|
208
237
|
end
|
209
238
|
|
210
239
|
def **(other)
|
240
|
+
check_argument(other)
|
211
241
|
if other.is_a? Numeric
|
212
242
|
power(other)
|
213
|
-
|
243
|
+
else
|
214
244
|
x = other.to_f
|
215
245
|
power(x)
|
216
|
-
else
|
217
|
-
raise IncorrectArgumentsError
|
218
246
|
end
|
219
247
|
end
|
220
248
|
|
@@ -414,6 +442,8 @@ module Fractify
|
|
414
442
|
numerator_multiplication = least_common_multiple / other.denominator
|
415
443
|
other.numerator *= numerator_multiplication
|
416
444
|
other.denominator = least_common_multiple
|
445
|
+
|
446
|
+
true
|
417
447
|
end
|
418
448
|
|
419
449
|
def to_reciprocal!
|
@@ -421,37 +451,41 @@ module Fractify
|
|
421
451
|
aux = denominator
|
422
452
|
@denominator = numerator
|
423
453
|
@numerator = aux
|
454
|
+
|
455
|
+
true
|
424
456
|
end
|
425
457
|
|
426
458
|
protected
|
427
459
|
|
428
|
-
def to_improper!
|
429
|
-
return if whole_part.zero?
|
430
|
-
|
431
|
-
@numerator -= 1 if numerator == 1 && denominator == 1
|
432
|
-
@numerator += whole_part * denominator
|
433
|
-
@whole_part = 0
|
434
|
-
end
|
435
|
-
|
436
460
|
def minus_to_numerator!
|
437
|
-
return unless negative
|
461
|
+
return true unless negative
|
438
462
|
|
439
463
|
@negative = false
|
440
464
|
@numerator = -numerator
|
465
|
+
|
466
|
+
true
|
441
467
|
end
|
442
468
|
|
443
469
|
def minus_to_negative_field!
|
444
|
-
return unless numerator.negative?
|
470
|
+
return true unless numerator.negative?
|
445
471
|
|
446
472
|
@negative = true
|
447
473
|
@numerator = numerator.abs
|
474
|
+
|
475
|
+
true
|
448
476
|
end
|
449
477
|
|
450
478
|
private
|
451
479
|
|
480
|
+
def check_argument(other)
|
481
|
+
return unless !other.is_a?(Numeric) && !other.is_a?(Fractify::Fraction)
|
482
|
+
|
483
|
+
raise IncorrectArgumentsError
|
484
|
+
end
|
485
|
+
|
452
486
|
def power(x)
|
453
487
|
fraction = dup
|
454
|
-
fraction.negative = false if x % 2
|
488
|
+
fraction.negative = false if (x % 2).zero?
|
455
489
|
|
456
490
|
if x.negative?
|
457
491
|
fraction.to_reciprocal!
|
@@ -468,7 +502,7 @@ module Fractify
|
|
468
502
|
float = fraction.to_f.abs
|
469
503
|
float **= x
|
470
504
|
float = -float if fraction.negative?
|
471
|
-
fraction = Fractify::Fraction.new(
|
505
|
+
fraction = Fractify::Fraction.new(numeric: float)
|
472
506
|
end
|
473
507
|
|
474
508
|
fraction
|
@@ -480,6 +514,8 @@ module Fractify
|
|
480
514
|
@whole_part = whole_part.abs
|
481
515
|
@numerator = numerator.abs
|
482
516
|
@denominator = denominator.abs
|
517
|
+
|
518
|
+
true
|
483
519
|
end
|
484
520
|
|
485
521
|
def arguments_make_negative?
|
@@ -682,6 +718,7 @@ module Fractify
|
|
682
718
|
@numerator = 1 if numerator.zero?
|
683
719
|
|
684
720
|
simplify!
|
721
|
+
to_improper!
|
685
722
|
end
|
686
723
|
end
|
687
724
|
end
|
data/lib/fractify/version.rb
CHANGED