DefaultFloatingPointTolerance
DefaultFloatingPointToleranceAttribute sets the default tolerance for floating-point equality comparisons (float and double) unless a comparison explicitly specifies a tolerance.
You can apply it at method, fixture, or assembly scope.
Constructor
DefaultFloatingPointToleranceAttribute(double tolerance)
| Parameter | Type | Description |
|---|---|---|
tolerance |
double |
The default tolerance used for floating-point comparisons when no explicit tolerance is specified. |
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ✅ | ✅ |
Note
When applied at fixture or assembly level, this default tolerance applies to all contained tests unless overridden by a more specific scope or by an explicit tolerance in an assertion.
Examples
Fixture Default Tolerance
[TestFixture]
[DefaultFloatingPointTolerance(1)]
public class ToleranceFixtureTests
{
[Test]
public void ComparisonUsingFixtureDefaultTolerance()
{
Assert.That(1f, Is.EqualTo(2));
}
[Test]
public void ExplicitToleranceOverridesDefault()
{
Assert.That(1f, Is.EqualTo(1.2f).Within(0.3));
}
}
Method Override
[TestFixture]
[DefaultFloatingPointTolerance(1)]
public class ToleranceMethodOverrideTests
{
[Test]
[DefaultFloatingPointTolerance(2)]
public void MethodToleranceOverridesFixtureTolerance()
{
Assert.That(2f, Is.EqualTo(4));
}
}
Notes
- This attribute affects default tolerance for floating-point equality comparisons only.
- Integer comparisons are not affected.
- An explicit tolerance in an assertion (for example,
.Within(...)) overrides the default tolerance.
See Also
- Assert.AreEqual
- EqualConstraint — numeric
Withintolerances and floating-point comparison behavior (including the Comparing floating-point values section)