# How are irrational numbers best represented and processed by computers?

My question is closely related to this one: How do computers understand decimal numbers?

However, that question deals with rational numbers only. I was wondering if irrational numbers can be represented by computers.

Irrational numbers are numbers with non-repeating and non-terminating decimal expansion. They have an infinite non-repeating sequence of numbers after the decimal point.

A common example where irrational numbers are seen is when calculating the area of a circle. The area of a circle will be an irrational number in most cases.

If the decimal expansion of an irrational number is shortened then the accuracy would reduce. As a simple example, the natural logarithm of e is 1, while the natural log of 2.72 or any approximation of e, is not 1 but a number a close to 1. If I'm writing a program using floating point arithmetic and the computer uses this approximation of e it will lead to errors.

Is it possible to avoid such errors and error propagation? Computations in science and engineering often involve irrational numbers. If this error propagates the final result will be very far from the correct one.

• I see you're writing and answering questions, which is a nice way to contribute, but I think you have this one on the wrong stack. This is more of a programming problem than an electrical engineering one.
– K H
Mar 28 at 8:15
• Your question is vague in a very leading manner. You need to be specific about what you mean by "process...correctly" a number. Do you mean to produce a useful numerical result or do you mean to simplify symbolic equations? Can you give an example of the kind of computation and the kind of result you are talking about? Mar 28 at 12:14
• But your example does not illustrate your point. In python there seems to be no error: >>> math.log(math.e) 1.0 Mar 28 at 17:59
• Ah, so now we are changing the rules. Why don't we start with you telling me the right answer? Mar 29 at 11:29
• print("{0:1.40f}".format(math.log(math.e))) yields 1.0000000000000000000000000000000000000000 Mar 29 at 13:27

There’s nothing special about computers in this context. An infinite number of nonrepeating decimal places cannot be stored or manipulated on a computer, on paper, or in a human brain.

Irrational numbers can be represented in a few different ways:

1. A symbol that names the number, such as e or π. A computer can use symbolic computation to work with such symbols.

2. An algorithm that describes how to compute the number. The algorithm can only be run if it can be terminated early to produce an approximation. If the algorithm is a symbolic computation (such as sqrt(2)), it may be manipulated in that form.

3. A rational approximation of the number, which can be stored using any normal method such as floating point or BCD.

EDIT: Crasic points out in a comment that "...system epsilon is usually well defined for computers. That is, from the perspective of future computation or manipulation within the computer system using standard built in numerical types, you should be able to state what is the smallest unit of numerical precision is for your computer system and can therefore terminate algorithm when your estimated remaining error is less than this known value."

Technically, you can do this by hand as well, but hand calculations are too slow to make it worthwhile.

• The only, possibly, special thing is that system epsilon is usually well defined for computers. That is, from the perspective of future computation or manipulation within the computer system using standard built in numerical types, you should be able to state what is the smallest unit of numerical precision is for your computer system and can therefore terminate algorithm when your estimated remaining error is less than this known value. Mar 29 at 14:03
• @crasic I would argue that epsilon is not defined at all for computers but only for specific representations of real numbers. If you do not place limits on computation time or available memory then I can keep using more and more bits to represent my numbers and drive epsilon to an arbitrary small value. Apr 22 at 18:40
• @ElliotAlderson this is absolutely true. However , if one restricts analysis to machine defined and hardware enabled representations and operations, and not representations built on higher order logic, then there is an intrinsic meaning for system epsilon in the context of the computer as a machine. Apr 22 at 18:51

The error is not limited to irrational numbers. As an example, the number 0.1 is represented as 0.100000001490116119384765625 in single precision floating point number (floating point online conversion).

The numbers are internally represented in binary and each bit has a value defined in powers of two (positive or negative). Additionally to that, floating points have integer (also with both signs) exponents to expand the representation range.

It is not only operations that result in errors. Even representing numbers internally have this problem. Every number (and not only irrationals) that cannot be defined in powers of two with the limited bit length of the hardware representations, will be represented with error.

If you perform iterations where the results are feed back as inputs, errors may accumulate and give you results that are worse at each step.

• On the other hand, in base three, 1/3 is simply 0.1 so there are situations where rational numbers can be expressed without loss of precision in other bases,, what you are describing is the outcome when the prime factors of the denominator are coprime to the base. Base ten has the benefit of that prime factor of 5 in addition to 2 which reduces repeated and non terminating representations of common rational numbers. But this is not the same as an irrational number which has no terminating or repeating representation in any base. Mar 29 at 14:11