How do calculators know the answers?

811 views

How does a calculator get programmed or how does it always know the right answer? I’m assuming elves but I could be wrong.

In: Technology

5 Answers

Anonymous 0 Comments

You’re asking how a calculator knows the square root of 17, or calculates functions like `sin`, `cos` and `log`.

A calculator is a computer. It contains a software program that calculates these functions.

So the question is, how does the software program work?

For the case of square roots, you can use a simple algorithm called *bisection* to refine a guess. Let’s say we want to find the square root of `1234`. It’s a 4-digit number, and `100^2` is five digits, so you know the answer is somewhere between `0` and `100`.

It’s the classic “guess-the-number” game, where the winning strategy is to guess halfway in between your last too-small and too-big guess:

– Guess it might be `50`. Since `50^2 = 2500`, our guess was too big. It must be somewhere in the range `0-50`.
– Guess it might be `25`. Since `25^2 = 625`, our guess was too small. It must be somewhere in the range `25-50`.
– Guess it might be `37` (the midpoint between 25-50, rounded down). Since `37^2 = 1369`, our guess was too big. It must be somewhere in the range `25-37`.
– Guess it might be `31` (the midpoint between 25-37). Since `31^2 = 961`, our guess was too small. It must be somewhere in the range `31-37`.
– Guess it might be `34` (the midpoint between `31-37). Since `34^2 = `1156`, our guess was too small. It must be somewhere in the range `34-37`.
– Guess it might be `35` (the midpoint between 34-37, rounded down). Since `35^2 = 1225`, our guess was too small. It must be somewhere in the range `35-37`.
– Guess it might be `36` (the midpoint between 35-37). Since `36^2 = 1296`, our guess was too big. It must be somewhere in the range `36-37`.

We rounded where necessary to keep everything integer math, but you could keep going with decimals.

Bisection is a “generic” method that works with a lot of different functions.

You could also have a method based on what, in modern terminology, is an *attracting fixed point*. Let’s say you guess the square root of `1234` is `x`.

What happens if you take the average of your guess and `1234 / x`?

If you guessed right, then the average `(x + 1234/x) / 2` gives you your original guess back, because the first term is `sqrt(1234)` and the second term is `sqrt(1234)`, so the part in parenthesis is two times `sqrt(1234)`, and dividing by 2 gives you back `sqrt(1234)`.

Here’s the strategy: *Use the average as your next guess*.

– Our first guess is `100`. The average of `100` and `1234 / 100` is `56` (rounded).
– Our second guess is `56`. The average of `56` and `1234 / 56` is `39` (rounded).
– Our third guess is `39`. The average of `39` and `1234 / 39` is `35` (rounded).
– Our fourth guess is `35`. The average of `35` and `1234 / 39` is `35` (rounded).

Since our guess stopped changing, we take the last value as the answer: The square root of `1234` is about `35`. (A calculator will give you more precision, because it’s easy for a computer to do the above calculations with 9-12 decimals or so).

The ancient Babylonians knew how to calculate square roots this way thousands of years ago.

Later Sir Isaac Newton showed how it’s related to the shape of a curve, and how to turn the Babylonian method for square roots (that’s hard to adapt to other functions) into a *generic* approximation method that can be used with many different functions (Newton’s method).

If you take calculus, you’ll study two basic function approximation methods: Newton’s method and Taylor series.

There are many more, some for specific functions or even constants like pi. There are others that are generic and can be applied to lots of different functions.

You can also combine them, for example, for sine and cosine you can use Taylor series to find exact values for a small number of angles. You could store these answers in a lookup table in the calculator software, and use angle addition formulas to combine them to get the value. (For example if you store cosine and sine values for 1, 2, 4, 8, 16, 32, 64, 128, 256 degrees you could find any integer degrees.)

There’s also an entire fascinating history of people who calculated these functions for many values and published tables of function values in books. For hundreds of years, from the 1600’s to the early 1900’s, a table of logarithms was essential reference for any serious scientist. Of course nowadays, they’ve been completely replaced by calculators and computers.

You are viewing 1 out of 5 answers, click here to view all answers.