Code mystery (4)-floating point number (from surprise to thinking)

Source: Internet
Author: User

In the first several articles in the "Code Mystery" series, floating point numbers have appeared many times. Floating Point Numbers are called simple data types in many programming languages. In fact, floating point numbers are not simple compared to complex data types (such as strings.

It is just a description of the IEEE floating point number to write a book. I will use a few blog posts to briefly talk about the floating point number I understand.

One interview

I remember an interview on floating point number, binary division, and coding when I hired a Java programmer many years ago. After so many years, he has become a very good programmer. He will tell me at each Party that "the interview completely changed my previous learning methods.I just blindly accept the knowledge and have never thought about it myself.In that conversation, I learned more than I did in my university for four years ".

I have read his resume and read "Information Theory" to talk about many topics about binary classification and coding. The entire process is about three hours-the longest time I have been interviewing.

For a long time, I sorted out some floating point-number content that I can recall in this blog.

Format description:

All my words are included in references. His words are enclosed in quotation marks. My psychological activities or descriptions are not enclosed in quotation marks.

Number of Floating Point Numbers

On an 8-bit computer, how many floating point numbers are there?

"The 8-bit data seems too outdated. Currently, the mainstream data is 32-bit. It seems that it can represent 3x10 ^ 38 ."

As expected,Many graduates have developed computer science into liberal arts. They are not learning theoretical knowledge, but accepting/reciting this knowledge..

How many integers can an eight-bit computer represent?

"This is simple. The power of 2 is 256. An integer represented by an N-bit computer is the N power of 2 ."

He was very excited when he answered the question, because he could finally refute my point of view. He didn't regard computer as a hard-to-remember discipline.

Eight computers, or8 bits can represent 2 ^ 8 Integers. How many characters can this 8-bit character represent?

"Oh, of course, it is also the 8th power of 2, otherwise there is no need to invent a 16-bit or 32-bit Unicode to represent Chinese characters ."

As you mentioned earlier, 8 bits can represent 3 ^ 38 floating point numbers. How many floating point numbers can be expressed by 2bit?

"Since 2 bits can represent four integers, there must be more floating-point numbers than this. At least it must be able to represent more than 10 floating-point numbers ."

Well, I 'd like to talk about a few numbers based on your ideas.

  • 0 should always be, expressed as 00.
  • 1 In 01 format
  • 2 In 10 format
  • 3, represented by 11

Now, how do you express 0.4 to me?

"He thought for a moment" "Oh. I understand,2bit can represent four numbers, whether it is an integer, a decimal number, or a characterEven if we use 2bit to represent an apple, we can only represent four. If we want to represent more, we need to use more bits ."

Although he has read information theory in his resumeThe amount of information that n bit can represent is 2 ^ nCertainly not fully understood, or just passively accepted this theorem.

After a while, he continued: "According to this logic, 8 bits can only represent 256 floating point numbers, which is too small. I'm a little confused. The floating point number generally has tens of thousands or even hundreds of millions ."

Floating point Precision

So I wrote several lines of code in firebug (the code can be found in the preface of the first article in this series ).

0.2 + 0.40.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1

"How is this possible? Isn't JS so strict ?"

Apparently, he attributed this phenomenon to JS (fortunately, he did not add his sins to firebug ). So I used Java to rewrite the above Code. This time, he was stunned.

Now that he has startedSurprised, The next step isThoughts. I explained a little more:

Any language claims that their floating point number ranges from 3x10 ^ 38. How big is this number? Currently, we know that the age of the universe is 1.373x10 ^ 10 years.

However, 32bit can only represent 2 ^ 32 numbers at most, which is about 4x10 ^ 9.

By comparison, you will find amazing results. If the range of a floating point number is greater than that of the earth, the floating point number that can be accurately expressed is not as big as a sesame.

"In this case, 0.2 + 0.4 is calculated incorrectly because it cannot be accurately expressed. How can this problem be avoided in programming ?"

UseNumber of points indicates decimal places.

Is floating point number equivalent to decimal number?

"Isn't the number of fixed points an integer? How does a fixed point represent decimal places ?"

Obviously, there is a theoretical misunderstanding. He does not really understand what is a fixed point or a floating point.

Can floating-point numbers represent integers? For example, can float a = 2 be used?

"Yes, yes. What is 2.0 stored in the computer ?"

The computer certainly does not store 2.0. 1% million. The computer stores 0 and 1 strings. Haha.

"I think the floating point number should not store 2 of the integer. It should store 2.0 of the decimal number and convert it to 0 or 1. Is that true ?"

He asked me several questions in a row, and I felt that I was not interviewing, But attending classes.

Integers and decimals are concepts in mathematics. in computers, there are only fixed points and floating points, and there are no integers or decimals..

How do I define the number of points in the textbook?

"Forget, only know that the number of points is an integer, and the floating point is a decimal. It seems that the teacher said the same thing ."

That's because your teacher is not me. If I were a teacher, I would never teach students that way. Smile 』

What does "point" mean by a fixed point or floating point? "Point" indicates the decimal point. Fix the decimal point, usually on the rightmost side, that is, the number of points. Float the decimal point. Where is the floating point? This is defined in the IEEE floating point number standard.

Back to the previous topic, how can we accurately represent decimal places? One solution is to set the number of points.

Take 8bit for example. We can place the decimal point in the middle and use 4 bits to represent the integer part and 4 bits to represent the decimal part. This construction method (professional points we call it a data structure. Generally, the language calls integers and decimals a simple data type. In fact, they are not simple at all, and more complex than strings that have become Composite data types), we can accurately represent 256 decimal places.

In the next chapter, we will construct an 8-bit floating point representation to explore the secrets unknown to floating point numbers. I call it JJFN-134 (justjavac float notation, justjavac floating point notation), 1bit symbol, 3bit index, 4bit ending number.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.