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);
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 $\pi $ will always be $\pi $ .

`Math.PI`

variable is a **constant**, which means it can't be re-assigned. We can trust our math more when we know thatYou 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!(15 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);(5 votes)- Yes, both ways are correct. However, in the article, they wrote it out so that it was more understandable by first-time programmers.(5 votes)

- What does num means and why do the computer provide it?(3 votes)
`num`

is just the name of a parameter, which indicates that this function takes an input of type`number`

(6 votes)

- In the program, why are all the procedures repeated twice?(2 votes)
- 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 first`println`

statement, a string literal ("Math.abs(-32):") is being printed. In the second`println`

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.(7 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(3 votes) - 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(2 votes)

- Does khan academy have the constant PHI?(2 votes)
- 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)

- Forky asks a question(0 votes)
- Quoted from above-"In JavaScript, we can use the built-in procedure Math.abs():

var abs1 = Math.abs(-5);

println(Math.abs(-5));"

Aren't these the only ways to print in Javascript?

Using the console.log() function

Using the windows.alert() function

Using the innerHTML property

Using the document.write() function(0 votes)