Questions
I have a large decimal number, such as the square root of 2, and I want to view the first 100 decimal digits. However, float does not support this capability: 1.4142135623730951454746218587388284504413604736328125000000000000000000000000000000000000000000000000
What is the best way to do this? I do not want to import anything, preferably
Answers
If your requirement for precision is 100 decimal digits, I think you have to use decimal.Decimal.
The float in Python is not designed for this kind of precise calculation.
Using decimal.Decimal is almost as simple as float, you can do +, -, *, / and some other commonly used calculations directly between any decimal.Decimal without any consideration.
Also, decimal.Decimal supports setting up the precision directly you want:
>>> from decimal import getcontext, Decimal
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal( 0.142857 )
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal( 0.1428571428571428571428571429 )
https://docs.python.org/3.5/library/decimal.html?highlight=decimal#module-decimal
https://docs.python.org/3.5/library/decimal.html?highlight=decimal#module-decimal
Source
License : cc by-sa 3.0
http://stackoverflow.com/questions/34279711/100-digit-floating-point-python
Related