Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this happening?
#1
Hi,
Newly learning Python through Linkedin Learning and I got the following result when I make multiplication. Why is this happening, any idea? I have experienced this oddness in some other multiplications but not with the numbers with 2 decimal..

Thanks

[Image: Zh2JPl1_d.webp?maxwidth=728&fidelity=grand]
Reply
#2
Many numbers can't be exactly represented by the standard floating point representation.  You should write your code in such a way that this limitation doesn't cause problems for you.   See the python FAQ entry on this.
Skaperen and taner7 like this post
Reply
#3
Yes:

https://docs.python.org/3/tutorial/floatingpoint.html

In [5]: 0.1 + 0.1 + 0.1
Out[5]: 0.30000000000000004
taner7 likes this post
Reply
#4
try to work out what that number is in base two.  the trouble is that 3.2 takes an infinite number of bits to do it.  but computers limit the number of bits per number.  the bits get set to a number that needs no more bits than the maximum.  it gets set to the number closest to what was wanted.  when that number gets printed, it is printing that very close number the best it can (most numbers can't even be printed in base ten, such as 1/3).
taner7 likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb What's happening here? jesse68 3 950 Feb-27-2023, 04:53 PM
Last Post: Gribouillis
  2 if statements happening at the same time M0TH 3 946 Nov-19-2022, 11:37 PM
Last Post: M0TH
  Binding not explicitly declared happening karkas 4 2,886 Aug-05-2019, 05:09 PM
Last Post: karkas
  Why this python ValueError is not happening aurquiel 2 3,041 Aug-20-2018, 07:17 PM
Last Post: aurquiel
  Something strange happening here !? TerryRitchie 4 3,914 Apr-20-2017, 07:14 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020