Java Float Number Equal equal(float val1, float val2)

Here you can find the source of equal(float val1, float val2)

Description

Checks if two floats are equal based on identity.

License

Apache License

Parameter

Parameter Description
val1 the first value, may be null
val2 the second value, may be null

Return

true if equal

Declaration

public static boolean equal(float val1, float val2) 

Method Source Code

//package com.java2s;
/*/*from  ww w .  ja  v a  2s .c o m*/
 *  Copyright 2001-2014 Stephen Colebourne
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * Checks if two objects are equal handling null.
     * 
     * @param obj1  the first object, may be null
     * @param obj2  the second object, may be null
     * @return true if equal
     */
    public static boolean equal(Object obj1, Object obj2) {
        if (obj1 == obj2) {
            return true;
        }
        if (obj1 == null || obj2 == null) {
            return false;
        }
        if (obj1.getClass().isArray() && obj1.getClass() == obj2.getClass()) {
            if (obj1 instanceof Object[] && obj2 instanceof Object[]) {
                return Arrays.deepEquals((Object[]) obj1, (Object[]) obj2);
            } else if (obj1 instanceof int[] && obj2 instanceof int[]) {
                return Arrays.equals((int[]) obj1, (int[]) obj2);
            } else if (obj1 instanceof long[] && obj2 instanceof long[]) {
                return Arrays.equals((long[]) obj1, (long[]) obj2);
            } else if (obj1 instanceof byte[] && obj2 instanceof byte[]) {
                return Arrays.equals((byte[]) obj1, (byte[]) obj2);
            } else if (obj1 instanceof double[] && obj2 instanceof double[]) {
                return Arrays.equals((double[]) obj1, (double[]) obj2);
            } else if (obj1 instanceof float[] && obj2 instanceof float[]) {
                return Arrays.equals((float[]) obj1, (float[]) obj2);
            } else if (obj1 instanceof char[] && obj2 instanceof char[]) {
                return Arrays.equals((char[]) obj1, (char[]) obj2);
            } else if (obj1 instanceof short[] && obj2 instanceof short[]) {
                return Arrays.equals((short[]) obj1, (short[]) obj2);
            } else if (obj1 instanceof boolean[] && obj2 instanceof boolean[]) {
                return Arrays.equals((boolean[]) obj1, (boolean[]) obj2);
            }
        }
        // this does not handle arrays embedded in objects, such as in lists/maps
        // but you shouldn't use arrays like that, should you?
        return obj1.equals(obj2);
    }

    /**
     * Checks if two floats are equal based on identity.
     * <p>
     * This performs the same check as {@link Float#equals(Object)}.
     * 
     * @param val1  the first value, may be null
     * @param val2  the second value, may be null
     * @return true if equal
     */
    public static boolean equal(float val1, float val2) {
        return Float.floatToIntBits(val1) == Float.floatToIntBits(val2);
    }

    /**
     * Checks if two doubles are equal based on identity.
     * <p>
     * This performs the same check as {@link Double#equals(Object)}.
     * 
     * @param val1  the first value, may be null
     * @param val2  the second value, may be null
     * @return true if equal
     */
    public static boolean equal(double val1, double val2) {
        return Double.doubleToLongBits(val1) == Double.doubleToLongBits(val2);
    }
}

Related

  1. areFloatsEqual(float f1, float f2)
  2. areFloatsEqual(float f1, float f2)
  3. areFloatsEqual(Float one, Float two)
  4. equals(float f1, float f2)
  5. floatEqual(float a, float b, float epsilon)
  6. floatEqual(float f1, float f2)
  7. floatEquals(float a, float b)