COSC 235: Programming and Problem Solving
February 16, 2009

Joseph D. Sloan
sloanjd @ wofford.edu


Functions and Scope

You have to explictly pass information (parameters) and get information back from a function (assignment and return) or the information stays in the function.

Computers & Numbers

Decimal place system—Base 10, ten digits from 0 thru 9

decimal place system

Binary place system—Base 2, two digits from 0 thru 1

binary place system

There are 10 types of people in this world, those who understand binary and those that don't.

Simple Conversions: There are more sophisticated techniques, but this should give you the idea.

Binary to Decimal:
1010112 = 1x32 + 0x16 + 1x8 + 0x4 + 1x2 + 1x1 = 43

Decimal to Binary:
3910
= 1x32 + 7
= 1x32 + 0x16 + 7
= 1x32 + 0x16 + 0x8 + 7
= 1x32 + 0x16 + 0x8 + 1x4 + 3
= 1x32 + 0x16 + 0x8 + 1x4 + 1x2 + 1
= 1x32 + 0x16 + 0x8 + 1x4 + 1x2 + 1x1 + 0
= 1001112

Internal representations:

Integers: fixed storage for pattern of 1's and 0's, typically 32 bits.
(Python automatically extends into Longs but most languages don't)

Floats: fixed storage for mantissa's sign, mantissa, exponent's sign, and exponent, typically 128 bits.

This means there is a maximun number and a minimum number that can be represented, and there are gaps between floats. Numbers have limited precisions.

In Python

Difference between 1/3 and 1/3.0
1/3.0 isn't really one third
type()
int()
float()

Mixed mode expressions

Math library in Python
Common functions: sqrt, sin, floor, ceil (e.g., ceil(-1.2)), ...
import math
from math import sqrt
from math import *

Homework

 


This page was created by Joe Sloan.
It was last modified around: 16 February 2009
Send mail to: sloanjd@wofford.edu