Skip to content

Commit 02361b0

Browse files
committed
spec stringified forms of Complex and Rat
1 parent 8fcd8d6 commit 02361b0

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

S32-setting-library/Numeric.pod

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ DRAFT: Synopsis 32: Setting Library - Numeric
2020

2121
Created: 19 Mar 2009 extracted from S29-functions.pod
2222

23-
Last Modified: 28 June 2011
24-
Version: 11
23+
Last Modified: 6 Jan 2012
24+
Version: 12
2525

2626
The document is a draft.
2727

@@ -340,7 +340,7 @@ C<Num> is a machine-precision numeric real value.
340340
C<Complex> is an immutable type. Each C<Complex> object stores two numbers,
341341
the real and imaginary part. For all practical purposes a C<Complex> with
342342
a C<NaN> in real or imaginary part may be considered a C<NaN> itself (and
343-
C<(NaN + 1i) ~~ NaN> is C<True>).
343+
C<(NaN+1i) ~~ NaN> is C<True>).
344344

345345
Coercion of a C<Complex> to any C<Real> returns the real part (coerced, if
346346
necessary) if the imaginary part is 0, and fails otherwise. Comparison
@@ -354,7 +354,8 @@ the C<Complex> to a real number blindly.
354354
our Complex multi method new(Real $re, Real $im)
355355

356356
Constructs a C<Complex> number from real and imaginary part. This is the
357-
method form of C<$re + ($im)i>.
357+
method form of C<$re+$im\i>. (But use the C<< <1+2i> >> form for literals,
358+
so that you don't have to worry about precedence or rely on constant folding.)
358359

359360
=item polar
360361

@@ -375,6 +376,22 @@ Returns the real part of the complex number.
375376

376377
Returns the imaginary part of a complex number.
377378

379+
=item gist
380+
381+
our Str multi method gist()
382+
383+
Returns a string representation of the form "C<1+2i>", without
384+
internal spaces. (C<Str> coercion also returns this.)
385+
386+
=item perl
387+
388+
our Str multi method perl()
389+
390+
Returns a string representation corresponding to the unambiguous
391+
C<val()>-based representation of complex literals, of the form "C<< <1+2i> >>",
392+
without internal spaces, and including the angles that keep the C<+>
393+
from being treated as a normal addition operator.
394+
378395
=back
379396

380397
=head2 Trigonometric functions
@@ -464,32 +481,58 @@ denominator were stored in a normal form: both numerator and denominator are
464481
minimal in their magnitude, and the denominator is positive. So
465482
C<Rat.new(2, -4).denominator> return C<2>, because the normal form is C<-1/2>.
466483

484+
(An implementation is allowed to be lazy about this internally when it
485+
determines that normalizing repeatedly is detrimental to performance,
486+
such as when adding a column of numbers that all have an internal
487+
denominator of 100.)
488+
467489
=over
468490

469491
=item new
470492

471493
multi method new(Int $num, Int $denom)
472494

473495
Constructs a C<Rat> object from the numerator and denominator.
474-
Fails if C<$denom == 0>.
496+
Fails if C<$denom == 0>. You can use division to produce a C<Rat>
497+
through constant folding, but generally if you know the values in
498+
advance, you should use one of literal forms so that you don't have to
499+
rely on precedence. You may use the C<val()>-based C<< <3/5> >> form,
500+
or you can simply write decimal numbers with a decimal point, since
501+
C<12.34> is essentially identical to C<< <1234/100> >> as a literal.
475502

476503
=item nude
477504

478505
our Seq[Int] multi method nude()
479506

480-
Returns a C<Seq> of numerator and denominator
507+
Returns a C<Seq> of numerator and denominator.
481508

482509
=item denominator
483510

484511
our Int multi method denominator()
485512

486-
Returns the denominator
513+
Returns the denominator.
487514

488515
=item numerator
489516

490517
our Int multi method numerator()
491518

492-
Returns the numerator
519+
Returns the numerator.
520+
521+
=item gist
522+
523+
our Str multi method gist()
524+
525+
Returns a string representation by first converting to C<Num> and then using the normal decimal
526+
output of that type. (C<Str> coercion also does this.)
527+
528+
=item perl
529+
530+
our Str multi method perl()
531+
532+
Returns a string representation corresponding to the unambiguous
533+
C<val()>-based representation of complex literals, of the form "C<< <3/5> >>",
534+
without internal spaces, and including the angles that keep the C</>
535+
from being treated as a normal division operator.
493536

494537
=back
495538

0 commit comments

Comments
 (0)