Hướng dẫn max float python

In python 3 there is no sys.maxint There is a sys.maxsize

>>> sys.maxsize

That does not mean that the maximum int is limited to 2 billion! It means that the size of the object containing the integer has a maximum size of 2 billion bytes. I.e. a very very large number

For float have a look at sys.float_info

>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

And specifically sys.float_info.max

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

In Python, the floating-point number type float is a 64-bit representation of a double-precision floating-point number, equivalent to double in other programming languages such as C.

This article explains how to get and check the range (maximum and minimum values) that float can represent in Python.

In many environments, the range is as follows.

-1.7976931348623157e+308 <= f <= 1.7976931348623157e+308

This article describes the following contents.

  • float is a double-precision floating-point number in Python
  • Information about float: sys.float_info
  • The maximum value that float can represent: sys.float_info.max
  • The minimum value that float can represent
    • The minimum negative value
    • The minimum positive normalized value: sys.float_info.min
    • The minimum positive denormalized value

Note that there is no limit for the integer type int in Python3.

  • Integer (int) has no max limit in Python3

The float type also has a special value, inf, which represents infinity.

  • "inf" for infinity in Python

float is a double-precision floating-point number in Python

In Python, floating-point numbers are usually implemented using the C language double, as described in the official documentation.

Floating point numbers are usually implemented using double in C; Built-in Types — Python 3.9.7 documentation

Unless you use a special implementation that is not CPython, you can assume that double is used. You can check the actual precision by using sys.float_info described below.

Double-precision floating-point number double is a 64-bit floating-point number representation.

  • Double-precision floating-point format - Wikipedia

In many other programming languages, such as C, single-precision floating-point numbers are used as float and double-precision floating-point numbers as double, but in Python, double precision is called float, and there is no single-precision type.

Note that in NumPy, you can explicitly specify the type with the number of bits, such as float32, float64.

  • NumPy: Cast ndarray to a specific dtype with astype()

Information about float: sys.float_info

Use sys.float_info to get information about float.

  • sys.float_info — System-specific parameters and functions — Python 3.9.7 documentation

The sys module is included in the standard library, so no additional installation is required.

import sys

print(sys.float_info)
# sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

print(type(sys.float_info))
# 

You can get the value of each item as an attribute, like sys.float_info..

print(sys.float_info.max)
# 1.7976931348623157e+308

See the official documentation for a list of items and their explanations. Each item corresponds to a floating point constant defined in the standard header file float.h of C language.

attributefloat.h macroexplanation
epsilon DBL_EPSILON difference between 1.0 and the least value greater than 1.0 that is representable as a float
dig DBL_DIG maximum number of decimal digits that can be faithfully represented in a float
mant_dig DBL_MANT_DIG float precision: the number of base-radix digits in the significand of a float
max DBL_MAX maximum representable positive finite float
max_exp DBL_MAX_EXP maximum integer e such that radix**(e-1) is a representable finite float
max_10_exp DBL_MAX_10_EXP maximum integer e such that 10**e is in the range of representable finite floats
min DBL_MIN minimum representable positive normalized float
min_exp DBL_MIN_EXP minimum integer e such that radix**(e-1) is a normalized float
min_10_exp DBL_MIN_10_EXP minimum integer e such that 10**e is a normalized float
radix FLT_RADIX radix of exponent representation
rounds FLT_ROUNDS integer constant representing the rounding mode used for arithmetic operations. This reflects the value of the system FLT_ROUNDS macro at interpreter startup time

The maximum value that float can represent: sys.float_info.max

You can get the maximum value that float can represent with sys.float_info.max. e+XXX means 10 to the power of XXX. + is optional.

print(sys.float_info.max)
# 1.7976931348623157e+308

Values exceeding this are treated as inf, which means infinity. See the following article for details about inf.

  • "inf" for infinity in Python

print(1.8e+308)
# inf

print(type(1.8e+308))
# 

In hexadecimal, it is as follows.

print(sys.float_info.max.hex())
# 0x1.fffffffffffffp+1023

The minimum value that float can represent

The minimum negative value

The minimum negative value that float can represent is sys.float_info.max with -. Values smaller than this are treated as negative infinity.

print(-sys.float_info.max)
# -1.7976931348623157e+308

print(-1.8e+308)
# -inf

print(type(-1.8e+308))
# 

The minimum positive normalized value: sys.float_info.min

You can get the minimum positive normalized value with sys.float_info.min.

print(sys.float_info.min)
# 2.2250738585072014e-308

A normalized number is a value whose exponent part is not 0. sys.float_info.min is as follows in hexadecimal.

print(sys.float_info.min.hex())
# 0x1.0000000000000p-1022

The minimum positive denormalized value

sys.float_info.min is the minimum positive normalized value.

Values that their exponent part is 0 and their mantissa part is not 0 are called denormalized numbers.

  • Double-precision floating-point format - Exponent encoding - Wikipedia
  • Subnormal number - Wikipedia

The minimum positive denormalized value can be converted from a hexadecimal string as follows:

print(float.fromhex('0x0.0000000000001p-1022'))
# 5e-324

print(format(float.fromhex('0x0.0000000000001p-1022'), '.17'))
# 4.9406564584124654e-324

A value less than this is considered to be 0.0.

print(1e-323)
# 1e-323

print(1e-324)
# 0.0

In Python 3.9 or later, you can get the minimum positive denormalized value by passing 0.0 to the newly added function math.ulp().

  • math.ulp() — Mathematical functions — Python 3.9.7 documentation

import math

print(math.ulp(0.0))
# 5e-324

print(format(math.ulp(0.0), '.17'))
# 4.9406564584124654e-324