Re: Integerizing a rat involves truncating its tail
HaloO, Darren Duncan wrote: Or maybe your question is more about what method to use by default if users don't explicitly choose one? Yes. I thought we have gone over this in the div/mod discussion that ended with specifying floor semantics for %. I sort of hoped for a synopsis update with subject 'fossil spotted by TSa' or so :) Regards, TSa. -- The Angel of Geometry and the Devil of Algebra fight for the soul of any mathematical being. -- Attributed to Hermann Weyl
Re: Integerizing a rat involves truncating its tail
HaloO, I asked: just re-reading S03 I saw that it defines the Rat to Int conversion as truncation. Hmm, does assuming floor semantics reveal the availability of the tail function for Nums? (-1.25).tail == 0.75 Can it also be used as an lvalue? my Rat $x = -5/4; # note that this is (-5)/4 not -(5/4) $x.tail = 0; say $x; # -2 The idea here is that the tail of a Num points towards -Inf and ends at the next Int in that direction. So numbers are snakes crawling rightwards ;) Regards, TSa. -- The Angel of Geometry and the Devil of Algebra fight for the soul of any mathematical being. -- Attributed to Hermann Weyl
Integerizing a rat involves truncating its tail
HaloO, just re-reading S03 I saw that it defines the Rat to Int conversion as truncation. Why not floor semantics like in %? Actually I would recommend floor semantics whenever an integer is coerced. With the sole exception of Num propably using rounding. Regards, TSa. -- The Angel of Geometry and the Devil of Algebra fight for the soul of any mathematical being. -- Attributed to Hermann Weyl
Re: Integerizing a rat involves truncating its tail
I think nearest makes more sense. People will be really surprised when /1 turns into 0. -- Mark Biggar [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] -- Original message -- From: TSa [EMAIL PROTECTED] HaloO, just re-reading S03 I saw that it defines the Rat to Int conversion as truncation. Why not floor semantics like in %? Actually I would recommend floor semantics whenever an integer is coerced. With the sole exception of Num propably using rounding. Regards, TSa. -- The Angel of Geometry and the Devil of Algebra fight for the soul of any mathematical being. -- Attributed to Hermann Weyl
Re: Integerizing a rat involves truncating its tail
TSa wrote: just re-reading S03 I saw that it defines the Rat to Int conversion as truncation. Why not floor semantics like in %? Actually I would recommend floor semantics whenever an integer is coerced. With the sole exception of Num propably using rounding. If the difference matters to people, I suggest just having available a range of explicit rounding methods to choose from, though not necessarily as separate operators, but rather as an enumerated type you can supply as an additional argument (or trait?) to a routine that would do rounding, to coerce the desired semantics. In my Muldis D language, I have so far these 7 rounding methods as values of a particular enumerated type: half_down|half_up|half_even|to_floor|to_ceiling|to_zero|to_inf, for use as an extra argument. To give you an idea of what I refer to. These can also apply to any situation of simply rounding to lesser precision, not necessarily just to integers. Or maybe your question is more about what method to use by default if users don't explicitly choose one? -- Darren Duncan
Re: Integerizing a rat involves truncating its tail
The choice of floor vs ceiling is essentially arbitrary, as long as its consistent; using truncation or rounding is mathematically unsound. Most implementations use floor, though. So in general I would expect these to hold: x div y = floor(x/y) x mod y = x - y * floor(x/y) Most importantly, this means that -3 mod 5 should yield 2, not -3. Perl5, Python, and Ruby get this right; Java, JavaScript, and PHP do not. (C's behavior in this regard is theoretically implementation-dependent but most implementations return the negative value.) I would also expect the modulus operator to be generalized to non-integers, since there is nothing in the above formulae that requires integral inputs. f mod 1 would then return the fractional part of a number, for instance. -- Mark J. Reed [EMAIL PROTECTED]
Re: Integerizing a rat involves truncating its tail
On Fri, Mar 28, 2008 at 3:47 PM, [EMAIL PROTECTED] wrote: I think nearest makes more sense. People will be really surprised when /1 turns into 0. They shouldn't be, if they're asking for an integer specifically. That's what happens now in Perl 5... If you have a rational number and need to turn it into an integer, best be explicit about how you want the conversion done. I'd almost be tempted to have no default coercion there, but that'd break too much ported code. -- Mark J. Reed [EMAIL PROTECTED]