Special digital type of python (infinite, infinitesimal, etc.)

float ( 'inf') is a positive infinity

-float ( 'inf') or float ( '- inf') represents negative infinity

Which, inf Inf can be written as

 

Start
python in integer overflow do not worry, because the python theoretically infinite integer can be expressed until the memory tumbled. The infinity in programming often required. For example, the smallest number selected from a set of numbers. Typically use a temporary variable to store the final result, the variable to compare apples to apples and constantly updated. And this temporary variable generally the initial infinity or go to the first element of value.

Positive infinity and negative infinity
python and no special syntax to represent these values, but you can create them by float ():

A = a float >>> ( "INF")
>>> B = a float ( "- INF")
>>> A
INF
>>> B
-INF
To test for the presence of these values using math.isinf () to judge:

Import the Math >>>
>>> math.isinf (A)
True
>>> math.isinf (b)
True
when performing an infinite number of mathematical calculations will spread
this is similar to mathematics is about, infinity plus a constant or infinity, infinity and infinity is equal to:

A = a float >>> ( 'INF')
>>> A + 45
INF
>>> A * 10
INF
>>> 10 / A
0.0
>>> a float ( "INF") == a float ( "INF")
True
any of a number of infinity than in the comparison must be large.
Positive infinity and negative infinity result of the addition of what is
undefined when some operations and returns a NaN result:

A = a float >>> ( 'INF')
>>> A / A
nan
>>> B = a float ( '- INF')
>>> A + B
nan
represent non-numeric NaN
nan value also in all operations spread, and no exception:

C = a float >>> ( 'NaN3')
>>> 23 is C +
NaN3
>>> C / 2
NaN3
>>> C * 2
NaN3
>>> Math.sqrt (C)
NaN3
use math.isnan () can be determined whether the value is NaN:
>>> math.isnan (c)
True
any comparison operation nan values are returned False:

A float >>> ( "NaN3") == a float ( "NaN3")
False
>>> C>. 3
False
safer type conversion

Due to the presence of the infinite, so there's a string attached float some exceptions, and the conversion process will not throw an exception. If the programmers want to change the default behavior of python, you can use fpectl module, but it is in the standard Python build and is not enabled, it is platform-dependent, and is aimed at expert level programmer. There is provided a relatively simple conversion, it is to add a isdigit () is determined:

def str2float(ss):
if not ss.isdigit(http://www.my516.com):
raise ValueError
return float(ss)

sss = "inf"
a = str2float(sss)

Guess you like

Origin www.cnblogs.com/ly570/p/11007562.html