Some questions about using NaN and Inf

2007-10-06 Thread brian d foy
I'm thinking about how to explain Perl 6's numbers to the beginners
just picking up Learning Perl 6. I had some questions about NaN and Inf
(which I can't just try since neither Parrot or Pugs appear to know
about these yet).

* In S02's table of Immutable types,  it mentions that Int allows Inf
and NaN, but doesn't say anything about Num and Complex handling them. 
Is it that it's unexpected that Int would handle it, so you have to say
this explicitly, or um, I don't have a good alternative :)

   Int Perl integer (allows Inf/NaN, arbitrary precision, etc.)
   Num Perl number
   Complex Perl complex number

The complex and num  native type handles NaN accroding to Native
type, a couple of subsections back, and I'm confident that Num and
Complex should have them too. It just looked odd to me that only one of
Int, Complex, and Num said anything about it.

* will NaN, -Inf, or +Inf be literal values (or something close) so I
can use them in comparisons? e.g. $x ~~ NaN. I see uses of Inf in list
creation ( 1 .. Inf ), but can I use that everywhere?

* If I can match $x to NaN (or its stand-in), what happens when $x is
undef? There's a note about this in S02 (Conjecture: num might ...).
Native type say that an int type defaults to 0, which complicates
things for beginners, but if everything starts off as a num, it doesn't
matter until we talk about types.

* If I declare a sub to return a number of some sort (either by using
Cof or Cas, what happens when the value is NaN or Inf? I suppose
that should be fine as a return value, but it also seems that if
someone wants to impose some sort of constaint on the return value that
they wouldn't want exceptional values.


Re: Some questions about using NaN and Inf

2007-10-06 Thread Moritz Lenz
brian d foy wrote:
 * If I can match $x to NaN (or its stand-in), what happens when $x is
 undef? 

undef is a property of the container variable (that it holds no value),
whereas NaN is a property of the content (like 1/0). so undef ~~ NaN
should be false IMHO.

 There's a note about this in S02 (Conjecture: num might ...).
 Native type say that an int type defaults to 0, which complicates
 things for beginners, but if everything starts off as a num, it doesn't
 matter until we talk about types.

And native means no container, no magic, so it can't be undef.

 * If I declare a sub to return a number of some sort (either by using
 Cof or Cas, what happens when the value is NaN or Inf? I suppose
 that should be fine as a return value, but it also seems that if
 someone wants to impose some sort of constaint on the return value that
 they wouldn't want exceptional values.

return a native type?

-- 
Moritz Lenz
http://moritz.faui2k3.org/ |  http://perl-6.de/



signature.asc
Description: OpenPGP digital signature


Re: Some questions about using NaN and Inf

2007-10-06 Thread brian d foy
In article [EMAIL PROTECTED], brian d foy
[EMAIL PROTECTED] wrote:

 I'm thinking about how to explain Perl 6's numbers to the beginners
 just picking up Learning Perl 6. I had some questions about NaN and Inf
 (which I can't just try since neither Parrot or Pugs appear to know
 about these yet).

Oi, sent that before I was done writing it. I was looking way back into
the past at Numeric literals, take 1

http://www.nntp.perl.org/group/perl.perl6.documentation/2002/11/msg205.h
tml

and wondering how much of that is should be in S02.

For comparisons, how are we going to use Inf and NaN? Are those going
to be special flyweight objects, so:

   $x = 1 / 0;
   
   $x == Inf;# is it the same value
   $x === Inf;  # it is always the same object


Pair notation for number radix

2007-10-06 Thread brian d foy
This is basically the same question I had about file test operators
earlier
(http://www.nntp.perl.org/group/perl.perl6.language/2007/04/msg27415.htm
l). I never got an answer on my syntax question and the discussion went
off to talk about file tests instead of pair notation. 

From S02 The general radix form of a number involves prefixing with
the radix in adverbial form.

This seems to say that there are non-general radix forms, and that
those might involve a different radix form that's not adverbial.

Later in the Literals section of S02, there's a chart of the
corresponding forms for fat arrow, pair, and paren notation. It has

   a = 'foo'  :afoo  :a(foo)

That looks like it might mean that these are corresponding forms:

   8 = 377:8377:8(377)

Now, if I can do that, what happens to the pair form in a hash composer
when I want the key of '8' and the value of :10377?

Also, going a bit further, the table lists

   a = foo bar  :afoo bar  :a(foo bar)

So can I do things like

   255 = 10 1 0 6;  # hey, that looks like an IP address

   :25510 1 0 6; # is that the same as :255[ 10,1,0,6 ] ?

And, if that works, what might this do? 

   q:w:25510 1 0 6


Re: Some questions about using NaN and Inf

2007-10-06 Thread Darren Duncan

At 3:20 PM -0500 10/6/07, brian d foy wrote:

For comparisons, how are we going to use Inf and NaN? Are those going
to be special flyweight objects, so:

   $x = 1 / 0;
  
   $x == Inf;# is it the same value

   $x === Inf;  # it is always the same object


Be mindful of the difference between value types and non value types. 
For value types, the generalized value equality test === can return 
true for 2 objects if they have the same value, but for non value 
types, === tests if they are the same object.  All built-in number 
types are value types, so === and == would return the same answer for 
them.  By contrast, the =:= operator always tests if 2 things are the 
same object or not, even for those of value types. -- Darren Duncan