ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 02 Oct 2019 09:02:50 -0500Request: Have the len function output a Sage Integer instead of a Python inthttp://ask.sagemath.org/question/48141/request-have-the-len-function-output-a-sage-integer-instead-of-a-python-int/In the following code (See https://sagecell.sagemath.org/?q=cctcfj ):
a=[4,5,6]
b=[0,1,2]
for i in zip([0,1,2],a):
print i[0],"divided by",len(a),"is equal to",i[0]/len(a)
a=[4,5,6]
b=range(len(a))
print(b)
for i in zip(b,a):
print i[0],"divided by",len(a),"is equal to",i[0]/len(a)
Gives the following output
0 divided by 3 is equal to 0
1 divided by 3 is equal to 1/3
2 divided by 3 is equal to 2/3
[0, 1, 2]
0 divided by 3 is equal to 0
1 divided by 3 is equal to 0
2 divided by 3 is equal to 0
Greetings
-AAdrian sotoWed, 02 Oct 2019 09:02:50 -0500http://ask.sagemath.org/question/48141/Possible bug of sum functionhttp://ask.sagemath.org/question/33068/possible-bug-of-sum-function/Consider the following code:
def s(n):
return sum(k/n for k in range(n + 1))
L0 = s(2)
for n in range(2, 3):
L1 = s(n)
The answers are (using sage 7.1):
print L0, L1
(3/2, 1)
In the second case sage is using the usual integer division of python 2.x while in the first one it is using the exact representation of rationals. Is this a known bug?
jllbMon, 11 Apr 2016 12:15:50 -0500http://ask.sagemath.org/question/33068/range and division : unexpected behaviorhttp://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/Consider the following snippet :
N=5
# code 1
for n in range(N,N+1):
for k in range(0,n):
print k/n
print '-'*10
# code 2
n=N
for k in range(0,n):
print k/n
I was expecting code 1 and code 2 to print the same output. This is not the case :
0
0
0
0
0
----------
0
1/5
2/5
3/5
4/5
In the first case, `k/n` is Python-evaluated as an integer division, in the second case, `k/n` is Sage-evaluated as a fraction. Can someone elaborate please ?
I only notice that substituting `srange(N,N+1)` to `range(N,N+1)` fixes the problem.
candideSun, 27 Mar 2016 04:28:01 -0500http://ask.sagemath.org/question/32891/Integer result for fractionhttp://ask.sagemath.org/question/32347/integer-result-for-fraction/ Hi!
Can somebody explain me this strange behavior of Sage :
for p in range(1,10):
for q in range(1,10):
print p/q
Then `p/q` return the integer division.
However:
for p in range(1,10):
print p/7
give rational results.
A solution is to use `Rational(p)/Rational(q)` instead of `p/q`. But it is not an explanation.
Thanks,
Arnaud
Arnaud1418Wed, 27 Jan 2016 13:12:43 -0600http://ask.sagemath.org/question/32347/When does 1/2=0 ? (python's integer division Vs Sage's exact fractions)http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/Hi all !
I know that python evaluates 1/2 to zero because it is making an integer division. Well. I was expecting Sage not to do so.
It does more or less ...
------ sagess.py ----------
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def MyFunction(x,y):
print x/y
MyFunction(1,2)
------ end of sagess.py ----------
When I launche that script from the command line, I got what Python would give :
18:08:03 ~/script >$ ./sagess.py
0
But when I launch it from the sage'terminal it is less clear how it works :
18:16:04 ~/script >$ sage
----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
At the import, it returns zero, but when I reask the same computation, it gives the correct 1/2
I know that writing
float(x)/y
"fixes" the problem. But it looks weird and I want to keep exact values.
What do I have to write in my scripts in order to make understand to Sage that I always want 1/2 to be 1/2 ?
Thanks
Have a good night !
Laurent
Laurent ClaessensTue, 01 Feb 2011 05:24:55 -0600http://ask.sagemath.org/question/7917/