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

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

Parameters

expectedThe expected value.
actualActual value to be compared with the expected value.
allowedRelativeErrorThe 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); } }

Did you find this page useful? Please give it a rating: