简体   繁体   中英

Precision of floating-point numbers

What precision are the floating-point numbers in Python? Is it always double-precision or is it implementation or platform specific? Does it differ in, say, CPython and PyPy?

From Python documentation:

Floating point numbers are usually implemented using double in C ; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in sys.float_info .

sys.float_info is a struct sequence holding information about the float type. It contains low level information about the precision and internal representation.

import sys

print(sys.float_info.mant_dig)

mant_dig attribute points to float precision: the number of base-radix digits in the significand of a float

Almost all platforms map Python floats to IEEE 754 double precision.

import sys
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)

Found here: https://docs.python.org/3/library/stdtypes.html#typesnumeric Sec 4.4

Does this help you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM