AP®︎/College Computer Science Principles
Mathematical procedures and constants
With just the basic arithmetic operations (+, -, *, /), we can write programs to compute anything that a simple calculator can compute.
What about equaling the power of a scientific calculator, with the ability to calculate sines, exponents, and logs? Programming languages often provide built-in procedures and constants for such advanced mathematical functionality.
Let's start with the simplest example: calculating the absolute value of a number.
var abs1 = Math.abs(-5); println(Math.abs(-5));
We pass a single parameter to the command, the number to process, and the command returns the absolute value of that number. We can either store that result in a variable or we can display it to the screen.
Of course, it's not very exciting that we can calculate the absolute value of negative 5: we already know that it's 5. What's exciting is that we can pass in any expression to that procedure, and it will calculate the result.
var result = Math.abs( (33 * 1/2) - (40 * 0.75) );
The result is 13.5, and I sure am happy that I asked the computer to calculate that one for me.
|Returns the minimum of the given numbers.|
|Returns the maximum of the given numbers.|
|Returns the largest integer less than or equal to |
|Returns the smallest integer greater than or equal to |
|Returns the sine of |
|Returns the cosine of |
|Returns the tangent of |
✏️ Check them all out in the program below and try plugging in different numbers. You can also look up other available procedures from the Math documentation and try them out here.
The field of math includes a number of special numbers. The most famous is probably , since it pops up in all things circular. If you've memorized the first dozen digits of , then you can go ahead and type that in your programs... For the rest of us mere mortals, we can rely on programming languages to memorize those digits for us.
Math.PI, a variable that's already defined in every JS program.
For example, this expression calculates the circumference of a circle with radius 12:
var circumference = 2 * Math.PI * 12;
Math.PIvariable is a constant, which means it can't be re-assigned. We can trust our math more when we know that will always be .
✏️The program below uses
Math.pow()to help us answer the age old question: what pizza size should you buy? Change it to reflect your local pizza joint's prices and see how the results change.
Math in pseudocode
In pseudocode, you will likely see a built-in mathematical procedure described like this:
|Returns the absolute value of |
That pseudocode can be used like this:
abs1 ← ABS(-5) DISPLAY( ABS(-5) )
In pseudocode, constants are written in all caps, like the constant
An expression to calculate circumference could be written like this:
circumference ← 2 * PI * radius
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- From the author:Ah, I was trying to show that the results from calling procedures can be used in multiple ways, but it does indeed look like I'm just ignoring good practices by repeating the expression. I'll try to break that up into separate blocks!(7 votes)
- In the article, the function explaining how to calculate an absolute is written like this:
var abs1 = Math.abs(-5);
Can't it be written like this instead?
var abs1 = Math.abs(-5);
- Yes, both ways are correct. However, in the article, they wrote it out so that it was more understandable by first-time programmers.(2 votes)
- What does num means and why do the computer provide it?(2 votes)
numis just the name of a parameter, which indicates that this function takes an input of type
- I don't see the ← symbol on my keyboard.How can I type the pseudocode without that symbol?(2 votes)
- If you're on windows you can use alt keys
For ← you type Alt and 27.
If you're on linux you can use AltGr
For ← you type AltGr and y(1 vote)
- What does the abs1 mean in
abs1 ← ABS(-5)
DISPLAY( ABS(-5) )(1 vote)
- abs1 is simply the variable name. You could use whatever name you want, although you should use a name that reminds you of what it's used for.(2 votes)
- In the program, why are all the procedures repeated twice?(1 vote)
- If you are referring to repetition such as:
Then it is important to understand that two different actions are happening here. In the first
printlnstatement, a string literal ("Math.abs(-32):") is being printed. In the second
printlnstatement, the Math.abs() function is actually being called to show the result of the function call.
The purpose of doing it this way is to add clarity to the example. Without printing the string literal, you would simply see a succession of numbers, which may be unhelpful.(2 votes)
- Is it just me or the equivalent Python code of Mathematical Procedures is linked to the wrong webpage?
This is the correct link: https://repl.it/@PamelaFox1/MathematicalProcedures#main.py(1 vote)
- how do I command the parameter so as to see the result(1 vote)
- It depends, you can save the result first inside a variable, then display the value of the variable using println
var absl = Math.abs(-10);
or you can just utilize a println procedure and directly write the operation
both ways output result