@@ -62,7 +62,7 @@ L<double-precision floating-point|https://en.wikipedia.org/wiki/Double-precision
62
62
63
63
A L < Num > literal is written with the exponent separated using letter C < e > . Keep
64
64
in mind that letter C < e > B < is required > even if the exponent is zero, as
65
- otherwise you'll get a L < Rat > or L < MidRat > rational literal instead:
65
+ otherwise you'll get a L < Rat > rational literal instead:
66
66
67
67
= begin code
68
68
say 42e0.^name; # OUTPUT: «Num»
@@ -137,18 +137,19 @@ how-is-it 3+2i; # OUTPUT: «meh»
137
137
138
138
The types that do the L < Rational > role offer high-precision and
139
139
arbitrary-precision decimal numbers. Since the higher the precision the
140
- larger the performance penalty, the L < Rational > types come in three flavours:
141
- L < Rat > , L < MidRat > , and L < FatRat > . The L < Rat > is the most oft-used variant
142
- that degrades into a L < Num > when it can no longer hold all of the requested
143
- precision. The L < FatRat > is the arbitrary-precision variant that keeps growing
144
- to provide all of the requested precision. Lastly, the L < MidRat > type is
145
- somewhat in-between of a L < Rat > and L < FatRat > : offering arbitrary-precision
146
- but degrading like a L < Rat > , when used in lower-precision operations.
140
+ larger the performance penalty, the L < Rational > types come in two flavours:
141
+ L < Rat > and L < FatRat > . The L < Rat > is the most oft-used variant
142
+ that degrades into a L < Num > in most cases, when it can no longer hold all of
143
+ the requested precision. The L < FatRat > is the arbitrary-precision variant that
144
+ keeps growing to provide all of the requested precision.
147
145
148
146
= head2 C < Rat >
149
147
150
148
The most common of L < Rational > types. It supports rationals with denominators
151
149
as large as 64 bits (after reduction of the fraction to the lowest denominator).
150
+ C < Rat > objects with larger denominators can be created directly, however, when
151
+ C < Rat > s with such denominators are the result of mathematical operations, they
152
+ degrade to a L < Num > object.
152
153
153
154
The L < Rat > literals use syntax similar to L < Num > literals in many other
154
155
languages, using the dot to indicate the number is a decimal:
@@ -209,29 +210,8 @@ say ½ + ⅓ + ⅝ + ⅙; # OUTPUT: «1.625»
209
210
If a I < mathematical operation > that produces a L < Rat > answer would produce
210
211
a L < Rat > with denominator larger than 64 bits, that operation would instead
211
212
return a L < Num > object. When I < constructing > a L < Rat > (i.e. when it is not
212
- a result of some mathematical expression), however, in similar
213
- circumstances a L < MidRat > type is produced instead.
214
-
215
- = head2 C < MidRat >
216
-
217
- The L < MidRat > type is a subclass of a L < Rat > : it's used when construction of a
218
- L < Rat > object is attempted, but too much precision is requested. The L < MidRat >
219
- can hold all of the given precision, just like a L < FatRat > would, but it doesn't
220
- have the same "infectiousness" so if it's not used in high-precision operations
221
- with other L < FatRat > objects, it'll degrade to a L < Num > , just like a L < Rat >
222
- object would.
223
-
224
- Attempts to I < construct > a L < Rat > with denominator larger (after reduction)
225
- than 64 bits in size produce a L < MidRat >
226
- object (L < MidRatStr > allomorph for L < RatStr > allomorph constructon). Such
227
- construction occurs when a L < Rational > literal is written in the code, the
228
- L « C < Str.Rat > |/type/Str#method_Rat» or L « C < Str.Numeric > |/type/Str#method_Numeric»
229
- methods are used, or a L < RatStr > allomorph is created using L < val > ,
230
- L < angle brackets|/language/quoting#index-entry-<_>_word_quote > , or other
231
- allomorph-producing means, such as arguments to
232
- L « C < MAIN > routine|/language/functions#sub_MAIN» or calling
233
- L « C < RatStr.new > |/type/RatStr#method_new» . Mathematical operations are B < NOT >
234
- subject to this rule and instead degrade their result to a L < Num > instead.
213
+ a result of some mathematical expression), however, a "fattish" L < Rat > with
214
+ larger denominator will be created.
235
215
236
216
= head2 C < FatRat >
237
217
@@ -307,19 +287,9 @@ which can be explosive.
307
287
= head2 Zero-Denominator Rationals
308
288
309
289
A Zero-Denominator Rational is a numeric that does role L < Rational > , which
310
- among core numerics would be L < Rat > , L < MidRat > , and L < FatRat > objects, which
290
+ among core numerics would be L < Rat > and L < FatRat > objects, which
311
291
has denominator of zero.
312
292
313
- On creation, the numerator of such numerics is always normalized to C < -1 > ,
314
- C < 1 > , C < 0 > , depending on whether the original numerator is negative,
315
- positive, or zero respectively:
316
-
317
- = begin code
318
- say <1000/0>.nude; # OUTPUT: «(1 0)»
319
- say <-1000/0>.nude; # OUTPUT: «(-1 0)»
320
- say <0/0>.nude; # OUTPUT: «(0 0)»
321
- = end code
322
-
323
293
Operations that can be performed without requiring actual division to occur
324
294
are non-explosive. For example, you can separately examine
325
295
L < numerator > and L < denominator > in the L < nude > or perform mathematical
@@ -360,23 +330,20 @@ L<Str> types and will be accepted by any type constraint that requires an L<Int>
360
330
361
331
Allomorphs can be created using L « angle brackets|/language/quoting#Word_quoting:_<_> » , either used
362
332
standalone or as part of a hash key lookup; directly
363
- using method C < .new > ; and are also provided by some constructs such as
364
- parameters of L « C < sub MAIN > |/language/functions#sub_MAIN» or, in the case of the L < MidRat >
365
- allomorph, L < Rational > literals with large denominators.
333
+ using method C < .new > and are also provided by some constructs such as
334
+ parameters of L « C < sub MAIN > |/language/functions#sub_MAIN» .
366
335
367
336
= begin code
368
- say <42>.^name; # OUTPUT: «IntStr»
369
- say <42e0>.^name; # OUTPUT: «NumStr»
370
- say < 42+42i>.^name; # OUTPUT: «ComplexStr»
371
- say < 1/2>.^name; # OUTPUT: «RatStr»
372
- say <0.5>.^name; # OUTPUT: «RatStr»
373
- say <1/99999999999999999999>.^name; # OUTPUT: «MidRat»
374
- say < 1/99999999999999999999>.^name; # OUTPUT: «MidRatStr»
337
+ say <42>.^name; # OUTPUT: «IntStr»
338
+ say <42e0>.^name; # OUTPUT: «NumStr»
339
+ say < 42+42i>.^name; # OUTPUT: «ComplexStr»
340
+ say < 1/2>.^name; # OUTPUT: «RatStr»
341
+ say <0.5>.^name; # OUTPUT: «RatStr»
375
342
376
343
@*ARGS = "42";
377
- sub MAIN($x) { say $x.^name } # OUTPUT: «IntStr»
344
+ sub MAIN($x) { say $x.^name } # OUTPUT: «IntStr»
378
345
379
- say IntStr.new(42, "42").^name; # OUTPUT: «IntStr»
346
+ say IntStr.new(42, "42").^name; # OUTPUT: «IntStr»
380
347
= end code
381
348
382
349
A couple of constructs above have a space after the opening angle bracket. That space isn't
@@ -405,8 +372,6 @@ The core language offers the following allomorphs:
405
372
NumStr | Num and Str | <42e0>
406
373
ComplexStr | Complex and Str | < 1+2i>
407
374
RatStr | Rat and Str | <1.5>
408
- MidRat | Rat and FatRat | <1/99999999999999999999>
409
- MidRatStr | Rat, FatRat, and Str | < 1/99999999999999999999>
410
375
411
376
= end table
412
377
@@ -447,7 +412,6 @@ my $al := IntStr.new: 42, "forty two";
447
412
say $al.Str; # OUTPUT: «forty two»
448
413
say +$al; # OUTPUT: «42»
449
414
450
- say <1/99999999999999999999>.^name; # OUTPUT: «MidRat»
451
415
say <1/99999999999999999999>.Rat.^name; # OUTPUT: «Rat»
452
416
say <1/99999999999999999999>.FatRat.^name; # OUTPUT: «FatRat»
453
417
= end code
@@ -740,42 +704,24 @@ result.
740
704
741
705
The infectiousness is as follows, with the most infectious type listed first:
742
706
743
- = begin table
707
+ = item * Complex
744
708
745
- Type | Comments |
746
- ========+=================================================+
747
- Complex | |
748
- Num | |
749
- FatRat | |
750
- MidRat | Special infectiousness. See prose that follows. |
751
- Rat | |
752
- Int | |
709
+ = item * Num
753
710
754
- = end table
711
+ = item * FatRat
755
712
756
- The L < MidRat > type is a special type that offers greater precision, like a L < FatRat > type, yet
757
- has the same infectiousness as a L < Rat > type. In other words, operations involving a L < MidRat >
758
- type and a L < MidRat > , L < Rat > , or L < Int > type would result in a L < Rat > result, B < not > L < MidRat >
759
- result (or a L < Num > result if after reduction the denominator ended up over 64-bits, too large for
760
- a L < Rat > ). Otherwise, we're dealing with a type more infectious than a L < MidRat > and the result will
761
- be of that type.
713
+ = item * Rat
762
714
763
- Some examples of what we discussed:
715
+ = item * Int
764
716
765
717
= begin code
766
718
say (2 + 2e0).^name; # Int + Num => OUTPUT: «Num»
767
719
say (½ + ½).^name; # Rat + Rat => OUTPUT: «Rat»
768
720
say (FatRat.new(1,2) + ½).^name; # FatRat + Rat => OUTPUT: «FatRat»
769
- say (FatRat.new(1,2) + <1/99999999999999999999>).^name; # FatRat + MidRat => OUTPUT: «FatRat»
770
-
771
- say (99999999999999999999 * <1/99999999999999999999>).^name;
772
- # Int * MidRat (after reduction, denominator fits into a Rat) => OUTPUT: «Rat»
773
- say (1 + <1/99999999999999999999>).^name;
774
- # Int * MidRat (after reduction, denominator too big to fit into a Rat) => OUTPUT: «Num»
775
721
= end code
776
722
777
- The allomorphs have the same infectiousness as their numeric component. Native types get autoboxed
778
- and have the same infectiousness as their boxed variant.
723
+ The allomorphs have the same infectiousness as their numeric component. Native
724
+ types get autoboxed and have the same infectiousness as their boxed variant.
779
725
780
726
= end pod
781
727
0 commit comments