After inputting
from decimal import *
getcontext().prec = 6
Decimal (1) / Decimal (7)
I get the value
Decimal('0.142857')
However if I enter Decimal (1.0/7) I get
Decimal('0.142857142857142849212692681248881854116916656494140625')
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The 1.0 / 7 computes a binary floating point number to 17 digits of precision. This happens before the Decimal constructor sees it:
The binary fraction, 2573485501354569 / 18014398509481984 is as close as binary floating point can get using 53 bits of precision. It is not exactly 1/7th, but it’s pretty close.
The Decimal constructor then converts the binary fraction to as many places as necessary to get an exact decimal equivalent. The result you’re are seeing is what you get when you evaluate 2573485501354569 / 18014398509481984 exactly:
Learning point 1: Binary floating point computes binary fractions to 53 bits of precision. The result is rounded if necessary.
Learning point 2: The Decimal constructor converts binary floating point numbers to decimals losslessly (no rounding). This tends to result in many more digits of precision than you might expect (See the 6th question in the Decimal FAQ).
Learning point 3: The decimal module is designed to treat all numbers as being exact. Only the results of computations get rounded to the context precision. The binary floating point input is converted to decimal exactly and context precision isn’t applied until you do a computation with the number (See the final question and answer in the Decimal FAQ for details).
Executive summary: Don’t do binary floating point division before handing the numbers to the decimal module. Let it do the work to your desired precision.
Hope this helps 🙂