The Science of Programming/A Little of This, A Little of That
In the last chapter, you were introduced to the idea of a variable. A variable is a spot in the computer's memory where you can store a value:
sway> var x = 1000000; INTEGER: 1000000 sway> x; INTEGER: 1000000
Here, I've named that spot x and I've stored the number 1000000 at that spot. Once, I've reserved that spot, I can change the value there by assigning a new value to the variable. Here's an example:
sway> x = 42; INTEGER: 42 sway> x; INTEGER: 42
Notice that I did not use the word var this time. When I use var, it means reserve me a new spot. When I don't use var, it means update a previously reserved spot.
To learn more about assignment, please see Sway assignment.
What happens if we try to update a spot without reserving it first?
sway> y = 3; EVALUATION ERROR: :assignError stdin,line 3: assignment: variable y is undefined
We get a message that y is undefined. This is the interpreter's way of telling us that we forgot to reserve a spot for y using var. Sway knows we didn't define the variable because it looked for it in the current environment and, failing, in the context environment and, failing, in the context of the context, and so, until it ran out of places to look.
The word variable implies that the value can vary, which is exactly what we did with x; we changed its value. In some programming languages, there are analogous beasties named constants, only once created, you cannot vary them. In Sway, there are no constants, per se. Should we need a constant, we will use a variable instead, but we will make a pledge not to change a variable's value. To remind us that we should not change its value, we will use capital letters (mostly) in naming the variable:
sway> var PI = 3.14159; REAL_NUMBER: 3.1415900000
Making this pledge is known as following a convention. Note that variable names can be (and generally should be) more than a single letter. Note also that SPT in Chapter 3 of CME uses lower case letters early in the alphabet to indicate constants and lower case letters late in the alphabet to indicate variables. That is a different convention.
Adding a little bit
editHere's a problem that is a little bit simpler than the one from CME[1]:
+ + + + + + + + + + + + + + + + + + + + + + h = w/2 + + + + + + + + + + + + + + + + + + + + w
Consider a rectangle whose width and height are related. Suppose we increase the width a little bit. What happens to the height, assuming the new height is still one half of the new width? Since the height is calculated from the width, we say that width is the independent variable and that height is the dependent variable. First, let's define a function that gives us the height, h, given the width, w:
function h(w) { w / 2.0; }
Note that when we write functions like this, it is almost always the case that the dependent variable is the name of the function and the independent variable is the name of the formal parameter. Use this rule to help you define the functions you need.
We can test out our new function:
sway> h(100); REAL_NUMBER: 50.0000000
This confirms our suspicion that if the width is 100 units, the height must be 50 units.
Calculus is often concerned with the change in the dependent variable given a change in the independent variable, particularly the ratio of the two changes.
Now, let's figure out that ratio for the above rectangle, using a small change in width:
sway> var w = 100; //width sway> var dw = 0.01; //change in width sway> var dh = h(w + dw) - h(w); //change in height
The change in the height is simply the new height minus the old height. Now we can compute the ratio:
sway> dh / dw; REAL_NUMBER: 0.5000000000
Another thing Calculus is concerned with is does this ratio change with different amounts of change or different values for the independent variable? The lazy person's way to answer these questions is, you guessed it, to write a function to do the work.
function ratio(w,dw) { var dh = h(w + dw) - h(w); dh / dw; }
Note that we pass in the values of w and dw and then compute the ratio. This allows us to try a wide variety of values quite easily:
sway> ratio(40,0.01); REAL_NUMBER: 0.5000000000 sway> ratio(40,0.0001); REAL_NUMBER: 0.5000000000 sway> ratio(20,0.0001); REAL_NUMBER: 0.5000000000
This ratio function is quite useful! We see that the ratio of does not seem to ever change. But suppose we now start looking at rectangles with a different relationship between width and height. All of a sudden, our ratio function is looking a lot less useful. This is because we hard-wired the height function.[2] We can fix this by passing in the height function as a third argument. Now we can easily try out new height functions. By doing so, we have made the ratio computing function more general.
function ratio(w,dw,h) { var dh = h(w + dw) - h(w); dh / dw; }
It should work just the same:
sway> ratio(40,0.01,h); REAL_NUMBER: 0.5000000000 sway> ratio(40,0.0001,h); REAL_NUMBER: 0.5000000000 sway> ratio(20,0.0001,h); REAL_NUMBER: 0.5000000000
Let's try a different height function:
function g(w) { w * w; }
With this height function, the height g, grows as the square of the width. What does ratio produce now? Let's try one width and ever smaller changes:
sway> ratio(100,1,g); INTEGER: 201 sway> ratio(100,0.1,g); REAL_NUMBER: 200.10000000 sway> ratio(100,0.01,g); REAL_NUMBER: 200.01000000 sway> ratio(100,0.001,g); REAL_NUMBER: 200.00100000
It's pretty clear that if the change in width becomes infinitely small, the ratio when the width is 100 will be 200.
Now let's try a different width and ever smaller changes:
sway> ratio(200,1,g); INTEGER: 401 sway> ratio(200,0.1,g); REAL_NUMBER: 400.10000000 sway> ratio(200,0.01,g); REAL_NUMBER: 400.01000000 sway> ratio(200,0.001,g); REAL_NUMBER: 400.00100000 sway> ratio(200,0.0001,g); REAL_NUMBER: 400.00009998
For this new width, 200, the ratio appears to converge on 400. Also, for the last test, we got a value that was a little unexpected. This should be a warning to you when dealing with real numbers.[3]
For the height function g, we see that different starting widths yield different ratios. A good deal of calculus is for explaining why such ratios change. Also, this process of computing ratios is known as the taking the derivative.
Questions
editAll formulas are written using Grammar School Precedence.
1. Does it matter what variable name we use as a formal parameter when defining a function?
2. Draw a picture of a labelled rectangle so that the height is the independent variable, keeping the relationship that the height is one half the width.
3. In the function ratio, dh is computed from . If we assume that ) is equivalent to ) then dh is or simply . Define a new function named ratio2 to test whether this assumption is valid.
4. Which function is better for very small changes? Why?
5. Consider a rectangle whose area never varies. That is, if the width is increased, the height decreases to compensate so that the area of the rectangle remains the same. Define a function that computes the height of this rectangle given the width and the area.
6. Do we observe the same behavior in the ratios as we did with function g?
7. "Change is constant," but does not have to be a constant. Most of us are comfortable with the notion of a constant rate of change. For instance the change in height versus distance as described by a straight line, for instance . Use the ratio function to explore how y changes as x changes. Produce a table of y, x, and dy/dx.
8. The previous problem had a constant rate of change. Often, however, the rate of change of the dependent variable changes with the independent variable. Let's take as an example the function for area of a square, . First of all, what is the change in the area as x changes from 2 to 4? Draw a sketch of this situation, labeling dy and dx. Do the same thing as x changes from 2 to 3, 2 to 2.5, 2 to 2.1, and 2 to 2.01. What do you observe? Try the same thing, but now start with x = 4. The point is that the rate of change in the area, itself changes depending on the length. The function of x that expresses this relationship between the change in y and x is called the derivative, dy/dx. Based on your experiments, what is the derivative of ?
9. The rate of damage reduction from armor is where . What is the relative growing of damage reduction with Armor? With EnemyLevel?
Footnotes
edit- ↑ This figure is drawn using Cheesy ASCII (tm) Graphics (for fast loading).
- ↑ We hard-wire something when we treat it as a constant, something that doesn't change.
- ↑ You can trust integers, because they are exact. But you should never trust real numbers completely, because they are approximations. Sway real numbers are only precise to 15 significant digits.