I have created a 2D array to store double values,on executing the below code the decimal range of some values are getting modified unexpectedly !
package learnJavaProj;
public class SquareRootin2D {
public static void main(String[] args) {
double [][] arr = new double[10][10];
double f= 0.00;
double g=0.00;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
arr[i][j]=0.00;
}
}
for (int i=0;i<10;i++)
{
arr[i][0]=f;
arr[0][i]=g;
g=g+0.10;
f++;
}
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println("\n");
}
}
}
Output :
0.0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6 0.7 0.7999999999999999 0.8999999999999999
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
6.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
7.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
8.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
9.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
This kind of fluctuation appeared while using float too ! Kindly explain this chaos. Thank You !