Pay attention to the “water drop and silver bullet” public account, the first time to obtain high-quality technical dry goods. 7 years of senior back-end development, with a simple way to explain the technology clearly.

It takes about 3 minutes to read this article.

In real life, we often use whole numbers and decimals. Have you ever thought about how these numbers are stored in a computer?

When we study computer science, we often hear about “fixed point” and “floating point”. What is the difference between them?

Is it true that an integer is a fixed point number and a decimal is a floating point number?

In this article, we will first look at the relationship between fixed point number and integer and decimal.

What are fixed point numbers?

To understand what fixed-point numbers are, first of all, we need to understand what “fixed-point” actually means.

As we all know, numbers include both integers and decimals, and decimals are much more precise than integers, so if we want to represent both integers and decimals in a computer, the key is how do we represent this decimal point?

Agreed so people come up with a way, that is, the position of the decimal point in the computer, and the fixed location, before and after the decimal point in the figure, with binary representation, and then combine can put the number stored in the computer, this representation is called “dot notation”, in this way, said the number is called the “fixed-point number”.

In other words, “ding” refers to the fixed meaning, “point” refers to the decimal point, the fixed position of the decimal point is the origin of the name of the fixed number.

How do fixed point numbers represent numbers?

Since fixed-point numbers are just a way of representing numbers, can they represent integers? Can you represent decimals?

The answer is yes.

Fixed-point numbers can be divided into the following three cases if they want to represent integers or decimals:

  1. Pure integer: for example, the integer 100, the decimal point is in the last digit, so ignore it
  2. Pure decimal: for example, 0.123, with the decimal point fixed in the highest place
  3. Integer + decimal: for example, 1.24, 10.34, where the decimal point is in a specified position

For the first two cases, pure integers and pure decimals, because the decimal point is fixed in the lowest and highest place, so they are represented by fixed-point numbers, the principle is the same, only need to integer part, decimal part, according to the rule of decimal to binary, respectively.

And for the integer + decimal case, with a fixed point, the need to agree on the position of the decimal point, in order to express in the computer.

Fixed-point numbers represent pure integers and decimals

Hearing is better than seeing, so let’s give a few examples. So let’s see how do fixed-point numbers represent pure integers and decimals?

For the pure integer 100, since the decimal point is fixed at the lowest point, suppose we represent it as 1 byte (8 bits) and use fixed-point numbers as follows (D for decimal abbreviation, B for binary abbreviation) :

100(D) = 01100100(B)
Copy the code

For the pure decimal 0.125, since the decimal point is fixed in the highest place, it is also represented by 1 byte (8 bits), and is represented by fixed-point numbers as follows:

(D) = 0.00100000 0.125 (B)Copy the code

As can be seen from the above examples, both cases are relatively simple to represent by fixed-point numbers, and the result can be obtained according to the rule of converting decimal to binary.

Fixed-point numbers represent integers + decimals

Let’s look at fixed-point numbers how do we represent integers plus decimals?

In this case, we have to agree on where the decimal point is.

Using the same example of 1 byte (8 bits), we can agree that the first 5 bits represent the integer part and the last 3 bits represent the decimal part.

This is true for the number 1.5 in fixed-point terms:

1.5 (D) = 00001, 100 (B)Copy the code

The number 25.125 looks like this in fixed-point terms:

25.125 (D) = 11001, 001 (B)Copy the code

That’s how you write a decimal in terms of fixed point numbers. Here’s a summary of the process:

  1. With limited bit width, the position of the decimal point is first agreed
  2. The integer and fractional parts are converted to binary representations, respectively
  3. When the two binary parts are combined, they are the result

Isn’t it easy to use fixed-point numbers to represent a number in a computer?

But have you found a problem? We agreed that the first 5 digits represent the integer part and the last 3 digits represent the decimal part. At this time, the maximum binary value of the integer part can only be 11111, namely 31 in decimal, and the maximum binary value of the decimal part can only be 0.111, namely 0.875 in decimal.

What if we want to represent a larger range of values?

  1. Increase the width of a bit: for example, use 2 bytes, 4 bytes, so that the integer and fractional parts are wider, indicating a larger range
  2. Change the position of the decimal point: as the decimal point moves back, the whole range of digits increases, but the decimal part becomes less and less accurate, and there is no way to represent a value as high as 0.00001

Therefore, we find that no matter how to agree the position of the decimal point, there will be the following problems:

  • The range of values is limited (the further to the left of the decimal point, the smaller the range)
  • The accuracy range of the value is limited (the further to the right of the decimal point, the lower the accuracy of the value)

In general, it is a decimal represented by fixed-point numbers, not only the range of numerical representation is limited, but also its accuracy is very low. To solve these two problems, people proposed the use of “floating point” representation of numbers, which will be explained in the next article.

Although fixed-point numbers represent numbers, there are these problems mentioned above, but only in the context of representing decimals. It’s very handy if you just use it for integers.

Therefore, modern computers generally use fixed point numbers to represent integers.

conclusion

This article focuses on how fixed-point numbers can be used to represent numbers in computers. The summary is as follows:

  1. Fixed-point numbers are a way of representing numbers in a computer, which can be integers or decimals
  2. In fixed bit, the position of the decimal point is agreed, and then the integer part and the decimal part are converted to binary respectively, which is the result of fixed point number
  3. Limited by the position of the decimal point, the range of values and decimal precision are limited when representing decimals with fixed-point numbers
  4. In modern computers, fixed-point numbers are usually used to represent integers, and for high precision decimals, floating-point numbers are usually used

Want to read more hardcore technology articles? Focus on”Water drops and silver bullets”Public number, the first time to obtain high-quality technical dry goods. 7 years of senior back-end development, with a simple way to explain the technology clearly.