why does 4.6+.2==4.7999997

I'm unconventionally navigating a 2-d array by adding.2 to an arbitrary variable. it cut down on a lot of code and worked really well until right after 4.6... also I made a small example of a for loop adding the same way... it produces the same result.

float hi=0;
for (int i=0;i<25;i++) {
  hi+=.2;
  println(hi);
}

these are the last 3 sets of println()'s it's working

4.6
[0] -1
[1] 0
[2] 649
[3] 382
[4] 649
[5] 382
lines omitted
[4] 649
[5] 382
[6] 649
[7] 382
[8] 649
[9] 383
[0] 649
[1] 383
[2] 649
[3] 383
[4] 649
[5] 383
[6] -50
[7] -50
[8] -50
[9] -50
it's working
4.7999997
[0] -1
[1] 0
[2] 649
[3] 382
[4] 649
[5] 382
lines omitted
[0] 649
[1] 383
[2] 649
[3] 383
[4] 649
[5] 649
[6] 383
[7] -50
[8] -50
[9] -50
it's working
4.9999995
[0] -1
[1] 0
[2] 649
[3] 382
[4] 649
[5] 382
[6] 649
lines omitted
[9] 383
[0] 649
[1] 383
[2] 649
[3] 383
[4] 649
[5] 649<-------- problem arises here
[6] 383
[7] 649
[8] 383
[9] -50
java.lang.NullPointerException
    at processing.mode.java.runner.Runner.findException(Runner.java:926)
    at processing.mode.java.runner.Runner.reportException(Runner.java:871)
    at processing.mode.java.runner.Runner.exceptionEvent(Runner.java:797)
    at processing.mode.java.runner.Runner$2.run(Runner.java:686)

Answers

Sign In or Register to comment.