Puzzle 28: cyclist

Source: Internet
Author: User

The following puzzles and the following five puzzles have reversed the situation for you. They do not show you some code and then ask you what the code will do, they require you to write code, but the number is very small. These puzzles are called "loop )". You will see a loop, which should be terminated quickly, and your task is to write a variable declaration. when it acts on the loop, makes this loop infinite. For example, consider the following for loop:

for (int i = start; i <= start + 1; i++) {}

It seems that it should only be iterated twice, but it can be made infinitely loop through the overflow behavior shown in puzzle 26. The following statement uses this technique:

int start = Integer.MAX_VALUE - 1;

Now it's your turn. What kind of statement can turn the following loop into an infinite loop?

While (i == i + 1) {}

Check the while loop carefully. It seems that it should be terminated immediately. A number will never add 1 to itself, right? Well, what if the number is infinite? Java enforces the use of IEEE 754 Floating Point Number Arithmetic Operations [IEEE 754], which allows you to use a double or float to represent infinity. As we learned in school, the infinite plus 1 is still infinite. If I is initialized to infinity before the loop starts, the terminate condition test (I = I + 1) is calculated as true, so that the loop will never end.

You can use any floating-point arithmetic expression that is computed as infinity to initialize I. For example:

double i = 1.0 / 0.0;

However, you 'd better use the constants provided by the standard class Library:

double i = Double.POSITIVE_INFINITY;

In fact, you do not have to initialize I to infinity to ensure that the loop is always executed. Any large floating point number can achieve this purpose, for example:

double i = 1.0e40;

This can work because the larger the floating point value, the larger the interval between it and its subsequent values. This distribution of floating point numbers is an inevitable result of a fixed number of valid positions. Adding 1 to a floating point number that is large enough does not change its value, because 1 is not enough to "fill the gap between it and its successor ".

The floating point operation returns the floating point value closest to its exact mathematical result. Once the distance between adjacent floating point values is greater than 2, adding 1 to a floating point value does not produce any effect because the result is not half of the two values. For the float type, adding 1 will not produce any effect. The minimum series is 225, that is, 33,554,432. For the double type, the minimum series is 254, which is about 1.8 × 1016.

The distance between adjacent floating point values is called an ULP, which is the abbreviation of the first letter of "unit in the last place. In version 5.0, the math. ULP method is introduced to calculate the ULP of float or double values.

In short, it is okay to use a double or a float value to represent an infinity. Most people are a little surprised when they hear this sentence for the first time, probably because we cannot use any integer type to represent infinity. Second, when a small floating point number is added to a large floating point number, the value of the large floating point number will not change. This is too intuitive, because it is not true for actual numbers. We should remember that binary floating point arithmetic is only an approximation of actual arithmetic.

Puzzle 28: cyclist

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.