Can you give a specific example please?
> Easy enough to correct - but it prompts discussion of why do we even have to
> bother with that?
> In a class where we're interested also in pure bare bones Pythonic
> expression of ideas, this provides a nice example of how Python 2 and Python
> 3 differ, but for classes not interested in programming per se, just in
> 'math', it might be seen as a glitch.
>
> --
> ==================================
> "What I cannot create, I do not understand."
> - Richard Feynman
> ==================================
> "Computer science is the new mathematics."
> - Dr. Christos Papadimitriou
> ==================================
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-edu" group.
> To post to this group, send email to sage...@googlegroups.com.
> To unsubscribe from this group, send email to
> sage-edu+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sage-edu?hl=en.
>
On Sat, Oct 29, 2011 at 11:09 AM, michel paul <mpau...@gmail.com> wrote:Can you give a specific example please?
> Here's a relatively minor issue that might not be minor for someone new to
> Sage.
> In illustrating very simple probability as len(outcomes)/len(sample_space),
> integer division occurs, so the probability becomes 0.
len(E)/len(S) will produce 0. We can very easily use 1.0*len(E)/len(S), or we can use Integer(len(E))/len(S). I prefer the latter. Either way, a student response will be, "Why do we have to do that here?"S = [(die1, die2) for die1 in [1..6] for die2 in [1..6]]E = [throw for throw in S if sum(throw) == 7]
I see what you mean:
sage: S = [(die1, die2) for die1 in [1..6] for die2 in [1..6]]
sage: E = [throw for throw in S if sum(throw) == 7]
sage: len(E); len(S); len(E)/len(S); (1*len(E))/(1*len(S))
6
36
0
1/6
I would suggest that this "feature" in Python, was "fixed" in Python 3.0.
Sage has not yet upgraded to 3.0.
> - Michel
>
>> > Easy enough to correct - but it prompts discussion of why do we even
>> > have to
>> > bother with that?
>> > In a class where we're interested also in pure bare bones Pythonic
>> > expression of ideas, this provides a nice example of how Python 2 and
>> > Python
>> > 3 differ, but for classes not interested in programming per se, just in
>> > 'math', it might be seen as a glitch.
>
>
> --
> ==================================
> "What I cannot create, I do not understand."
> - Richard Feynman
> ==================================
> "Computer science is the new mathematics."
> - Dr. Christos Papadimitriou
> ==================================
>
Luckily, Python includes a time machine to import things from the future ;)
sage: from __future__ import division
sage: S = [(die1, die2) for die1 in [1..6] for die2 in [1..6]]
sage: E = [throw for throw in S if sum(throw) == 7]
sage: len(E)/len(S)
0.16666666666666666
Thanks,
Jason
I don't think it would screw up internal stuff.
>
> Plus, the "right" answer is 1/6, not n(1/6), and I think it's
> reasonable to want to show that answer without needing to use Integer
> or 1* (though perhaps not currently possible).
Most likely, we aren't going to change len() to return Sage Integers, so
we're stuck with the Python integer, which is to return floating point
division in this case.
>
> What about the cardinality being an int instead of an Integer? Just
> curious what you think.
I'm curious about the combinat people's reasons. I don't have an
opinion on that yet, since I know those guys think long and hard about
most issues, so I'm sure there must be a good reason.
Thanks,
Jason
def slen(lst):
return Integer(len(lst))
// is in Python 2.x for integer quotient. That's what we should
always use for floor division of integers in the Sage library. It'll
continue to work fine *when* we transition to Python 3, which I see
happening during 2012.
I would to clarify something. When you wrote above that the reason we
don't have / being int floor division by default is "Efficiency.",
that might suggest that having it be anything else would be slower.
However, that is definitely not the case. See below, where when we
switch to division giving floats, the benchmark is unchanged (i.e.,
just as fast).
sage: S = [(die1, die2) for die1 in [1..6] for die2 in [1..6]]
sage: E = [throw for throw in S if sum(throw) == 7]
sage: timeit('len(E)/len(S)')
625 loops, best of 3: 278 ns per loop
sage: len(E)/len(S)
0
sage: from __future__ import division
sage: timeit('len(E)/len(S)')
625 loops, best of 3: 272 ns per loop
sage: len(E)/len(S)
0.16666666666666666
I was in a talk that Guido Van Rosum gave at Scipy 2006 in P3k, and
somebody (me?) asked him about Python using floor division for integer
division by default, and he humbly called it a design mistake on his
part.
> regards
> john perry
>
> PS: For some reason this reminds me of http://www.elsop.com/wrc/humor/unixhoax.htm
>
> --
> You received this message because you are subscribed to the Google Groups "sage-edu" group.
> To post to this group, send email to sage...@googlegroups.com.
> To unsubscribe from this group, send email to sage-edu+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sage-edu?hl=en.
>
>
--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org