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, 10 Oct 2018 14:07:28 -0500Use of / operator when defining a functionhttp://ask.sagemath.org/question/43906/use-of-operator-when-defining-a-function/ Hello. I need a clarification on the behaviour of fractions. More precisely, I don't understand the behaviour of the / operator when used inside a def().
I noticed that / finds the integer part of the fraction when used inside a def:
def sumA():
a=randint(2,10)
b=randint(2,10)
c=randint(3,10)
d=randint(4,10)
return [a,b,c,d,a/b + c/d]
Then sumA() the following result:
[8, 3, 9, 8, 3]
That is: 8/3 + 9/8 = 3. How can I make a/b + c/d work like a sum of fractions inside this function?
Oddly enough, the behaviour is different when generating random numbers a and b and adding 1/a + 1/b.
def sumB():
a=randint(2,10)
b=randint(2,10)
return [a,b,1/a + 1/b]
This function adds the fractions in the correct way. The result of sumB() is
[5, 9, 14/45]
Finally, I noticed that writing a*1/b + c*1/d instead of a/b + c/d makes everything work like fractions:
def sumC():
a=randint(2,10)
b=randint(2,10)
c=randint(3,10)
d=randint(4,10)
return [a,b,c,d,a*1/b + c*1/d]
The result of sumC() is
[6, 7, 5, 6, 71/42]
jcarrilloWed, 10 Oct 2018 14:07:28 -0500http://ask.sagemath.org/question/43906/formatting fractions involving square rootshttp://ask.sagemath.org/question/32383/formatting-fractions-involving-square-roots/How can I tell sage that I want the result to be shown as $\frac{1}{\sqrt{3}}$ rather than $\frac13\sqrt3$ when I type <code>show(1/sqrt(3))</code>?BjörnSat, 30 Jan 2016 06:48:36 -0600http://ask.sagemath.org/question/32383/