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 462753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:06:24+00:00 2026-05-12T23:06:24+00:00

I’ve run into something odd, here, and I’m at a loss — I have

  • 0

I’ve run into something odd, here, and I’m at a loss — I have a feeling this has something to do with floating precision, but I’m surprised Python would not display the approximation error, if so.

I’m working on Project Euler problem 62. As a simple test (I’ve since solved using a different approach), I had a is_cube which I intended to check if a number cubes perfectly. So, to try the sample given, I did this:

def is_cube(i):
    c = i ** (1./3)
    print "c is", c

When I ran this with i = 41063625, the expected output was:

c is 345.0

Here’s where the unexpected happened:

def is_cube(i):
    c = i ** (1./3)
    print "c is", int(c)

Suddenly, I had this:

c is 344

The value c does not compare against my 345.0 literal, either — c < 345.0 is True.

Is there precision in this number that Python is not showing me? I seem to recall reading about a change to make floats appear more sane when printed; is this it? What is different about these two cases?

>>> def is_cube(i):
...     c = i ** (1./3)
...     print "c is", c
... 
>>> is_cube(41063625)
c is 345.0
>>> 41063625 ** (1./3)
344.99999999999989

Edit: Still had the window open and did this:

>>> print _
345.0

Now I’m starting to think I should have known all along that print was to blame.

  • 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-05-12T23:06:24+00:00Added an answer on May 12, 2026 at 11:06 pm

    This is using c.__str__() (aka. str(c)) :

    print "c is", c
    

    This is using c.__repr__() (aka. repr(c)) :

    >>> c # In the Python shell
    

    IIRC, __str__ truncates to 10 decimals, whereas __repr__ goes further. To get the same behavior as in the Python shell, you could do :

    print repr(c)
    # Or
    print "%r" % c
    # Or
    print "%.16f" % c
    

    The change you’re talking about in your message is only about Python 3.1 and do not alter the precision of the output : Gay’s algorithm which is used for __repr__ on floating point numbers in Python 3.1 will, when given two representation choices which yields the same floating point value (like 0.2 and 0.2000000000000001), choose the shortest one.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have some data like this: 1 2 3 4 5 9 2 6
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.