Thursday, 21 July 2022

How to check two float values equality?

Approach 1: Using == operator.

== operator return true, if the both the primitive operands has same value, else false. == operator return true, when two references point to same object, else false.

 

FloatEqualityCheck1.java

package com.sample.app.numbers;

public class FloatEqualityCheck1 {
	public static void main(String[] args) {
		float d1 = 12.34f;
		float d2 = 12.34f;
		float d3 = 12.3333339f;

		System.out.printf("d1 = %f", d1);
		System.out.printf("\nd2 = %f", d2);
		System.out.printf("\nd3 = %f", d3);

		System.out.printf("\n(%f == %f) : %s", d1, d2, (d1 == d2));
		System.out.printf("\n(%f == %f) : %s", d1, d3, (d1 == d3));

		// 0.0 == -0.0
		float d4 = 0.0f;
		float d5 = -0.0f;
		System.out.printf("\n\n(%f == %f) : %s", d4, d5, (d4 == d5));

		float d6 = Float.NaN;
		float d7 = Float.NaN;
		System.out.printf("\n\n(%f == %f) : %s", d6, d7, (d6 == d7));

	}
}

 

Output

d1 = 12.340000
d2 = 12.340000
d3 = 12.333334
(12.340000 == 12.340000) : true
(12.340000 == 12.333334) : false

(0.000000 == -0.000000) : true

(NaN == NaN) : false

 

We need to consider below points while using == operator for equality check.

 

a.      (+0.0 == -0.0) evaluates to  true, if you want to consider positive 0 is greater than negative 0, then you can use Double.compare method.

b.       Float.NaN == Float.NaN evaluates to false, use Float.compare method to treat them same.

 

 

If you have two Float objects, then you should not use == operator to check their value equality. == operator return true, when two references point to same object, else false.

 

Example
Float d1 = Float.valueOf(10);
Float d2 = Float.valueOf(10);
Float d3 = d1;

d1 == d2 will evaluate to false, since d1 and d2 point to two different objects.

d1 == d3 will evaluate to true, since d1 and d3 point to same object.

 

How to compare two Float objects using == operator?

Get the float primitive value by calling floatValue() method and use == operator.

 

d1.floatValue() == d2.floatValue()

 

FloatObjectsEqualityCheck1.java

package com.sample.app.numbers;

public class FloatObjectsEqualityCheck1 {

	public static void main(String[] args) {
		Float d1 = Float.valueOf(10);
		Float d2 = Float.valueOf(10);
		Float d3 = d1;

		System.out.printf("d1 = %f", d1);
		System.out.printf("\nd2 = %f", d2);
		System.out.printf("\nd3 = %f", d3);

		System.out.printf("\n\n(d1 == d2) : %s", (d1 == d2));
		System.out.printf("\n(d1 == d3) : %s", (d1 == d3));

		System.out.printf("\n\n(d1.floatValue() == d2.floatValue()) : %s", (d1.floatValue() == d2.floatValue()));

	}

}

Output

d1 = 10.000000
d2 = 10.000000
d3 = 10.000000

(d1 == d2) : false
(d1 == d3) : true

(d1.floatValue() == d2.floatValue()) : true

Approach 2: Using Float.compare method.

 

public static int compare(float f1, float f2)

Compare two float values and return the value 0 if f1 is numerically equal to f2, a value less than 0 if f1 is numerically less than f2 and a value greater than 0 if f1 is numerically greater than f2.

 

Float.compare(0.000000, -0.000000) evaluated to 1

Float.compare method treats 0.0 is greater than to -0.0, so it is evaluated to 1.

 

Float.compare(NaN, NaN) is evaluated to 0.

 

Float.compare method internally use Float.floatToIntBits method to check the equality. Implementation looks like below.

public static int compare(float f1, float f2) {
    if (f1 < f2)
        return -1;           // Neither val is NaN, thisVal is smaller
    if (f1 > f2)
        return 1;            // Neither val is NaN, thisVal is larger

    // Cannot use floatToRawIntBits because of possibility of NaNs.
    int thisBits    = Float.floatToIntBits(f1);
    int anotherBits = Float.floatToIntBits(f2);

    return (thisBits == anotherBits ?  0 : // Values are equal
            (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
             1));                          // (0.0, -0.0) or (NaN, !NaN)
}

Float.floatToIntBits returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.

 


FloatEqualityCheck2.java

package com.sample.app.numbers;

public class FloatEqualityCheck2 {
	public static void main(String[] args) {
		float d1 = 12.34f;
		float d2 = 12.34f;
		float d3 = 12.3333339f;

		System.out.printf("d1 = %f", d1);
		System.out.printf("\nd2 = %f", d2);
		System.out.printf("\nd3 = %f", d3);

		System.out.printf("\nFloat.compare(%f, %f) : %s", d1, d2, (Float.compare(d1, d2)));
		System.out.printf("\nFloat.compare(%f, %f) : %s", d1, d3, (Float.compare(d1, d3)));

		// 0.0 == -0.0
		float d4 = 0.0f;
		float d5 = -0.0f;
		System.out.printf("\nFloat.compare(%f, %f) : %s", d4, d5, (Float.compare(d4, d5)));

		float d6 = Float.NaN;
		float d7 = Float.NaN;
		System.out.printf("\nFloat.compare(%f, %f) : %s", d6, d7, (Float.compare(d6, d7)));

	}

}

 

Output

d1 = 12.340000d2 = 12.340000d3 = 12.333334
Float.compare(12.340000, 12.340000) : 0
Float.compare(12.340000, 12.333334) : 1
Float.compare(0.000000, -0.000000) : 1
Float.compare(NaN, NaN) : 0

Approach 3: Using Float#equals method. You can use this method when you have two Float objects to compare.

public boolean equals(Object obj) {
    return (obj instanceof Float)
           && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
}

Float equals method also used floatToIntBits internally, functionality is similar to Float.compare method on NaN,  0.0 and -0.0 checks.

 

Find the below working application.

 

FloatEqualsMethod.java

package com.sample.app.numbers;

public class FloatEqualsMethod {
    public static void main(String[] args) {
        Float d1 = Float.valueOf(10.01f);
        Float d2 = Float.valueOf(10.01f);
        Float d3 = Float.valueOf(10.0099999f);

        System.out.printf("%f.equals(%f) : %s", d1, d2, d1.equals(d2));
        System.out.printf("\n%f.equals(%f) : %s", d1, d2, d1.equals(d3));

        Float d4 = Float.valueOf(0.0f);
        Float d5 = Float.valueOf(-0.0f);
        System.out.printf("\n\n%f.equals(%f) : %s", d4, d5, d4.equals(d5));

        Float d6 = Float.NaN;
        Float d7 = Float.NaN;
        System.out.printf("\n\n%f.equals(%f) : %s", d6, d7, d6.equals(d7));
    }

}

Output

10.010000.equals(10.010000) : true
10.010000.equals(10.010000) : true

0.000000.equals(-0.000000) : false

NaN.equals(NaN) : true


 

You may like

Interview Questions

Why should we restrict direct access to instance properties?

Closeable vs AutoCloseable in Java

What is the behaviour of static final method in Java?

How to check two double values for equality?

Why to do explicit type casting from double to float conversion?

When is a class or interface is initialized or loaded in Java?

No comments:

Post a Comment