OSSU comp sci

MIT 6.100L Intro to CS and Programming using Python

lecture 5 - Floats and Approximation Methods

I’ve already covered some of this content in datacamp, but the different presentation style and being given more finger exercises is really useful.

This is an archived course, but you can access the lecture code and work through alongside the lecturer. I find working using an IDE adds a certain something to the learning.

sidebar: I replaced the battery on this dell xps and am really happy with the way it runs using linux mint.

##Lecture Notes

prev lecture recap:
Talking about Binary numbers…
a way to convert base 10 numbers into base 2 so we can deal with numbers that aren’t nice tidy integers.
for example…

x = 0
for i in ranger(10: 
	x += 0.1
print(x==1)
print(x, '==', 10*0.1)

this comes back with 0.9999999999999999 == 1 which is FALSE.
so we convert base10 to base2 numbers -> binary numbers which is how the computer reads things.

if num < 0:
	is_neg = True
	num = abs(num)
else:
	is_neg = False

result = ''
if num == 0:
	result = '0'

while num > 0:
	result = str(num%2) + result
	num = num//2

if is_neg:
	result = '-' + result

Each time this code passes it divides a number (example 19) by 2 and adds the remainder (either 0 or 1) to the front of the string. The string left at the end is the binary number!

so in the case of 19 the binary representation is 10011:

19/2 = 1 leftover
9/2 = 1 leftover
4/2 = 0 leftover
2/2 = 0 leftover
1/2 = 1 leftover

What about fractions??

i’m fading, doing this on break during nightshift and i’m too tired…going to just watch the lecture and go back to take notes…night!

Eddie(WTR)