0

I have a double called int_step which i'm increasing every loop and expect to end when I have reached 1. However it iterates too much as rounding throws in some unexpected behaviour.

For some reason the following code adds quite nicely, but close to 1 i get an offset:

            int_step += 0.01;

            // TODO : Fix rounding step in double with step increase of + 0.01, provides error because value will become 1.000000007
            if (int_step >= 1)
                int_step = 1;

Can anyone explain why this happens and what the correct way is to solve this?

Martin
  • 397
  • 3
  • 16
  • not sure I understand what you're saying is happening but it sounds like Mid Point Rounding Away From Zero issue – chris-crush-code May 26 '17 at 18:32
  • 2
    Why aren't you using an int from 1 to 100? doubles and floats have rounding issues, and if you need precision you should use another type, like decimal. – Magnetron May 26 '17 at 18:35

0 Answers0