One of the hardest things about programming is translating a description of a task, to an algorithm, to a program — a patcher, in the case of Max. First, let’s make sure we understand each part of the above statement. I’ll use the Fahrenheit to Celsius converter problem from the last class.
Description
A description is just a normal language-based presentation of what you want the program to do. Often, this is the way the assignment is presented to you.
Create a patcher that converts Fahrenheit to Celsius, and Celsius to Fahrenheit. There should be two streams of computation: one that allows you to type in a Fahrenheit temp and get the converted output in Celsius, and the other that does the reverse. Label input and output for each with comments. The output temperature should be in floating point form.
(F – 32) divided by 9/5 = C, or in simpler mathematical terms,
(F – 32) multiplied by 5/9 = C
Algorithm
An algorithm is a set of steps to follow in solving a problem or performing a task. In short, it’s a recipe. We use the term a lot to describe computer processes. You can think of an algorithm as translation of the problem/task into a step-by-step outline of the required actions. Algorithms often use a pseudo-computer language appropriate to the programming language you’re using. With max, your algorithm will often include max objects in the steps. It is important to understand that computers only do one thing at a time, so any computer program must be broken down into single steps or actions.
An algorithm of the above description will look something like this:
- Input Fahrenheit temperature as number (number object)
- Subtract 32 (- object) from output of number object
- Take output of subtraction and multiply it by 5 (* object)
- Take output of multiply and divide it by 9 (/ object)
- Take output of divide and display in float number object
Patcher
It is pretty easy to translate the above algorithm to a Max patcher. It is important to note that you have to test your program for any problems. The main changes are the math objects containing arguments. Here is a first attempt, with a Fahrenheit temp input:
Notice that the input temp of 90 equals 32. The answer should be 32.22…. So what can we do? Let’s trace our steps, keeping in mind how computer languages perform math operations.
Up until the divide object, the output of all the operations are whole numbers, nothing fractional or extending past the decimal place. The problem is probably obvious to you after having worked through the division. The output of the division object is not a whole number (integer), but rather a floating-point number. But you need to tell Max to use floating-point math when calculating to get floating point output. The final floating-point number box does not make up for the previous integer math.
The solution is to perform all of your math calculations with floating-point precision by specifying floating-point arguments to the math objects.
Here is a floating point version of the patcher:
I made the first integer number object a floating-point object. Then I made each math object operate at floating-point precision by adding a decimal at the end of the argument. The / object has an argument of 9., instead of 9, which tells Max to perform the division operation as a floating-point operation with more precision.
Leave a Reply