as77c
as77c

Reputation: 155

equality of two double array

i have problem with testing two double arrays if they are equals i wrote this method

    public static boolean equalsArray(double[]a,double[]b){
    if(a.length!=b.length)
        return false;
    else{
        for(int i=0;i<a.length;i++)
        if(a[i]!=b[i])
            return false;
    }
    return true;
}

and every time when i use it i get false!! for example:

double []a={1.7,6.9};
double []b={1.7,6.9};
System.out.println(equalsArray(a,b));

it works fine with int arrays but with double it doesn't

Thanks so much

Upvotes: 0

Views: 699

Answers (1)

Trying
Trying

Reputation: 14278

Best way to compare double values is:

double a = 1.000000;
    double b = 1.000009;
    if(Math.abs(a-b)<=0.00000001){
        System.out.println("equal");
    }

0.00000001 is called epsilon and you can adjust it accordingly.

Upvotes: 1

Related Questions