If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

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);
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:
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.
📝 See equivalent code in: App Lab | Snap | Python

Math constants

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.
In JavaScript, we can get an approximate value of π 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 π will always be π.
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.
📝 See equivalent code in: App Lab | Snap | Python

Math in pseudocode

In pseudocode, you will likely see a built-in mathematical procedure described like this:
ABS(num)Returns the absolute value of num
That pseudocode can be used like this:
abs1 ← 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?