Week 12



 




This week I felt a sudden urge to try to convert my minlab1 into python to try to learn the language. The first photo is the python code and the second is the java that I did during 205. As you can see in the code it is very different in java then in python. You can sorta till the idea and goal is the same but the code and grammar is different. Python needs no semi colons, which is so weird but convenient because its one less thing I need to worry about. Print doesnt need the system.out. I need to my int variables in to string in order to print, which is interesting. Conditional statements done need parenthesis and after its condition there is a colon instead of a pair of brackets. Input is set to take  in string so I was getting a lot of errors until I found out you have to type cast it to an int. Conveniently, it makes the left side variable a float if the right has division. Down below we see the results when given the same data.




In addition to this little side quest, we did discuss in class about the way to find the square root by hand(Babylonian method). I did it in mips for one of my assignments but I want to show it in java to show you what it does and if/how much it actually works. Gonna try to give a brief run down of the code. D is to check the accuracy of the number that is being found the smaller the D the more accurate. The G value is the Guess that will be manipulated to match the square root. N is the number being square rooted. Our while is squaring G - from N and seeing how close the difference between the numbers are. The reason this works because G is suppose to be sqrt(N) and so if G^2 then we should get N and N-N is zero but G^2 isn't gonna be perfect so we use .001 or smaller to get as close as possible. The while allows it to keep checking until it reaches less than the D. We print the results and compare with an actual square root. The first pair of photos after the first shows how the D effects the result and how close you can get by changing it. The second pair shows how the G effects the result, which is none. No matter what the G is it will fall down to the answer. The last picture is an extreme example of that even though I'm pretty sure it only stored 2 billion something.














Comments

  1. Very nice work, Ronin. I'm sure you'll find Python quite useful.

    ReplyDelete

Post a Comment

Popular posts from this blog

Week 9

Week 10

Week 11