Main content
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.
Mathematical procedures
Let's start with the simplest example: calculating the absolute value of a number.
In JavaScript, we can use the built-in procedure
Math.abs()
: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.
The JavaScript language provides 36 built-in math procedures. Here are some you're likely to use in your own programs:
Procedure | Description |
---|---|
Math.pow(base, exp) | Returns base to the power of exp . |
Math.min(x, y, z…) | Returns the minimum of the given numbers. |
Math.max(x, y, z…) | Returns the maximum of the given numbers. |
Math.round(num) | Returns num rounded to the nearest integer. |
Math.floor(num) | Returns the largest integer less than or equal to num . |
Math.ceil(num) | Returns the smallest integer greater than or equal to num . |
Math.sin(num) | Returns the sine of num , an angle specified in radians. |
Math.cos(num) | Returns the cosine of num , an angle specified in radians. |
Math.tan(num) | Returns the tangent of num , an angle specified in radians. |
✏️ 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.
Math constants
The field of math includes a number of special numbers. The most famous is probably pi, since it pops up in all things circular. If you've memorized the first dozen digits of pi, 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.
In JavaScript, we can get an approximate value of pi by referencing
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;
The
Math.PI
variable is a constant, which means it can't be re-assigned. We can trust our math more when we know that pi will always be pi.You can learn about JavaScript's other math constants in the Math reference. Most languages will provide similar constants, so if you're in another language, you can search online for "Pi in [programming language]".
✏️The program below uses
Math.PI
and 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:
Pseudocode | Description |
---|---|
ABS(num) | Returns the absolute value of num |
That pseudocode can be used like this:
abs1 ← ABS(-5)
DISPLAY( ABS(-5) )
In pseudocode, constants are written in all caps, like the constant
PI
.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?
- For the first javascript example, and the last pseudocode example, in the print/display statament, the actual expression was written. Can't you just write the variable name to print instead of the whole expression?(5 votes)
- 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);
println(Math.abs(-5));
Can't it be written like this instead?var abs1 = Math.abs(-5);
println(abs1);(3 votes)- 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)
num
is just the name of a parameter, which indicates that this function takes an input of typenumber
(1 vote)
- 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:
println("Math.abs(-32):");
println(Math.abs(-32));
Then it is important to understand that two different actions are happening here. In the firstprintln
statement, a string literal ("Math.abs(-32):") is being printed. In the secondprintln
statement, 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);
println(absl);
or you can just utilize a println procedure and directly write the operation
println(Math.abs(-10));
both ways output result
10(1 vote)