Python: Why are sufficiently large floating point values infinite? -


>>> 1e100000000 inf >>> 1000000 ... [400 zeroes snipped] ... 000000 1000000 ... [400 zeroes snipped] ... 000000 >>> 1000000 ... [400 zeroes snipped] ... 000000.0 inf 

why python decide large floats infinite? if representation can't represent value, expect give error, does:

>>> 10.0 ** 1000000000000000000000000000000000000000000000000000000 overflowerror: (34, 'numerical result out of range') 

why first case silent if second case complains?

because tried create floating point number larger largest value representable in floating point value.

see sys.float_info named tuple; sys.float_info.max largest value representable in such values. python floats, unlike python integers, have upper limit determined os , hardware platform, integers limited available memory.

on mac, maximum value is:

>>> import sys >>> sys.float_info.max 1.7976931348623157e+308 

so 1 308 zeros representable, 2 many zeros not:

>>> float('1' + '0' * 308) 1e+308 >>> float('2' + '0' * 308) inf 

floats limited because floating point arithmetic typically handled floating point unit on cpu, , python's float type limited piece of hardware can handle.

defining float literal different operation applying arithmetic floating point value; first determined @ compile time, latter determined @ runtime. literal produce syntaxerror exception, while @ runtime can throw other exceptions. throwing syntax error float literal on other platforms still valid not option, instead of exception float('inf') instead.

the python specification float literals declares permitted range implementation dependent:

the allowed range of floating point literals implementation-dependent.

there point calculations return float('inf') because once have reached infinity calculations easy (most cases give form of infinity back), close infinity overflows instead. float exponentiation implementation uses c pow() function, , exception thrown when function reports overflow.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -