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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:57:17+00:00 2026-05-23T18:57:17+00:00

Got a question regarding to the underlying data structure of float (and precision) in

  • 0

Got a question regarding to the underlying data structure of float (and precision) in Python:

>>> b = 1.4 + 2.3
>>> b
3.6999999999999997

>>> c = 3.7
>>> c
3.7000000000000002

>>> print b, c
3.7  3.7

>>> b == c
False

it seems the values of b and c are machine dependent, they are the numbers that closest to the target values but not exactly the same numbers. I was supervised that we get the ‘right’ numbers with ‘Print’, and someone told me that it was because print ‘lies’ while Python chose to tell us the truth i.e. showing exactly what they have stored.

And my questions are:

1. How to lie? e.g. in a function we take two values and return if they are the same, how I could have a best guess if the number of decimal(precision) is unknown? like b and c mentioned above? is there a well defined algorithm to do that? I was told that every language (C/C++) will have this kind of issue if we have floating point calculation involved, but how do they ‘solve’ this?

2. why we cannot just store the actual number instead of storing the closest number? is it a limitation or trading for efficiency?

many thanks
John

  • 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-23T18:57:18+00:00Added an answer on May 23, 2026 at 6:57 pm

    For the answer to your first question, take a look at the following (slightly condensed) code from Python’s source:

    #define PREC_REPR       17
    #define PREC_STR        12
    
    void PyFloat_AsString(char *buf, PyFloatObject *v) {
        format_float(buf, 100, v, PREC_STR);
    }
    
    void PyFloat_AsReprString(char *buf, PyFloatObject *v) {
        format_float(buf, 100, v, PREC_REPR);
    }
    

    So basically, repr(float) will return a string formatted with 17 digits of precision, and str(float) will return a string with 12 digits of precision. As you might have guessed, print uses str() and entering the variable name in the interpreter uses repr(). With only 12 digits of precision, it looks like you get the “correct” answer, but that is just because what you expect and the actual value are the same up to 12 digits.

    Here is a quick example of the difference:

    >>> str(.1234567890123)
    '0.123456789012'
    >>> repr(.1234567890123)
    '0.12345678901230001'
    

    As for your second question, I suggest you read the following section of the Python tutorial: Floating Point Arithmetic: Issues and Limitations

    It boils down to efficiency, less memory and quicker floating point operations when you are storing base 10 decimals in base 2 than any other representation, but you do need to deal with the imprecision.

    As JBernardo pointed out in comments, this behavior is different in Python 2.7 and above, the following quote from the above tutorial link describes the difference (using 0.1 as an example):

    In versions prior to Python 2.7 and Python 3.1, Python rounded this
    value to 17 significant digits, giving ‘0.10000000000000001’. In
    current versions, Python displays a value based on the shortest
    decimal fraction that rounds correctly back to the true binary value,
    resulting simply in ‘0.1’.

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

Sidebar

Related Questions

I've got a question regarding a SQL-select-query: The table contains several columns, one of
I've got another question regarding to basic Android programming: How can I access the
While reading the answers to this question I got a doubt regarding the default
I've got a question regarding git branching protocol. I'm starting to get more familiar
I've got a question regarding the difference between PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) and normal filters defined
I am (very) new to WPF and I have got a question regarding that.
Got a simple question regarding twisted. I can create a trivial basic test with
I've got a quick question regarding the use of repositories. But the best way
Got a (hopefully small) question regarding SVN and checking out repos. Basically I see
Got a question regarding C2DM and how it is properly used for a turn-based

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.