The program I am writing for my class requires a row that counts by 0.1 from 0.5 to 42.9 along with a few other rows. Since doubles are not exact the program counts by 0.1 until 0.7 then the next number becomes 0.799999999 rather than 8. I am a little iffy on the code that corrects this. I want to say it has something to do with Math.abs and EPS but i'm not sure. Any advice would be appreciated.
{
System.out.println(" ");
final double MAX2 = 43;
for (double row = 0.5; row<MAX2; row+=0.1)
{
System.out.print(row);
if(row != MAX2 -1)
{System.out.print(",");
}//SOP COMMA
}//SOP ROW3
}//.5,42.9
the end result is
0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999,1.0999999999999999,1.2,1.3,1.4000000000000001,1.5000000000000002,1.6000000000000003,1.7000000000000004,1.8000000000000005,1.9000000000000006,2.0000000000000004,2.1000000000000005,2.2000000000000006,2.3000000000000007,2.400000000000001,2.500000000000001,2.600000000000001,2.700000000000001,2.800000000000001,2.9000000000000012 etc.