Maximum and Minimum float Values in Python

Modified: | Tags: Python, Numeric

In Python, the float type is a 64-bit double-precision floating-point number, equivalent to double in languages like 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 representable range for float is as follows. e+XXX means 10 to the power of XXX.

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

Note that there is no limit for the integer type (int) in Python 3.

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

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

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

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

You can check the actual precision using sys.float_info described below.

double is a double-precision floating-point number, which is a 64-bit representation of floating-point numbers.

In many programming languages, such as C, the 32-bit single-precision floating-point number is represented as float, and the double-precision as double. However, in Python, the double-precision type is named float, and there's no dedicated single-precision type.

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

Detailed information about float: sys.float_info

Use sys.float_info to get detailed information about float.

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))
# <class 'sys.float_info'>

You can access the value of each item as an attribute using the syntax sys.float_info.<item_name>.

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 in the C language.

attribute float.h macro explanation
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 float value: sys.float_info.max

You can obtain the maximum representable float value with sys.float_info.max. e+XXX means 10 to the power of XXX. The + sign is optional.

import sys

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

Values exceeding this are treated as inf, which represents infinity.

print(1.8e+308)
# inf

print(type(1.8e+308))
# <class 'float'>

In hexadecimal, it is as follows.

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

The minimum float value

The minimum negative float value

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

import sys

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

print(-1.8e+308)
# -inf

print(type(-1.8e+308))
# <class 'float'>

The minimum positive normalized float value: sys.float_info.min

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

import sys

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 float value

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

The minimum positive denormalized float 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

Any value smaller than this is considered to be 0.0.

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

print(1e-324)
# 0.0

In Python 3.9 and later, you can obtain the minimum positive denormalized float value by passing 0.0 to the math.ulp() function.

import math

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

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

Related Categories

Related Articles