3

So, I'm trying to complete an exercise in URI Online Judge but I'm having trouble with checking if a float is an whole number. Here's my code:

I = 0.0
J = 1.0

while I <= 2:
    for y in range(3):
        if I.is_integer():
            print("I=%d J=%d" % (I, J))
        else:
            print("I=%.1f J=%.1f" %(I, J))
        J += 1.0
    J -= 2.8
    I += 0.2

Well, the output works fine until the last loop. In the first two loops where the values are whole numbers, it prints as it's supposed to: with floating point, because it's a whole number. But in the last loop, I get a whole number with a floating point, any ideas to fix this? Here's the output:

I=0 J=1 #working fine here
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
I=0.4 J=1.4
I=0.4 J=2.4
I=0.4 J=3.4
I=0.6 J=1.6
I=0.6 J=2.6
I=0.6 J=3.6
I=0.8 J=1.8
I=0.8 J=2.8
I=0.8 J=3.8
I=1 J=2 # also works fine here
I=1 J=3
I=1 J=4
I=1.2 J=2.2
I=1.2 J=3.2
I=1.2 J=4.2
I=1.4 J=2.4
I=1.4 J=3.4
I=1.4 J=4.4
I=1.6 J=2.6
I=1.6 J=3.6
I=1.6 J=4.6
I=1.8 J=2.8
I=1.8 J=3.8
I=1.8 J=4.8
I=2.0 J=3.0 # got messed up here
I=2.0 J=4.0
I=2.0 J=5.0

[EDIT] - Working as expected now

I = 0.0
J = 1.0

while I <= 2:
    for y in range(3):
        if I.is_integer() or I > 1.9:
            if 1.9 < I < 2.1: # Made some workarounds here
                I += 0.1    
            print("I=%d J=%d" % (I, J))
        else:
            print("I=%.1f J=%.1f" %(I, J))
        J += 1.0
    J -= 2.8
    I += 0.2
Lucas Figueiredo
  • 175
  • 2
  • 2
  • 9
  • 1
    sorry I didn't read your question properly before suggesting the dupe. I see you are using `is_integer` already. – Paul Rooney Apr 15 '18 at 23:18
  • 1
    Your test with is_Integer works fine. The problem is that **J** is not an integer when you think it is. **0.2** and **0.8** do not have exact representations as floating point numbers -- they will be a little bit off, and so **J** will be a little bit off too. – Matt Timmermans Apr 15 '18 at 23:19
  • 1
    If you hadn't cut off the digits with `%.1f`, you would have seen that your numbers aren't integers. – user2357112 Apr 15 '18 at 23:20
  • 1
    This is because `0.2f` isn't actually `0.2`, it's actually `0.2000000000000000111022...` As you repeatedly add this to I, the small error from encoding the value as a float builds up, and eventually reaches a value that's large enough for `I.isInteger()` to return false, which causes your program to print the result as a float. – RenanB Apr 15 '18 at 23:20
  • I changed your title to reflect your actual issue and recommended for reopening. Recommended reading https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Paul Rooney Apr 15 '18 at 23:23
  • I've voted to reopen as well. The asker is already using `is_integer()` and is confused by the result, so it's insufficient to just tell them to use `is_integer()`. These comments contain the information needed for a good answer, but it cannot be posted as a real answer because the question is closed. – cgmb Apr 15 '18 at 23:39
  • I made some changes in the code to make the judge approve it, i tried to use math.isclose, but the platform don't support libraries. I don't think that it's the best way to do it but, it's working now. I'll edit the post. – Lucas Figueiredo Apr 16 '18 at 21:50
  • 1
    @tdelaney Please take another look at this question you closed. It does not look like a duplicate, and closing has turned a legitimate question into a mess. It cannot be improved without posting answers, so it should either be reopened or deleted. – cgmb Apr 28 '18 at 02:06

0 Answers0