-
Notifications
You must be signed in to change notification settings - Fork 135
/
numeric.t
154 lines (123 loc) · 5.91 KB
/
numeric.t
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
use v6;
use Test;
plan 69;
isa-ok 1, Int, '1 produces a Int';
does-ok 1, Numeric, '1 does Numeric';
does-ok 1, Real, '1 does Real';
isa-ok 1.Num, Num, '1.Num produces a Int';
does-ok 1.Num, Numeric, '1.Num does Numeric';
does-ok 1.Num, Real, '1.Num does Real';
# L<S02/Rational literals/Rational literals are indicated>
is-approx <1/2>, 0.5, '<1/2> Rat literal';
isa-ok <1/2>, Rat, '<1/2> produces a Rat';
does-ok <1/2>, Numeric, '<1/2> does Numeric';
does-ok <1/2>, Real, '<1/2> does Real';
isa-ok <0x01/0x02>, Rat, 'same with hexadecimal numbers';
#?rakudo 2 todo "Unsure of what's acceptable for val()"
ok <1/-3>.WHAT === Str, 'negative allowed only on numerator';
ok <-1/-3>.WHAT === Str, 'negative allowed only on numerator';
isa-ok <-1/3>, Rat, 'negative Rat literal';
ok <-1/3> * -3 == 1, 'negative Rat literal';
# https://github.com/Raku/old-issue-tracker/issues/3875
is <0x01/0x03>, (0x01/0x03), 'Rat works with hexadecimal numbers';
is <0b01/0b10>, (0b01/0b10), 'Rat works with binary numbers';
#?rakudo 2 todo 'Adverbial numbers in Rat literals not supported'
is <:13<01>/:13<07>>, (1/7), 'Rat works with colon radix numbers';
is <:12<1a>/:12<7b>>, (:12<1a> / :12<7b>), 'Rat works with colon radix numbers';
# L<S02/Complex literals/Complex literals are similarly indicated>
isa-ok <1+1i>, Complex, '<1+1i> is a Complex literal';
isa-ok <+2+2i>, Complex, '<+2+2i> is a Complex literal';
isa-ok <-3+3i>, Complex, '<-3+3i> is a Complex literal';
isa-ok <+4-4i>, Complex, '<+4-4i> is a Complex literal';
isa-ok <-5-5i>, Complex, '<-5-5i> is a Complex literal';
does-ok <1+1i>, Numeric, '1+1i does Numeric';
nok <1+1i>.does(Real), '1+1i doesn\'t do Real';
isa-ok <1*1i>, Str, '1*1i is a Str';
is <3+2i>, 3 + 2i, '<3+2i> produces correct value';
is <+3+2i>, +3 + 2i, '<+3+2i> produces correct value';
is <-3+2i>, -3 + 2i, '<-3+2i> produces correct value';
is <+3-2i>, +3 - 2i, '<+3-2i> produces correct value';
is <-3-2i>, -3 - 2i, '<-3-2i> produces correct value';
is <3.1+2.9i>, 3.1 + 2.9i, '<3.1+2.9i> produces correct value';
is <+3.2+2.8i>, +3.2 + 2.8i, '+<3.2+2.8i> produces correct value';
is <-3.3+2.7i>, -3.3 + 2.7i, '-<3.3+2.7i> produces correct value';
is <+3.4-2.6i>, +3.4 - 2.6i, '+<3.4-2.6i> produces correct value';
is <-3.5-2.5i>, -3.5 - 2.5i, '-<3.5-2.5i> produces correct value';
is <+3.1e10+2.9e10i>, 3.1e10 + 2.9e10i, '<3.1e10+2.9e10i> produces correct value';
is <+3.1e+11+2.9e+11i>, 3.1e11 + 2.9e11i, '<+3.1e+11+2.9e+11i> produces correct value';
is <-3.1e+12-2.9e+12i>, -3.1e+12 + -2.9e+12i, '<-3.1e+12-2.9e+12i> produces correct value';
is-approx <-3.1e-23-2.9e-23i>.re, -3.1e-23, '<-3.1e-23-2.9e-23i> produces correct real value';
is-approx <-3.1e-23-2.9e-23i>.im, -2.9e-23, '<-3.1e-23-2.9e-23i> produces correct imaginary value';
is-approx <3.1e-99+2.9e-99i>.re, 3.1e-99, '<3.1e-99+2.9e-99i> produces correct real value';
is-approx <3.1e-99+2.9e-99i>.im, 2.9e-99, '<3.1e-99+2.9e-99i> produces correct imaginary value';
is <NaN+Inf\i>, NaN + Inf\i, '<NaN+Inf\i> produces correct value';
is <NaN-Inf\i>, NaN - Inf\i, '<NaN-Inf\i> produces correct value';
# https://github.com/Raku/old-issue-tracker/issues/5759
{
isa-ok <0--Inf\i>, Str, '0--Inf\i is a Str';
isa-ok <0++Inf\i>, Str, '0++Inf\i is a Str';
isa-ok <0+-Inf\i>, Str, '0+-Inf\i is a Str';
isa-ok <0-+Inf\i>, Str, '0-+Inf\i is a Str';
isa-ok <--Inf-1i>, Str, '--Inf-1i is a Str';
isa-ok <++Inf-1i>, Str, '++Inf-1i is a Str';
isa-ok <+-Inf-1i>, Str, '+-Inf-1i is a Str';
isa-ok <-+Inf-1i>, Str, '-+Inf-1i is a Str';
}
# https://github.com/Raku/old-issue-tracker/issues/1721
is-approx 3.14159265358979323846264338327950288419716939937510e0,
3.141592, 'very long Num literals';
# https://github.com/Raku/old-issue-tracker/issues/1566
{
eval-lives-ok '0.' ~ '0' x 19,
'parsing 0.000... with 19 decimal places lives';
eval-lives-ok '0.' ~ '0' x 20,
'parsing 0.000... with 20 decimal places lives';
eval-lives-ok '0.' ~ '0' x 63,
'parsing 0.000... with 63 decimal places lives';
eval-lives-ok '0.' ~ '0' x 66,
'parsing 0.000... with 66 decimal places lives';
eval-lives-ok '0.' ~ '0' x 1024,
'parsing 0.000... with 1024 decimal places lives';
}
# https://github.com/Raku/old-issue-tracker/issues/1403
ok 0e999999999999999 == 0, '0e999999999999 equals zero';
# We are not afraid of unicode
{
#?rakudo.jvm skip 'is not a valid number'
#?rakudo.js skip 'unsupported unicode stuff'
is ۵۵, 55, "We can handle Unicode digits";
#?rakudo.jvm 3 skip 'bogus term'
#?rakudo.js 3 skip 'unsupported unicode stuff'
is ⅷ , 8, "We can handle Unicode non-digit numerics";
is ⅔, 2/3, "We can handle vulgar fractions";
is 𒑡 × 𒑒, 2/3, "We can multiply cuneiform :-)";
#?rakudo.jvm skip 'Prefix - requires an argument, but no valid term found'
#?rakudo.js skip 'unsupported unicode stuff'
ok -𝑒 ** −π\i ≅ 1, "We can write 1 in funny ways too";
is ∞, Inf, "yeah, we do that too...";
}
# https://github.com/rakudo/rakudo/issues/2094
subtest '#2094 prefix as post fix works on number literals' => {
plan 6;
cmp-ok 42.:<->, '==', -42, 'use negation as postfix';
cmp-ok 42.:<~>, '===', "42", 'use ~ as postfix';
cmp-ok 42.:«~», '===', "42", 'use « » to wrap the prefix';
cmp-ok 42.:<<~>>, '===', "42", 'use << >> to wrap the prefix';
cmp-ok 42.:["~"], '===', "42", 'use [" "] to wrap the prefix';
cmp-ok 42.:<<'~'>>, '===', "42", "use <<' '>> to wrap the prefix";
}
# https://github.com/rakudo/rakudo/issues/3694
#?rakudo.jvm skip 'uniprop NYI'
#?DOES 1
{
subtest "check simple non-ascii numerification" => {
for (^0x0FFF).grep({
.uniprop eq 'Nd' and .unival == 1|2|3
}).batch(3)>>.chr>>.join -> $string {
is-deeply $string.Numeric, 123, "is '$string'.Numeric ok?";
is-deeply $string.Int, 123, "is '$string'.Int ok?";
}
done-testing;
}
}
# vim: expandtab shiftwidth=4