Python Floating Point number calculations

385 pts.
Tags:
Floating point numbers
Python
Why the addition or subtraction of Floating point is so different in Python. for eg:- 0.1 + 0.2 - 0.3 would give a result of 5.55111512313e-17 or 1.1 + 2.2 would give a result like 3.3000000000000003
1

Answer Wiki

Thanks. We'll let you know when a new response is added.
Here is a good link on the Python site explaining it. It has to do with what the number base representation is.

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • TheRealRaven
    The problem has little to do with Python and much to do with floating point. Essentially the same result will be seen in all languages. For Python (and most languages), using floating point to perform calculations such as "1.1 + 2.2" is caused by inexperience. Using decimal() numbers will most often give more satisfactory results.

    Floating point numbers are represented internally by Python as binary fractions. To get a feeling for why that brings this problem, all you need to do is try to represent the value "0.1" as a binary fraction.
    36,880 pointsBadges:
    report
  • Subhendu Sen
    Assuming this is your homework. You have to read your study materials first then try to understand. It is better to solve your assignment with your own efforts.
    142,210 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: