0

Following code gave an incorrect answers. What is a best way to correct it? I am new to programming.

public class test {
  public static void main(String[] args) {

    double[] array = new double[] {5, 4, 6, 7, 7, 6};
    double t= 6.4;  

    for (int k = 0; k < array.length; ++k){
      double d= array[k]-t;
      System.out.println("d is="+d); 
    }
  }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Emalka
  • 381
  • 2
  • 4
  • 16
  • It gave following answers, d is=-1.4000000000000004 d is=-2.4000000000000004 d is=-0.40000000000000036 d is=0.5999999999999996 d is=0.5999999999999996 d is=-0.40000000000000036 But correct answers should be, d is=-1.4 d is=-2.4 d is=-0.4 d is=-0.4 d is=0.6 d is=0.6 d is=-0.4 – Emalka Jan 04 '16 at 07:38
  • try debugging the code, you'll know how, why and where the values are being assigned. – pri Jan 04 '16 at 07:40
  • 1
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Mat Jan 04 '16 at 07:44

0 Answers0