/
Num.pod6
123 lines (81 loc) · 4.37 KB
/
Num.pod6
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
=begin pod
=TITLE class Num
=SUBTITLE Floating-point number
class Num is Cool does Real { }
A C<Num> object stores a floating-point number. It is immutable. On most
platforms, it's an IEEE 754 64-bit floating point numbers, aka "double
precision".
X<|Inf (definition)>
X<|∞ (definition)>
=head2 Inf
The value C<Inf> is an instance of C<Num> and represents value that's too large
to represent in 64-bit double-precision floating point number (roughly, above
C<1.7976931348623158e308> for positive C<Inf> and below
C<-1.7976931348623157e308> for negative C<Inf>) as well as returned from certain
operations as defined by the
L<IEEE 754-2008 standard|https://ieeexplore.ieee.org/document/4610935>.
say 2e300 ** 2e300; # OUTPUT: «Inf»
say (-1/0).Num; # OUTPUT: «-Inf»
The C<∞> C<U+221E> Unicode character can be used instead of
the word C<Inf> and can be handy when C<Inf> would otherwise require an
L<unspace|/language/syntax#Unspace>, such as when writing L<Complex|/type/Complex> numbers:
say Inf+Inf\i; # Backslash (unspace) before `i` required
say ∞+∞i; # No backslash is needed
Note that there are just two infinities (positive and negative), so even if an
operation that would instinctively give a "larger" infinity is performed, the
result in still an infinity of the original magnitude. The infinities can be
compared, operated and used as an argument as if they were simply a number
that's too big to represent or to signify "without bounds" or limits:
say ∞²; # OUTPUT: «Inf»
say 42 + Inf === ∞; # OUTPUT: «True»
say atan ∞; # OUTPUT: «1.5707963267949»
say -∞ < 42 < ∞; # OUTPUT: «True»
my $l := 1, 2, 4, 8 ... Inf; # Infinite sequence (no limits)
In some cases, it's used as an implicit value to represent "all of them"
say "House of M".comb(3,Inf).join("←X→");
# OUTPUT: «Hou←X→se ←X→of ←X→M»
In the example above, C<Inf> can be eliminated, since it's the default value for
the second argument of L<C<.comb>|/type/Str#routine_comb>, used to indicate how
many parts should be returned.
Division of an infinity by another infinity results in a C<NaN>:
say ∞/∞; # OUTPUT: «NaN»
=head2 NaN
The value X<C<NaN>|NaN (definition)> is an instance of C<Num> and represents a
floating point not-a-number value, which is returned from some routines where
a concrete number as the answer is not defined, but a L<Numeric|/type/Numeric> value is still
acceptable. C<NaN> is L<defined|/routine/defined> and L<boolifies|/routine/Bool>
to C<True>, but is I<not> numerically equal to any value (including itself).
say cos ∞; # OUTPUT: «NaN»
say (0/0).Num; # OUTPUT: «NaN»
To test for C<NaN>, use L<isNaN|/routine/isNaN> method or L<=== operator|/language/operators#infix_===>:
say (0/0).isNaN; # OUTPUT: «True»
say (0/0).Num === NaN; # OUTPUT: «True»
=head2 method rand
method rand(Num:D: --> Num)
Returns a pseudo random number between 0 and the invocant.
=head2 sub srand
sub srand(Int $seed --> Int:D)
Seeds the pseudo random number generator used by L<Num.rand|/type/Num#rand> with
the provided value. Note that C<srand> is called with a platform dependent
value when a Perl 6 program is started.
=head2 method Capture
Defined as:
method Capture()
Throws C<X::Cannot::Capture>.
=head2 Int
method Int(Num:D:)
Converts the number to an L<Int|/type/Int>. L<Fails|/routine/fail> with
C<X::Numeric::CannotConvert> if the invocant L«is a C<NaN>|/routine/isNaN»
or C<Inf>/C<-Inf>. No L<rounding|/routine/round> is performed.
=head2 Rat
method Rat(Num:D: Real $epsilon = 1e-6)
Converts the number to a L<Rat|/type/Rat> with C<$epsilon> precision. If the invocant
is a C<Inf>, C<-Inf>, or a C<NaN>, converts them to a L<Rat|/type/Rat> with C<0>
L<denominator|/routine/denominator> and C<1>, C<-1>, or C<0> L<numerator|/routine/numerator>, respectively.
=head2 FatRat
method FatRat(Num:D: Real $epsilon = 1e-6)
Converts the number to a L<FatRat|/type/FatRat> with the precision C<$epsilon>. If invocant
is a C<Inf>, C<-Inf>, or a C<NaN>, converts them to a L<FatRat|/type/FatRat> with C<0>
L<denominator|/routine/denominator> and C<1>, C<-1>, or C<0> L<numerator|/routine/numerator>, respectively.
=end pod
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6