How do calculators know the answers?

740 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

[removed]

Anonymous 0 Comments

To keep it ELI5, tiny computer take input from buttons and add the binary inputs using a logic gate.

Anonymous 0 Comments

The calculator is a little computer. It does the calculations a lot like how you’d do them on paper (but in binary because it’s a computer). And it does them a lot faster (because it’s a computer).

You know how they taught you in school? To add 1234 and 5678, you do 4+8 which is 12, so write down 2 and carry 1, do 3+7+1 which is 11, so write down 1 and carry 1… etc? The calculator does that (but in binary and at the speed of electronics)

Anonymous 0 Comments

They’re built up from a bunch of simple components called logic gates, which are these sort of conditional light-switch circuits which can output either a high or a low voltage, depending on whether their inputs are receiving a high or low voltage. There are a few kinds of logic gates, but the simplest ones people usually learn about when they’re learning electronics, are the AND, OR, and NOT gates, which do pretty much exactly what their names suggest.

An AND gate with 2 inputs, will output a high voltage only if both of its inputs are receiving a high voltage. An OR gate will output a high voltage if either one of the inputs are receiving a high voltage. A NOT gate has only 1 input and will output the opposite of whatever input it’s receiving.

By wiring up a bunch of these gates’ inputs and outputs to each other in clever ways, you can get them to perform all kinds of functions.

You can buy integrated microchips which have a bunch of these gates pre-wired up so that they can do arithmetic tasks like adding, subtracting, etc. Most cheap calculators are made using these chips.

eta: if you’re interested in the details of *how* gates can be put together to form arithmetic circuits, [here](https://www.youtube.com/watch?v=m0C3-JWWvcc) is a brief explanation of one very common design pattern called an “adder.”

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.