Why is Python displaying inaccurate decimal values for floating point numbers?

  • Python
  • Thread starter ali PMPAINT
  • Start date
  • Tags
    Float
In summary, You have divided one float by another, got the floating point precision result of 1÷3, then converted it to a higher precision representation of the same truncated result.
  • #1
ali PMPAINT
44
8
TL;DR Summary
I want to obtain 1/3 for 20 decimal point, but I get wrong result:
So, I searched the whole internet, but wasn't the thing I actually wanted.
Capture.PNG

Which is obviously wrong! it should be 0.333333333333333333333333333333333333333333333333333333333333333333333...
And please, don't tell me the reason, I heard it a lot, I just want the solution to my problem.
 
Technology news on Phys.org
  • #2
The title is somewhat misleading - floats are working as designed. You've divided one float by another, got the floating point precision result of 1÷3, then converted it to a higher precision representation of the same truncated result.

If you don't want to divide a float by a float and get a float, then you need to do the type conversion before the division.
 
Last edited:
  • Like
Likes ali PMPAINT
  • #3
A side-effect of the approximate nature of floating points (and numerical methods in general) is that they introduce their own set of non-trivial errors in results compared to a purely mathematical formulation.

To understand and work with these errors there is probably no way around studying the topic a bit, even if it can appear a bit complicated. I would recommend the old classic What Every Computer Scientist Should Know About Floating-Point Arithmetic, but if that is too heavy for you then at least search for "floating point rounding errors" and go trough some of those descriptions.
 
  • Like
Likes Klystron, QuantumQuest and ali PMPAINT
  • #4
So, Thanks, but I didn't understand anything. I am a beginner, it's my first time learning programming. I just want to change0.333333333333333314829616256247390992939472198486328125 to 0.333333333333333333333333333333333333333333333333333333, how?
 
  • #5
Filip Larsen said:
at least search for "floating point rounding errors" and go trough some of those descriptions.
I did, but everything not related to my problem appeared.
 
  • #7
The default representation of a number has a limited number of decimal places (more than I'm writing here, but you'll see the point). So 1/3 is interpreted something like 1.00/3.00 and gives you 0.33. Then you give this number to the Decimal class, asking for more digits. So it stores 0.3300, which is exactly 0.33 with more digits. Since this is all being done in binary, adding a string of zeroes to the end gives you a string of apparently random digits to the end of the base 10 number that gets printed.

If you want to get 0.3333, you need to start with 1.0000. So you need to create a Decimal 1, a Decimal 3, and divide them, not divide them then create a Decimal.
 
  • Like
Likes Klystron, QuantumQuest and ali PMPAINT
  • #8
Ibix said:
The default representation of a number has a limited number of decimal places (more than I'm writing here, but you'll see the point). So 1/3 is interpreted something like 1.00/3.00 and gives you 0.33. Then you give this number to the Decimal class, asking for more digits. So it stores 0.3300, which is exactly 0.33 with more digits. Since this is all being done in binary, adding a string of zeroes to the end gives you a string of apparently random digits to the end of the base 10 number that gets printed.

If you want to get 0.3333, you need to start with 1.0000. So you need to create a Decimal 1, a Decimal 3, and divide them, not divide them then create a Decimal.
Thank you! That fixed my issue.
 
  • #9
Get used to dealing with rounding errors like this. It's an issue to which there is no general solution.
 
  • Like
  • Informative
Likes Filip Larsen and ali PMPAINT
  • #10
ali PMPAINT said:
And please, don't tell me the reason, I heard it a lot, I just want the solution to my problem.

Please don't tell you the reason? Well...OK.

Python:
print "0.33333333333333333333"

will do what you want without us telling you the reason.
 
  • Like
Likes phinds, hmmm27, ali PMPAINT and 3 others
  • #11
There is also the python 2 vs 3 issues with respect to the / operator.

In 2, 1 / 3 = 0 because it notices you used integers and so used an integer division.

Whereas 1. / 3. will use a floating pt division because one of the operands is a float.

In 3, the 1 / 3 will promote the operands to floats and use a floating pt divide too and give you a floating pt answer.

So to get an integer divide, you need to use the new operator // ie (1 // 3) which complements the % operator for module math.

This is important to know, should you need to port code from python 2 to 3 for some project.
 
  • Like
  • Informative
Likes ali PMPAINT, Klystron, QuantumQuest and 1 other person
  • #12
jedishrfu said:
This is important to know, should you need to port code from python 2 to 3 for some project.
And there is double fun because from __future__ import division makes python2 behave like python3 in this respect.
 
  • Informative
Likes ali PMPAINT
  • #13
ali PMPAINT said:
So, I searched the whole internet
The whole internet? That must have taken quite a bit of time...
 
  • Like
  • Haha
Likes phinds, ali PMPAINT, jim mcnamara and 1 other person
  • #14
Vanadium 50 said:
Please don't tell you the reason? Well...OK.

Python:
print "0.33333333333333333333"

will do what you want without us telling you the reason.
lol, but I meant the reason that why python is showing me 0.333333333333333314829616256247390992939472198486328125 instead of 0.333333333333333333333333333333333333333333333333333333 , which I have read it in many sites, but they didn't write any proper solution.
 
  • #15
ali PMPAINT said:
but they didn't write any proper solution.

This is because the solution that will work best will depend on exactly what you are doing.

Using decimal for everything will solve your immediate problem but unless more information is given we can't determine if there any pitfalls to doing that.

BoB
 
  • Informative
Likes ali PMPAINT
  • #16
ali PMPAINT said:
lol, but I meant the reason that why python is showing me 0.333333333333333314829616256247390992939472198486328125 instead of 0.333333333333333333333333333333333333333333333333333333 , which I have read it in many sites, but they didn't write any proper solution.
It's because floating point arithmetic is inherently imprecise, for most numbers. Floating point numbers are stored in just a few bytes, so numbers that have an infinitely long binary representation get rounded or truncated. Even some numbers that have a short decimal representation, such as 0.1 or 0.2, can have an infinitely long binary representation.

Other numbers, such as 0.5 and .25, with short decimal representations, also have short binary representations, so they are stored in exact form.
 
  • Informative
Likes ali PMPAINT

What is the meaning of "Float not working properly"?

"Float not working properly" refers to an issue where an element on a webpage that has been set to float is not behaving as intended. This can result in the element not appearing in the correct location or overlapping with other elements on the page.

What can cause float not to work properly?

There are a few common causes for float not working properly. These include using an incorrect or unsupported value for the float property, not clearing floats correctly, and having conflicting CSS rules that affect the positioning of the floated element.

How can I fix float not working properly?

The best way to fix float not working properly is to carefully review your CSS code and make sure that all float values and clearing techniques are used correctly. You may also need to adjust other CSS rules that may be affecting the positioning of the floated element.

Can float not working properly affect other elements on the page?

Yes, float not working properly can affect the positioning of other elements on the page. This is because floated elements are taken out of the normal flow of the document, which can cause other elements to shift or overlap. It is important to use proper clearing techniques to prevent this issue.

Is there an alternative to using float for positioning elements?

Yes, there are other CSS properties that can be used for positioning elements, such as flexbox and CSS grid. These may be better options in certain situations and can help avoid some of the issues that can arise with using float. However, float is still commonly used and can be a useful tool when used correctly.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
867
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top