docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class FloatEqualityComparer

    Use this class to compare two float values for equality with NUnit constraints. Use FloatEqualityComparer.Instance comparer to have the default error value set to 0.0001f. For any other error, use the one argument constructor to create a comparer.

    Inheritance
    object
    FloatEqualityComparer
    Implements
    IEqualityComparer<float>
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: UnityEngine.TestTools.Utils
    Assembly: UnityEngine.TestRunner.dll
    Syntax
    public class FloatEqualityComparer : IEqualityComparer<float>

    Constructors

    FloatEqualityComparer(float)

    Initializes an instance of a FloatEqualityComparer with a custom error value instead of the default 0.0001f.

    Declaration
    public FloatEqualityComparer(float allowedError)
    Parameters
    Type Name Description
    float allowedError

    The custom error value

    Properties

    Instance

    A singleton instance of the comparer with a default error value set to 0.0001f.

    Declaration
    public static FloatEqualityComparer Instance { get; }
    Property Value
    Type Description
    FloatEqualityComparer

    Methods

    Equals(float, float)

    Compares the actual and expected float values for equality using AreFloatsEqual(float, float, float).

    Declaration
    public bool Equals(float expected, float actual)
    Parameters
    Type Name Description
    float expected

    The expected float value used to compare.

    float actual

    The actual float value to test.

    Returns
    Type Description
    bool

    True if the values are equals, false otherwise.

    Examples
     [TestFixture]
     public class FloatsTest
    {
        [Test]
        public void VerifyThat_TwoFloatsAreEqual()
        {
            var comparer = new FloatEqualityComparer(10e-6f);
            var actual = -0.00009f;
            var expected = 0.00009f;
    
            Assert.That(actual, Is.EqualTo(expected).Using(comparer));
    
            // Default relative error 0.0001f
            actual = 10e-8f;
            expected = 0f;
    
            Assert.That(actual, Is.EqualTo(expected).Using(FloatEqualityComparer.Instance));
        }
    }

    GetHashCode(float)

    Serves as the default hash function.

    Declaration
    public int GetHashCode(float value)
    Parameters
    Type Name Description
    float value

    A not null float number.

    Returns
    Type Description
    int

    Returns 0.

    Implements

    IEqualityComparer<T>
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)