Skip to main content

Class 4: Numeric Computation

Held: Friday, 27 January 2017

We explore a bit more about data in Scheme, particularly the ways in which our version of Scheme supports numbers.

Preliminaries

Overview

  • Values and types
  • Kinds of numbers
  • Basic numeric operations

Updates

News / Etc.

  • We have a lot of preliminaries today. Sorry!
  • It appears that there was a problem with links on the news page. I think I have fixed it. Let me know if you encounter other problems. And yes, it’s fine if your email says PROBLEM. (It can also say QUESTION if you have a question.)
  • Lab writeups will start Monday. Generally, I pick one question from the lab and ask you to email an answer to the grader.
  • Reminder: Your partner from Wednesday is your partner from HW 1.
  • I have heard some concerns that “I don’t like group work, and this class seems to have a ton of it.” I thought I should address my reasons for giving so much group work.
    • You know one from the first day: People solve problems better in groups.
    • I also firmly believe that almost no matter what you do in life, you will be called upon to collaborate with other people, people of differing skills and levels of commitment. You might as well get practice now.
    • [Story about Sam in junior high]
  • The CS department maintains a mailing list of events and opportunities (and other things of potential interest).
  • Reminder: Starting next week, we will have mentor sessions on Wednesday and Thursday evenings from 8:00-9:00 p.m. Wednesdays will be more Q&A, Thursdays will include sample quizzes.
  • Reminder: We have tutors available Sunday through Thursday evening from 7-10 p.m. in Science 3813/15.
  • Friday PSA.

Upcoming Work

Extra credit (Academic/Artistic)

  • One of the Camille A Brown events
    • Dialogue/Conversation: Arts & Activism. Friday January 27 4:15-5:15PM, BCA 152
    • Master Class (Open to All): A Journey through Juba and Other Social Dances. Saturday January 28, 11-12:30PM, Bucksbaum Dance Studio
    • Performance: Black Girl A Linguistics Play. Saturday January 28, 7:30PM Roberts Theatre.
  • CS Table, Tuesday, noon, middle PDR: Planning for the semester.
  • Thursday extras, Thursday, 4:15 p.m., Science 3821: LaTeX

Extra credit (Peer)

  • Open practice for Ritalin Test Squad, 2-4 Saturday in the Wall.
  • Swim meet next weekend.

Good things to do

  • Moderation.

Types

  • If you reflect on the first algorithms we wrote, you’ll note that we worked with different kinds of things (e.g., knives, jars, bags).
  • While there are some commonalities between what you do with things (e.g., you can drop a knife or jar or bag on the floor), different things often imply different operations (e.g., you can unscrew a jar, but not a knife).
  • Computer scientists use the term type to refer to the things/values we work with.
  • In order to design algorithms, we need to know what kinds of operations the computer already knows. Typically, these operations are associated with types. For example, you can multiply numbers and concatenate strings. (You can’t do much at all with symbols, other than make them and compare them.)
  • As you may have noted in your first experiments with Scheme, Scheme assigns types to values.
  • For example, a value might be a number, or a string, or a procedure, or ….
  • Computer scientists often think of types in two different ways:
    • Data-driven: A type is a set of values.
    • Purpose-driven: A type provides information on the valid operations that may be applied to a piece of data.
  • We will alternate between the two definitions.
  • Many languages (particularly the ones you’ve reported being familiar with) require you to assign a type to a variable when you declare that variable.
  • Scheme does not require you to assign types ot variables; it checks the type of each operand when it executes a procedure.
    • Scheme also provides procedures that let you determine the type of a value.
  • As the semester progresses, you will learn new types.
  • As you learn each type, you’ll learn a variety of things (that correspond, in some sense, to those two approaches):
    • How to express values in the type. For example, we express string values by surrounding them with double-quotation-marks and we express numbers in much the way we always have.
    • What operations are possible on values in the type. For example, we can use the addition operation (+) on numbers and we can use the string-append operation on strings.
  • Each time we encounter a type, I’ll either tell you or ask you (or both) how you might express values and what obvious operations there are.

Lab

Scheme’s Numeric Types

  • Instead of a general “numbers” type, Scheme provides a variety of kinds of numbers.
  • Integers are numbers without a fractional component.
  • Rational numbers can be expressed as the ratio of two integers.
  • Real numbers appear somewhere on the number line.
    • In mathematics, real numbers can be rational or irrational.
    • In Scheme, real numbers are all rational.
  • Complex numbers may include an imaginary component.
  • You can (almost) always use an integer when a real is expected, but you cannot always use a real when an integer is expected.
  • Scheme also represents some numbers exactly and some numbers inexactly. (That is, it approximates some numbers.)
    • It certainly has to approximate irrational numbers.
    • But it also approximates many other numbers.
    • It may surprise you to see which numbers are represented inexactly. (We’ll return to this issue later.)
  • Some important numeric predicates (procedures that return true or false): number?, real?, integer?, exact?, and inexact?.

Modulo

  • The mod (modulo, modulus) operation is one of the trickier operations we use in this class (and we use it a lot).
  • mod is like remainder except that it always gives positive values, even when the dividend is negative.
    • For negative dividend, (mod dividend divisor) is (+ divisor (quotient dividend divisor)).
  • Essentially, mod is used to break up the number line into even chunks.
    • If you mod by 7, you break the number line up into chunks of size 7.
    • If you mod by 23, you break the number line up into chunks of size 23.
  • For each chunk, we start counting at 0.
  • For example
Number line:  -9 -8|-7 -6 -5 -4 -3 -2 -1| 0  1  2  3  4  5  6| 7  8  9 10 11 
Modulo 7:      5  6| 0  1  2  3  4  5  6| 0  1  2  3  4  5  6| 0  1  2  3  4 
  • The (modulo *i* *n*) operation allows us to cycle through the numbers between 0 and *n*-1.