UKismetMathLibrary::SafeDivide
#include "Kismet/KismetMathLibrary.h"
Access: public
Specifiers: staticBlueprintPureUFUNCTION
Description
Divides A by B, returning 0 when B is zero instead of producing infinity or NaN. Use this whenever the denominator may be zero to avoid propagating invalid values.
Caveats & Gotchas
- • The zero-denominator fallback is always 0 — there is no way to specify a custom fallback value through this function. If your logic needs a non-zero default, apply it after the call.
- • This only guards against exact-zero B. Very small denominators (near-zero but not zero) still produce very large results — consider adding a small-epsilon guard if near-zero inputs are possible.
Signature
static ENGINE_API double SafeDivide(double A, double B); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| A | double | Numerator. | — |
| B | double | Denominator. | — |
Return Type
double Example
Normalize value against a total C++
float Fraction = UKismetMathLibrary::SafeDivide(CurrentHealth, MaxHealth);
// Fraction is 0 if MaxHealth is 0 rather than NaN Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?