Python defaults to 17 decimal places, but here's a question of what to do when our calculations need to use higher precision (more than 17 decimal places).

Source: Internet
Author: User

1. Using formatting (not recommended)123 >>> A ="%.30f"% (1/3)>>>a'0.333333333333333314829616256247'can be displayed, but inaccurate, the numbers that follow are often meaningless. 2. High precision using the decimal module, with GetContext123456789101112 >>> fromDecimalImport*>>>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)>>>Bdecimal ('0.33333333333333333333333333333333333333333333333333')>>> C = Decimal (1)/decimal (17)>>>Cdecimal ('0.058823529411764705882352941176470588235294117647059')>>>float (c)0.058823529411764705The default context precision is 28 bits, which can be set to 50 bits or higher. This way, when analyzing complex floating-point numbers, you can have a higher level of accuracy that you can control. Actually, you can keep an eye on the rounding in the context.The =round_half_even parameter. Round_half_even, when half, close to even.

Python defaults to 17 decimal places, but here's a question of what to do when our calculations need to use higher precision (more than 17 decimal places).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.