RealDocs

FMath::IsNearlyZero

function Core Since unknown
#include "Math/UnrealMathUtility.h"
Access: public Specifiers: static

Description

Returns true if a floating-point value is within ErrorTolerance of zero. Equivalent to FMath::Abs(Value) <= ErrorTolerance.

Signature

static UE_FORCEINLINE_HINT bool IsNearlyZero(float Value, float ErrorTolerance = UE_SMALL_NUMBER)

Parameters

Name Type Description Default
Value float The value to test.
ErrorTolerance float Maximum absolute value considered 'nearly zero'. Defaults to UE_SMALL_NUMBER (1e-8). UE_SMALL_NUMBER

Return Type

bool

Caveats & Gotchas

  • The default tolerance UE_SMALL_NUMBER (1e-8) is extremely tight. For gameplay use (e.g. checking if velocity is effectively zero), UE_KINDA_SMALL_NUMBER (1e-4) or a domain-specific epsilon is usually more appropriate.
  • FVector has its own IsNearlyZero() member that checks all three components independently — prefer that over calling FMath::IsNearlyZero on each component separately.

Example

Guard a division and check if movement has stopped C++
// Avoid divide-by-zero before normalising
if (!FMath::IsNearlyZero(Magnitude))
{
	Direction = RawVector / Magnitude;
}

// Check if the character has effectively stopped
if (FMath::IsNearlyZero(GetVelocity().Size(), 1.0f))
{
	OnMovementStopped();
}

Tags

Version History

Introduced in: unknown

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.