Lesson goal: Learning about built in math by taking absolute values

Previous: Literal vs. to-be text | Home | Next: Using Pi on the computer

Programming languages often have built in mathematical functions, that do much more than add, subtract, multiply and divide. In this lesson, we'll look at the absolute value function.

If you recall, the absolute value of a number is the number "made positive" by dropping the minus sign if the number if negative and just letting the number pass if it's positive. So for example, the absolute value of -3 is 3 and the the absolute value of 3 is 3. In math sense, this would be written as $|-3|=3$ or $|3|=3$, where the two vertical bars around a number mean absolute value. Absolute values can also be taken of entire expressions like $|5\times 2-15|$, which would come out to be 5 (not -5).

Here is how the computer will compute an absolute value:

Move the mouse over a dotted box for more information.

Notice the abs in the name, which is short for "ABSolute value." The prefix of math. means that abs is included in the "math" library that the computer knows about. The "something.something" notation is very common in computer programming.

Now you try. Use the print statement to find the absolute value of some positive and negative numbers.

Type your code here:

See your results here:

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.