Java Float Number Equal floatEqual(float f1, float f2)

Here you can find the source of floatEqual(float f1, float f2)

Description

Returns whether the two specified float value are equal.

License

Open Source License

Parameter

Parameter Description
f1 The first float to compare
f2 The second float to compare

Return

true if the two values are equal, otherwise false

Declaration

public static boolean floatEqual(float f1, float f2) 

Method Source Code

//package com.java2s;
/*//from   w  w  w . j a va  2s .  c  o m
 *   Copyright 2011 Calytrix Technologies
 *
 *   This file is part of Calytrix Disco.
 *
 *   Calytrix Disco is free software; you can redistribute it and/or modify
 *   it under the terms of the Common Developer and Distribution License (CDDL) 
 *   as published by Sun Microsystems. For more information see the LICENSE file.
 *   
 *   Use of this software is strictly AT YOUR OWN RISK!!!
 *   If something bad happens you do not have permission to come crying to me.
 *   (that goes for your lawyer as well)
 *
 */

public class Main {
    public static final double FP_EQUALITY_THRESHOLD = 1e-5;

    /**
     * Returns whether the two specified float value are equal. The two values
     * will be considered equal if the absolute difference between the values
     * is within a predefined threshold.
     * 
     * @param f1 The first float to compare
     * @param f2 The second float to compare
     * 
     * @return true if the two values are equal, otherwise false
     */
    public static boolean floatEqual(float f1, float f2) {
        float absDiff = Math.abs(f1 - f2);
        return absDiff < FP_EQUALITY_THRESHOLD;
    }
}

Related

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