floating point

floating point number

  A floating-point number is a numerical representation of a number that belongs to a certain subset of rational numbers , and is used in computers to approximate any real number . Specifically, this real number is obtained by multiplying an integer or fixed-point number (ie, the mantissa ) by an integer power of a base (usually 2 in computers), which is similar to base 10 scientific notation .

About floating point inaccuracy

  Python's default is 17 digits of precision, which is 16 digits after the decimal point. Although there are 16 digits, this accuracy is more and more inaccurate.

Secondly, the inaccuracy of decimals is due to the infinite loop in the process of converting to binary, and there will be deviations when approximating.

With the help of the decimal module, calculate high-precision floating-point numbers (more than 16 bits)

#With the help of the "getcontext" and "Decimal" methods of the decimal module
>>> a = 3.141592653513651054608317828332
>>> a
3.141592653513651
>>> from decimal import *
>>> getcontext()
Context(prec=50, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[FloatOperation], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> getcontext().prec = 50
>>> a = Decimal(1)/Decimal(3) #Note , the result is correct in the calculation of fractions, it will be inaccurate if the super-long precision decimal is defined directly 
>>> a
Decimal('0.33333333333333333333333333333333333333333333333333')

>>> a = '3.141592653513651054608317828332'
>>> Decimal(a)
Decimal('3.141592653513651054608317828332')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324628356&siteId=291194637