Hi Dima, hi Rado,
On 5 Sep., 11:58, Dima Pasechnik <dim...@gmail.com> wrote:
> I see no reason for int(3)^3 not to return a Sage Integer, and for int(3)^-3
> not to return a QQ.
I see a reason:
x^n is recursively defined by
x if n==0
x*x^(n-1) otherwise
Hence, if x is a Python int then x^n should be a Python int (namely
the n-fold product of x with itself) even if n is a Sage Integer.
Cheers,
Simon
is 4^(-2) (use various kinds of integers) integer rational float?
ditto for
5^(-2) ?
Seems to me that the presence of python integers is an inconsistency
waiting to appear,
and
the only proper use of python ints is as a sage integer which happens
to be, at the moment,
small in magnitude.
Lisp does this right, with fixnums automatically promoted to bignums
if they get too big.
Coercion from rational to integer happens if the rational has a
denominator of 1.
There are lots more of these, e.g complex with imag part zero etc.
Do you want
these all to be equal....
1+0*i, 1/1, 1, int(1)? 1.0 1.0d0? maybe.
RJF
Umm... Python ints do this too. Does that mean Python gets it right?
What Python is missing is a rational type, so 5^(-2) is either
truncated or approximated in floating point. (It can be argued that
rationals are not what one wants--computing with rationals can easily
become atrociously expensive.)
Anyway, +1 to fixing this.
> Coercion from rational to integer happens if the rational has a
> denominator of 1.
> There are lots more of these, e.g complex with imag part zero etc.
> Do you want
> these all to be equal....
> 1+0*i, 1/1, 1, int(1)? 1.0 1.0d0? maybe.
Yes. We have a clear rule about this in Sage as delineated in the
coercion manual.
> On Sep 5, 6:52 am, Dima Pasechnik <dimp...@gmail.com> wrote:
>> this is now #11779
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
This is what Sage *should* do, IMHO.
> Certain purists argue that I am wrong, and that the loss of precision must
> be preferred...
These "purists" are usually actually "applied-ists", and for numerical
computation they are often "right". When I'm doing lots of numerical
computation (which I do actually do sometimes), I tend to agree, and
set the relevant modes in Sage.
That said, for consistency with the rest of Sage, e.g., that int(1) +
Integer(2) is an Integer, we should have int(3)^Integer(2) be an
Integer.
>
>>
>> and
>> the only proper use of python ints is as a sage integer which happens
>> to be, at the moment,
>> small in magnitude.
>> Lisp does this right, with fixnums automatically promoted to bignums
>> if they get too big.
>> Coercion from rational to integer happens if the rational has a
>> denominator of 1.
>> There are lots more of these, e.g complex with imag part zero etc.
>> Do you want
>> these all to be equal....
>> 1+0*i, 1/1, 1, int(1)? 1.0 1.0d0? maybe.
>> RJF
>>
>> On Sep 5, 6:52 am, Dima Pasechnik <dim...@gmail.com> wrote:
>> > this is now #11779
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to
> sage-devel+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
It seems like we've had some big problems with this, in that maxima will
sometimes simplify things because it assumes an exact rational number
was entered, when in reality, an approximate number was entered [1].
Maybe a more correct "clean" resolution is to convert every float to a
rational interval, or a float interval, and do interval arithmetic? Of
course, that can get really nasty too...
Jason
[1] See http://trac.sagemath.org/sage_trac/ticket/2400 for example, or
http://trac.sagemath.org/sage_trac/search?q=keepfloat
I'm finding it hard to understand what's going on in Python 3, but here it is:
Python 3.2.2 (default, Sep 5 2011, 04:33:58)
[GCC 4.6.1 20110819 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/4
0.25
>>> 2**(-2)
0.25
Hmmm. OK, so what's this rational concept number that Python 3 has?
It must be:
>>> from fractions import Fraction
>>> Fraction(1, 4)
Fraction(1, 4)
>>> Fraction(1, 4) + Fraction(2, 3)
Fraction(11, 12)
Ah, alright. However:
>>> Fraction(1/4)
Fraction(1, 4)
>>> Fraction(2/3)
Fraction(6004799503160661, 9007199254740992)
So this can get confusing really quick. What's going on is that
Fraction takes a python float and finds the closest rational
approximation where the denominator is bounded by a default (large)
bound that can be modified by passing an optional argument.
Note that the fractions module is actually present in python 2.6 (so
it's in Sage right now!) but (a) Fraction(1/2) will fail from the sage
prompt because 1/2 is first preprocessed into a Sage rational, and
Fraction() doesn't know what to do with that and (b) even if you do
sage -python you will see different behaviour than in Python 3, e.g.
Fraction(2/3) will return Fraction(0, 1) because in python 2.6 you
have 2/3=0, while in python 3+ you have 2/3=0.66666...
I'm not sure we can stay compatible with Python on this *and* retain our sanity.
Best,
Alex
--
Alex Ghitza -- Lecturer in Mathematics -- The University of Melbourne
-- http://aghitza.org
On 5 Sep., 17:44, Dima Pasechnik <dim...@gmail.com> wrote:
> Presently, int()^whatever stays Pythonic, no matter it causes a loss of
> precision, or not.
Not true:
sage: 3r^ZZ(-3)
0.037037037037037035
sage: 3r^QQ(-3)
1/27
On 6 Sep., 06:36, Dima Pasechnik <dim...@gmail.com> wrote:
> On Tuesday, 6 September 2011 11:08:02 UTC+8, leif wrote:
>
Of course int(n)^-k should be int(1)/int(n)^k, yielding either a
Python int or float, to stay "pythonic", or, more precisely, within
Python types.
> (unless you want to stay Pythonic, and get int(1)/int()^k, i.e. 0 in Python
> 2, which would be
> even more stupid than the current conversion into float)
I consider this just a Python "bug" (perhaps inherited from early bad
design), since Python (now) does have explicit integer / truncating
division, a // b.
> Now, as int()^-k should to be Integer, so should be int()^k, too.
The former shouldn't, so the latter shouldn't either. ;-)
-leif
Done.