Larry Wall wrote:
: Luke Palmer wrote:
: >When do we do integer/rational math and when do we do floating point math?
: Since we have now flooring semantics on modulus and division I wonder
: how the coercion of nums to ints takes place. Does it also use floor?
: E.g. is @array[-0.3] accessing the last element or is it truncating
: to zero and indexing from the front?
Perl 5 truncates, I believe. One could argue that floor is better behaved,
but the 0/-1 transition in subscripts is totally cork-brained anyway, so
I don't think it really matters if our numbers behave when wrapping around
the end of an array.
Perhaps. Then again, it isn't the remainder that would define which
element you're looking at in this case; it would be the integer
portion. And if I understand flooring semantics properly, -0.3 div 1
== -1. Also, -0.3 mod 1 == 0.7; right? Or is that the difference
between mod and %: the former returns the remainder as an integer
(e.g., '0'), while % return the remainder as a Num (e.g., '0.7')?
With truncating semantics, -0.3 div 1 == 0, and -0.3 % 1 == -0.3.
I think that TSa has a point here: converting from Num to Int should
always be equivalent to saying 'Num div 1'. Which means that if you
ever override div to provide different semantics by default (e.g.,
Euclidean), you would also change the semantics of the Num-to-Int
Interestingly, negative fractions, if truncated, would give us a way to
talk about the element off the end of the array, presuming that the endness
of the subscript is determined from the sign before it is truncated:
push @array, 42;
@array[-0.1] = 42; # same thing
@array[-0.0] = 42; # same thing?
Eh. A neat little bell-and-whistle, but more confusing than it is useful.
But it probably doesn't matter. If one really wants "infinite" modular
arrays, then you probably just say @array[$x % @array] or some such,
which gives you the floor semantics.
True enough. I'd suggest doing this implicitly (with 'mod' instead of
'%'), ensuring that virtually every index maps to an existing element;
but then you'd lose the ability to append to an array by assigning to
'@[EMAIL PROTECTED]' (not that I'd do this; but I can see why some might
prefer this idiom to being pushy).
It's the sort of thing that I could see using a trait for: 'my @array
but oroborus' would invoke an implicit modulus on the index, while
standard arrays would not. Likewise, those who don't want the
backward-indexing semantics could remove them by saying something like
'my @array but ray', causing negative indices to throw errors instead.
Ideally, applying both would cause negative indices to choke, but
would wrap too-large positive indices back to the beginning of the
Jonathan "Dataweaver" Lang