Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

why there is no exact result with this fractions sum ?

Hi, here is my code, in order to use it with sagetex (I'm new with sage). I want to make a sum with two fractions.

L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0], 4)
print(L)
print(type(L[0]))
L[0]/L[1]+L[2]/L[3]

and the result is :

[-11, 12, 7, -7]
<class 'int'>
-1.9166666666666665

so, no exact value.

I've made an other test :

L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0],4)
print(L)
print(type(L[0]))
print(L[0]/L[1]+L[2]/L[3])
QQ(L[0]/L[1]+L[2]/L[3])

and the output is :

[-6, -8, -4, 5]
<class 'int'>
-0.050000000000000044
-52215647853572/1044312957071439

instead of -1/20. I would like to understand how to achieve the exact and simplified fraction.

Thank you


Edit :

I tried with this 1 min ago :

L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0],4)
print(L)
print(type(L[0]))
print(L[0]/L[1]+L[2]/L[3])
print(Integer(L[0])/Integer(L[1])+Integer(L[2])/Integer(L[3]))
QQ(L[0]/L[1]+L[2]/L[3])

and the output is :

[5, -8, -6, -13]
<class 'int'>
-0.16346153846153844
-17/104

-39792720200266/243437817695745

so ... i have to do this with "Integer" ?