How do I set float to special values? (inf, -inf, NaN)

When solving minimization problems (which actor is closest to this location?) I’d like to be able to initialize my stored “minimum found” value to infinity as per general programming best practices. I can make do by setting it to something around a billion, but I’d rather do it the proper way. Knowing how to set other special values would be useful as well.

You don’t. There is really no need to. Perform your first distance test and set that value as “Closest”. Then compare against that baseline in all subsequent tests. If the new test is a better match, override your current value and continue with remaining tests.

I thought of that possibility, but it requires a special first case which makes the blueprint messier and more annoying to maintain.

I can still get things done without it. I’d just like to do them right. If a dev says it’s not possible, then that will still be a useful answer.

This is an old question but I am in the same boat. The exclusion of these in Blueprints seems very odd. They exist for a reason after all. In my case I have a function that calculates the semi major axis of an orbit. If an orbit is a parabola then the proper way to represent that SMA is with an infinity value. Easy to do in code, but apparently impossible in Blueprint.

Hi ,

Your question is valid and as John stated, they do exist for a reason and you want to be able to set a float or double to NaN for multiple reasons, including for instance to check if the variable has been initialized or not.

I know your question is Blueprint-related, but in Unreal/C++, you do have the NAN value that you can assign to a float or a double and you do have the isnan() method that you can use. I have not tried it, but provided UE4 does not override the value in BP, you could simply create a class Toto (the come back) with as header:

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Math")
		float BP_NAN = NAN;

	UFUNCTION(BlueprintCallable, Category = "Math")
		bool (float Value);

And in the cpp file:

bool Toto::(float Value)
{
    return isnan(Value);
}

Then in BP, you should be able to both have a NAN value that you can access with BP_NAN, and a method to test if it is a NaN value: ().

Hope that help,
Bests

Thank you for this. I just came of this idea and found your post. NaN for initialization check feels clean af (I used to use BIG_NUMBER etc.). Some naysayers are just irresponsible and plain dumb, thinking their preschool workaround is somewhat special.

1 Like

Here’s a quick set of functions I use in the header of a C++ Blueprint function library to implement IsNAN and IsFinite checks:

UFUNCTION(BlueprintPure, DisplayName = "IsNAN", Category = "MyGameBlueprintLibrary | Math", meta = (ToolTip = "Returns whether the supplied float is not a number."))
static bool BlueprintIsNAN(const float TestValue) { return FMath::IsNaN(TestValue); }

UFUNCTION(BlueprintPure, DisplayName = "IsFinite", Category = "MyGameBlueprintLibrary | Math", meta = (ToolTip = "Returns whether the supplied float is finite."))
static bool BlueprintIsFinite(const float TestValue) { return FMath::IsFinite(TestValue); }

UFUNCTION(BlueprintPure, Category = "MyGameBlueprintLibrary | Math", meta = (ToolTip = "Returns the supplied float if it's valid, zero if it isn't."))
static float ZeroIfInvalid(const float TestValue) { return (FMath::IsNaN(TestValue) || !FMath::IsFinite(TestValue)) ? 0.0f : TestValue; }

UFUNCTION(BlueprintPure, Category = "MyGameBlueprintLibrary | Math", meta = (ToolTip = "Returns whether the supplied transform contains an invalid value (NaN)."))
static bool TransformContainsNaN(const FTransform& TestTransform) { return TestTransform.ContainsNaN(); }

UFUNCTION(BlueprintPure, Category = "MyGameBlueprintLibrary | Math", meta = (ToolTip = "Returns whether the supplied vector contains an invalid value (NaN)."))
static bool VectorContainsNaN(const FVector& TestV) { return TestV.ContainsNaN(); }

UFUNCTION(BlueprintPure, Category = "MyGameBlueprintLibrary | Math", meta = (ToolTip = "Returns whether the supplied rotator contains an invalid value (NaN)."))
static bool RotatorContainsNaN(const FRotator& TestRot) { return TestRot.ContainsNaN(); }
1 Like