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

  • SEARCH
  • Home
  • 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 9233271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:30:48+00:00 2026-06-18T06:30:48+00:00

I’m trying to understand what is happening in the python code below. I take

  • 0

I’m trying to understand what is happening in the python code below. I take the square root of 2 and divide its decimals by 1. Doing this for 5 times keeps giving the same value but the 6th and 7th time I get different values.

Why does the output change at the 6th time when the input value is the same as in the previous 5 calculations?

import math as M
frct = M.sqrt(2)

# 1
frct = 1 / (frct - int(frct))
print frct # 2.41421356237

# 2
frct = 1 / (frct - int(frct))
print frct # 2.41421356237

# 3
frct = 1 / (frct - int(frct))
print frct # 2.41421356237

# 4
frct = 1 / (frct - int(frct))
print frct # 2.41421356237

# 5
frct = 1 / (frct - int(frct))
print frct # 2.41421356237

# 6
frct = 1 / (frct - int(frct))
print frct # 2.41421356238

# 7
frct = 1 / (frct - int(frct))
print frct # 2.41421356235
  • 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-18T06:30:49+00:00Added an answer on June 18, 2026 at 6:30 am

    Short version, Python is rounding the output 🙂

    import math as M
    frct = M.sqrt(2)
    
    for i in range(7):
        frct = 1 / (frct - int(frct))
        print 'Attempt %d: %.20f' % (i, frct)
    

    Long version, floating points don’t store the real (no pun intended) value, they store an exponent and mantissa. See this wikipedia page for more info: http://en.wikipedia.org/wiki/Floating_point

    Basically, a floating point number is stored like this:

    Significant digits × base^exponent
    

    If you want a more precise version in Python, try the decimal module:

    import decimal
    context = decimal.Context(prec=100)
    frct = context.sqrt(decimal.Decimal(2))
    
    print 'Original square root:', frct
    
    for i in range(7):
        frct = context.divide(1, frct - int(frct))
        print 'Attempt %d: %s' % (i, frct)
    

    Output:

    Original square root: 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573
    Attempt 0: 2.414213562373095048801688724266222622763067167798368627068136427003657772608039155697953022512189319
    Attempt 1: 2.414213562373095048801688723683379910288448158038030882339615025168647691299718507620657724911891709
    Attempt 2: 2.414213562373095048801688727180436185136162216600057354932063779738350752352175486771948426117071942
    Attempt 3: 2.414213562373095048801688706780941248524496874988236407630784335182989956231878308913506955872772859
    Attempt 4: 2.414213562373095048801688825680854593346774866097140494471009059332623720827093783193465943198777227
    Attempt 5: 2.414213562373095048801688132680869461024772261055702548455940065126184103929661474210576202848416747
    Attempt 6: 2.414213562373095048801692171780866910134509900201052604731129303452089934643341550673727041448985316
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.