More numbers in irrational numbers

>>> str(1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702) '1.41421356237' 

Is there a way to get str () to write more digits of a number into a string? I do not understand why it truncates by default.

+4
source share
4 answers

Try the following:

 >>> from decimal import * >>> Decimal('1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702') Decimal('1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702') 

The float literal is truncated by default to fit in the space available to it (i.e. not because of str ):

 >>> 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702 1.4142135623730951 

If you need more decimals, use decimal .

+2
source

Python floating point numbers only use double precision, which is 64 bits. They simply cannot represent (significantly) more numbers than you see.

If you need more, look at the built-in decimal or mpmath module .

+5
source

The Python compiler trims; your floating literal has higher precision than can be represented in the double C. Express the number as a string in the first place if you need higher precision.

+1
source

This is because it is converted to float. This is not a conversion to the string calling it. You must use decimal.Decimal to represent such numbers with high precision.

0
source

Source: https://habr.com/ru/post/1485492/


All Articles