Version: 2018.4
LanguageEnglish
  • C#

Utils.AreFloatsEqual

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static bool AreFloatsEqual(float expected, float actual, float allowedRelativeError);

Parameters

expected The expected value.
actual Actual value to be compared with the expected value.
allowedRelativeError The relative error to be used in relative epsilon comparison.

Returns

bool Returns true if the actual value is equaivalent to the expected value.

Description

Relative epsilon comparison of two float values for equality.

This method does the relative epsilon comparison of two floating point numbers for equality. Supply the relative error as the third parameter. The relative error is the absolute error divided by the magnitude of the exact value.

using UnityEngine.TestTools.Utils;

using NUnit.Framework;

[TestFixture]

class UtilsTest { [Test]

public void FloatsAreEqual() { float expected = 10e-8f;

float actual = 0f;

float allowedRelativeError = 10e-6f;

Assert.That(Utils.AreFloatsEqual(expected, actual, allowedRelativeError), Is.True); } }