Python initial value represented as Infinity

Source: Internet
Author: User

You only know that the initial value of the set variable is 0. Today, when writing network path analysis, in order to find the closest node from any coordinate, the initial setting is infinitely large, and then constantly replaced until the nearest node is found.

The start setting is Min_dis = 999999999999, as the distance is not particularly large is also possible, so imagine whether Python provides an infinite representation method, consult the document, it is true:

 fromRandomImportrandrange seq= [Randrange (10**10) forIinchRange (100)] DD= Float ("inf")  forXinchseq: forYinchseq:ifx = = y:ContinueD= ABS (xy)ifD <dd:xx, yy, DD=x, y, DPrint(XX,YY)

The purpose of the above code is to find 2 nearest natural numbers (unequal) from 100 random numbers:

Notice the DD initial value:

Here you need to ensure that the initial dd is large enough to judge the DD represents a very large value, after inspection, DD represents infinity, see the document:

Float also accepts the strings "Nan" and "INF" with an optional prefix "+" or "-" for not a number (Nan) and positive or n Egative Infinity.

What is the expression of negative infinity?

>>> float ('-inf') ==-float ('Inf') True

The following is transferred from:


Infinite (Infinity) number

Obviously, this is relative to the number of finite. In the infinite number the value is fixed, divided into +∞ (positive infinity) and-∞ (negative infinity). The values for exponent and significand are as follows.

Type Exponent Significand Value
Single precision Ff 0 2
Double precision 7FF 0 21024x768
Extended Double Precision 7FFF 0x80000000_00000000 216384
For extended double precision, because its J bit is explicit, it must be a value of 1 (otherwise the unsupported type), so the value of Significand is 0x80000000_00000000.

NaN (not a number)

If a number exceeds infinite, it is a Nan (not a number). In the Nan number, its exponent part is the maximum value that can be expressed, i.e. FF (single precision), 7FF (double precision), and 7FFF (extended double precision).

The difference between the Nan number and the infinite number is that the significand portion of the infinite number is 0 values (the BIT63 bit with the extended double precision is 1). The significand portion of the Nan number is not a 0 value.

The number of Nan includes the following two classes.

①snan (Signaling NaN) Number: The Snan number represents a more serious error value.

②qnan (Quiet NaN) Number: In general, the number of Qnan is acceptable.

The coding difference between the Snan and Qnan numbers is different from the Significand section, as shown below.

Nan type Significand Note
Snan 1.0XXX... XXX XXX not for 0
Qnan 1.1 ... 1.1 Back any value
The significand of the Snan number begins with 1.0 (and the digits after 1.0 are not 0 values), and the significand of the Qnan number is the beginning of the 1.1.

The x87 FPU or SSE instruction generates a #ia exception when encountering Snan, and does not produce a #ia exception when encountering Qnan (except for some of the instructions).

So since Nan is not a real value, how does the program determine if the variable becomes nan? Most languages have a series of function definitions for Nan values, and the most common functions in C are as follows:

_isnan (double x); Determine if it is Nan

_finite (double x); Whether the interpretation is infinity

Python initial value represented as Infinity

Related Article

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: 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.