RealDocs

FMath::Clamp

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

Description

Clamps a value between a minimum and maximum, inclusive on both ends. Works with any comparable type including int32, float, and double.

Signature

template< class T >
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)

Parameters

Name Type Description Default
X T The value to clamp.
MinValue T The minimum allowed value (inclusive).
MaxValue T The maximum allowed value (inclusive).

Return Type

T

Caveats & Gotchas

  • The result is inclusive on both bounds — Clamp(5, 0, 5) returns 5, not an error.
  • Passing MinValue > MaxValue produces undefined behaviour. No assertion is performed in shipping builds.
  • When mixing float and double arguments, UE promotes via MIX_FLOATS_3_ARGS — prefer explicit casts in performance-critical code to avoid accidental double promotion.

Example

Clamp health and a normalized alpha C++
// Clamp integer health
Health = FMath::Clamp(Health - Damage, 0, MaxHealth);

// Clamp a normalised blend alpha
float Alpha = FMath::Clamp(ElapsedTime / Duration, 0.0f, 1.0f);

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.