Python Forum

Full Version: is it a bug?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[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 ?
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.
(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
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).
(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.