Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:11:44+00:00 2026-06-10T22:11:44+00:00

After inputting from decimal import * getcontext().prec = 6 Decimal (1) / Decimal (7)

  • 0

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')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:11:46+00:00Added an answer on June 10, 2026 at 10:11 pm

    The 1.0 / 7 computes a binary floating point number to 17 digits of precision. This happens before the Decimal constructor sees it:

    >>> d = 1.0 / 7
    >>> type(d)
    <type 'float'>
    >>> d.as_integer_ratio()
    (2573485501354569, 18014398509481984)
    

    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:

    >>> from decimal import Decimal, getcontext
    >>> getcontext().prec = 100
    >>> Decimal(2573485501354569) / Decimal(18014398509481984)
    Decimal('0.142857142857142849212692681248881854116916656494140625')
    

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i get a nsstring from a nsdictionary via: inputString = [dataUsage valueForKey:@amount]; after that
After some time researching and trying different things I still cannot get my @ExceptionHandler
I have the following code that works correctly. However after I add an else
I have a script which allows me to lookup for a hostname after inputting
After inputting a string such as: '3,11,15,16,35' if you wanted each number to represent
I'm attempting to get a few user-input parameters from the console, two strings, two
I want to save and filter user’s objects in my django app. After inputting
After deploying WCF server (svc) on my Server, I have got this message when
After discovering about Javascript namespaces, I tried to implement them but I run into
After a system upgrade i am no longer able to start my tomcat5 server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.