Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is it a bug?
#1
[Image: python-problem-1.png]

Hi. I started python today #I'm a noob

this is my code
X1=3.145
X2=X1-int(X1) # so it must be 3.145-3.000 =0.145

Y1=7.145
Y2=Y1-int(Y1) # and the results must be 0.145

# X2 and X1 must be equal ! but why they aren't ?

#question 1 : WHY?

#question 2 : what should I do ?
Reply
#2
Answer 1: Please don't post images.
Answer 2: Please copy and paste instead of posting images.
Answer 3: Floating point math: floats are stored as fractions of powers of two. This can lead to small deviations from expectation.
Answer 4: Compare the absolute difference to a small epsilon, check out the decimal or fractions modules, or use integers.
Answer 5: Please post code using Python tags.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Apr-24-2019, 01:42 AM)ichabod801 Wrote: Answer 1: Please don't post images.
Answer 2: Please copy and paste instead of posting images.
Answer 3: Floating point math: floats are stored as fractions of powers of two. This can lead to small deviations from expectation.
Answer 4: Compare the absolute difference to a small epsilon, check out the decimal or fractions modules, or use integers.
Answer 5: Please post code using Python tags.


how can I edit my post and remove the image ?
so when you want to compare two variations as a condition it gives you a "False"
and tnx
Reply
#4
To upset you a little
When you enter 0.3 in the repl, it's not stored as 0.3.
It a number, which is very close to 0.3, but it's not exact 0.3.

You should learn more about floating-point. 0.1 + 0.2 != 0.3
Never ever compare floating-point values of equality.
If you need to check, if a value is very close, use math.isclose(value1, value2).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Apr-24-2019, 02:21 AM)akbarasghar Wrote: how can I edit my post and remove the image ?

Don't worry about it. Just keep it in mind for the future.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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