Home  >  Article  >  Backend Development  >  How many bytes does Python's floating point number occupy?

How many bytes does Python's floating point number occupy?

anonymity
anonymityOriginal
2019-06-12 11:24:4514766browse

Floating point numbers are represented by the native double precision (64 bit) of the floating point number on the machine. Provides approximately 17 digits of precision and an exponent ranging from -308 to 308. It is the same as the double type in C language. Python does not support 32-bit single-precision floating point numbers. If your program requires precise control over intervals and numerical precision, consider using the numpy extension library.

Python 3.X provides a precision of 17 digits by default for floating point numbers.

How many bytes does Python's floating point number occupy?

Popular explanation about single precision and double precision:

Single precision type and double precision type , its type specifier is float single-precision specifier, double double-precision specifier. In Turbo C, the single precision type occupies 4 bytes (32 bits) of memory space, its value range is 3.4E-38 ~ 3.4E 38, and can only provide seven significant digits. The double precision type occupies 8 bytes (64 bits) of memory space, its value range is 1.7E-308 ~ 1.7E 308, and can provide 16 significant digits.

Requires more than 17 digits of precision analysis

Python defaults to 17 decimal digits of precision, but there is a problem here, that is, when we What to do when the calculation requires higher precision (more than 17 decimal places)?

1. Use formatting (not recommended)

>>> a = "%.30f" % (1/3)
>>> a
'0.333333333333333314829616256247'

can be displayed, but it is not accurate and the following numbers are often meaningless.

2. Use the decimal module for high precision and cooperate with getcontext

>>> from decimal import *
>>> print(getcontext())
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> getcontext().prec = 50
>>> b = Decimal(1)/Decimal(3)
>>> b
Decimal('0.33333333333333333333333333333333333333333333333333')
>>> c = Decimal(1)/Decimal(17)
>>> c
Decimal('0.058823529411764705882352941176470588235294117647059')
>>> float(c)
0.058823529411764705

The default context precision is 28 bits, which can be set to 50 bits or even higher. In this way, when analyzing complex floating point numbers, you can have higher controllable precision. In fact, you can pay attention to the rounding=ROUND_HALF_EVEN parameter in the context. ROUND_HALF_EVEN, when half, close to even.

The above is the detailed content of How many bytes does Python's floating point number occupy?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn