Chapter 3
Calculations

You’ve done some basic math already, but now it’s time to dive into more complex math. The exercises in this chapter are a little more challenging. You’ll work with formulas for numerical conversion and you’ll create some real-world financial programs, too.

These programs will test your knowledge of the order of operations. “Please Excuse My Dear Aunt Sally,” or PEMDAS, is a common way to remember the order of operations:

  • Parentheses

  • Exponents

  • Multiplication

  • Division

  • Addition

  • Subtraction

The computer will always follow these rules, even if you don’t want it to. So the exercises in this chapter will have you thinking about adding parentheses to your programs to ensure the output comes out correctly.

You’ll want to make good use of test plans for these exercises, too, because you’re going to be dealing with precision issues. If you work with decimal numbers in many programming languages, you may encounter some interesting, and unexpected, results. For example, if you add 0.1 and 0.2 in Ruby, you’ll get this:

 
> 0.1 + 0.2
 
=> 0.30000000000000004

This happens in JavaScript too. And multiplication can make things even more interesting. Look at this code:

 
> 1.25 * 0.055
 
=> 0.06875

Should that answer be rounded down to 0.06 or up to 0.07? It depends entirely on your business rules. If your answer must be a whole number, you may have to round it up.

Things get even messier with currency. One of the most common issues new programmers face occurs when they try to use floating-point numbers for currency. This will result in precision errors.

One common approach is to represent money using whole numbers. So instead of working with 1.25, work with 125. Do the math, and then shift the decimal back when finished. Here’s an example, again in Ruby:

 
> cents = 1.25 * 100.0
 
=> 125.0
 
> tax = cents * 0.055
 
=> 6.875
 
> tax = tax.round / 100.0
 
=> 0.07

You may need to be a lot more precise than this. These floating-point precision issues exist in many programming languages, and so there are libraries that make working with currency much better. For example, Java has the BigDecimal data type that even lets you specify what type of “banker’s rounding” you need to do. When you’re working on these problems, think carefully about how you need to handle precision. When you do problems for real, especially if it’s some kind of financial work, learn how the business you’re working with rounds numbers.

One last thing before you dive in: the exercises in this chapter might seem to get a little repetitive toward the end if you’re experienced. But for beginners, repetition builds up confidence quickly. It’s the same reason you do practice drills in sports or practice your scales over and over in music. By doing several similar problems, you build up your problem-solving skills and improve your speed at breaking down problems. And that translates into success on the job.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.21.138