Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 11 Online
OpenStudy (wolter1980):

In video of recitation 2, in descriptionof function Cube (returns cube of number) was indicated: input(int, float), output(float). My question; Why output - only float if function outputs "int" as well.

OpenStudy (anonymous):

I don't think integer-arithmetic and floating-point-arithmetic are handled the same way by the processor. So even though they both (in Java, at least) require 32 bits of memory to store their value, the compiler must still do a "widening" conversion on the integer so it can carry out whatever mathematical operation (in this case, ^3) it needs to.

OpenStudy (wolter1980):

Don't see any answer here. Or don't understand. But thank you.

OpenStudy (harsimran_hs4):

i think the cube of a number is found out using epsilon... if that is the case then every step you increment the number by some value which is a float and then you proceed ahead so with the specified error you may find a cube root that is float(or perhaps float only because int + float is treated as float)

OpenStudy (anonymous):

Integer operations aren't computed the same way floating point math operations are. The precise details of that are beyond my grasp (or interest), but we all experience it when we first learn to code and try to do something like 7/4 and are surprised when we get 1.

OpenStudy (anonymous):

because that is the specification that they want

OpenStudy (anonymous):

It is kind of like trying to add two fraction together, you need to make them have the same denominator first. In order to do exponent operations on an integer (and those exponents could be stuff like ^.5 (square root)), they need to both be the same type, and by necessity (fractional exponents) that type can't be integer.

OpenStudy (anonymous):

.. no - if we are talking about a function that returns the cube of its argument - you can do all the math inside the function with integers and then just convert it to a float when the answer is returned - for some reason they want a float returned so that is what you do. http://dpaste.com/1025586/

OpenStudy (wolter1980):

This code: def cube(number): """Takes a number and returns the cube of that number Input: number (float or int) Output: number (float)""" return number**3 How this code can convert int to float? http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-4-machine-interpretation-of-a-program/ time:45.24

OpenStudy (wolter1980):

And here: def copyList(LSource, LDest): for e in LSource: LDest.append(e) print 'LDest =', LDest L1 = [] L2 = [1,2,3] copyList(L2,L1) print L1 print L2 copyList(L1, L1) print L1 How this function "copyList" changes L1 and L2 without "return", when LSource, LDest are formal parameters.

OpenStudy (anonymous):

return float(number) - http://docs.python.org/2.7/library/functions.html#float

OpenStudy (wolter1980):

I know what "float()" does. I don't understand why in THIS very code (below, again) specification says that output is only float. (This code was in the recitation 2 video, 45.24 min.) http://dpaste.com/1026049/

OpenStudy (wolter1980):

And again about function and mutable objects. So dealing with them I don't need to use "return" operator?

OpenStudy (anonymous):

About Cube function: Words that goes between """triple quotes just words""" in function, that describes user in short way what functionality of the function SO THEY DONT AFFECT ANYTHING! what i want to tell you that you wouldnt get float output from this function unless u give it float input! the guy that coded this function just messed with description x) About List function: google.com/search?q=list.append

OpenStudy (anonymous):

Wolter: As Owlk said; it looks like a commenting error in the function, not a misunderstanding on your part. The only part of the function cube(number) that does anything is the last line: return number**3 All it does is multiply the number by itself (twice) to produce a cube. This doesn't change its type from into to float. However, as Ajk mentioned, if you were using the ** operator with a value like .5, this will give a non-integer answer and so will return a float (the alternative would be for Python to truncate the value, which users probably don't want). For example, 3**.5 gives you the float 1.7320508075688772 In terms of functions and mutables, it just depends what you're trying to do, really. If you want to change a value in a mutable object, and you write a function that causes that change, you're fine. However, if you want to do something else you may still want to return a value. (For future reference, I would suggest creating a new question rather than adding new questions to an existing discussion. It's easier to follow and makes it easier for other people to find relevant help.)

OpenStudy (anonymous):

well, i didn't watch the recitation - the information in the the docstring should be the function specification - if they presented that function as you posted then it doesn't comply with the function spec, period. you have to remember that they are just grad students - sometimes i think they don't pay very good attention, or maybe they are just sloppy - there are similar instances of inattention to detail and unfortunately sometimes those minor errors drive newbies nuts when there is an obvious contradiction presented to them by an authority. Another take on this would be that the error was intentional, this is a recitation video and perhaps it was meant to elicit comment/discussion.

OpenStudy (anonymous):

if i can find something i will post a link - it might not be a good practice to deliberately mutate an object 'outside' of a functions local scope - but then again if you really need that functionality for a particular solution then .... might want to search around on the net or read some of this: http://wiki.python.org/moin/BeginnerErrorsWithPythonProgramming http://wiki.python.org/moin/BeginnersGuide it might not be considered Pythonic i haven't read these yet - http://stackoverflow.com/questions/14540789/why-is-python-global-scope-affected-by-local-scope-operations http://stackoverflow.com/questions/8447947/is-it-possible-to-modify-variable-in-python-that-is-in-outer-but-not-global-sc http://stackoverflow.com/questions/5234126/python-variable-scope-passing-by-reference-or-copy you may get a better answer in the #python irc channel - there a bit ruff in there but i have always gotten excellent answers https://www.google.com/search?q=mutating+objects+outside+of+a+function's+local+scope+python&aq=f&oq=mutating+objects+outside+of+a+function's+local+scope+python

OpenStudy (wolter1980):

Thank you for your help. Mutable objects are still very hard for me. But a little bit more clear then before.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!