UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MathUtils.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include <cstdint>
7#include <math.h>
8#include <string.h>
9
10namespace uLang
11{
12
13class CMath
14{
15public:
16
18 template<typename T>
19 ULANG_FORCEINLINE static constexpr T Max(const T A, const T B)
20 {
21 return (A>=B) ? A : B;
22 }
23
25 template<typename T>
26 ULANG_FORCEINLINE static constexpr T Min(const T A, const T B)
27 {
28 return (A<=B) ? A : B;
29 }
30
32 template<typename T>
33 ULANG_FORCEINLINE static constexpr T Clamp(const T X, const T Min, const T Max)
34 {
35 return X < Min ? Min : (X < Max ? X : Max);
36 }
37
39 template <class T>
40 ULANG_FORCEINLINE static constexpr bool IsPowerOf2(const T X)
41 {
42 return X > 0 && (X & (X - 1)) == 0;
43 }
44
46 ULANG_FORCEINLINE static float Loge(float Value)
47 {
48 return ::logf(Value);
49 }
50
51 static ULANGCORE_API double Extensionalize(double Value);
52
53 static ULANGCORE_API double ToFloat(int64_t Value);
54
55 // Arithmetic operations in a non-fast-math environment (IEEE compliant).
56 // These can (mostly) be removed when callers are guaranteed to not be
57 // compiled with fast-math or similar enabled.
58 static ULANGCORE_API double FloatAdd(double Left, double Right);
59 static ULANGCORE_API double FloatSubtract(double Left, double Right);
60 static ULANGCORE_API double FloatMultiply(double Left, double Right);
61 static ULANGCORE_API double FloatDivide(double Left, double Right);
62
63 // FP special constants: NaN and infinity.
65 {
66 static_assert(sizeof(double) == sizeof(uint64_t));
67 double Result;
68 memcpy(&Result, &Int, sizeof(double));
69 return Result;
70 }
71 ULANG_FORCEINLINE static double FloatNaN() { return ReinterpretInt64AsDouble(0x7ff8'0000'0000'0000); }
72 ULANG_FORCEINLINE static double FloatInfinity() { return ReinterpretInt64AsDouble(0x7ff0'0000'0000'0000); }
73
74 // Predicates for different FP specials.
75 static ULANGCORE_API bool FloatIsFinite(double Value);
76 static ULANGCORE_API bool FloatIsInfinite(double Value);
77 static ULANGCORE_API bool FloatIsNaN(double Value);
78
79 // We use an ordering relationship different from the default IEEE float
80 // ordering (because we require NaNs to compare equal to each other).
81 static ULANGCORE_API bool FloatEqual(double Left, double Right);
82 static ULANGCORE_API bool FloatLess(double Left, double Right);
83 static ULANGCORE_API bool FloatLessEqual(double Left, double Right);
84
85 // The remaining relations can be inferred from the relations above.
86
87 ULANG_FORCEINLINE static bool FloatNotEqual(double Left, double Right)
88 {
89 return !FloatEqual(Left, Right);
90 }
91
92 ULANG_FORCEINLINE static bool FloatGreater(double Left, double Right)
93 {
94 return FloatLess(Right, Left);
95 }
96
97 ULANG_FORCEINLINE static bool FloatGreaterEqual(double Left, double Right)
98 {
99 return FloatLessEqual(Right, Left);
100 }
101
102 // Ranking function that turns a double into an int64 that defines a total order
103 // compatible with the ordering implied for floats, to be precise
104 //
105 // FloatLess(a,b) => FloatRanking(a) < FloatRanking(b)
106 // FloatEqual(a,b) <=> FloatRanking(a) == FloatRanking(b)
107 //
108 // For FloatLess(a,b), we only have implication in one direction because when a single
109 // NaN is involved, the strict "less" ordering relationship is partial. In the total
110 // order implied by FloatRanking, NaN compares larger than all other floats. Unlike
111 // normal IEEE semantics, NaNs compare equal to each other in our ordering, so for
112 // FloatEqual we have full equivalence.
113 //
114 // FloatRanking can be used directly or as a key for sorted maps and hashes.
116};
117
118}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
#define ULANG_FORCEINLINE
Definition Common.h:188
memcpy(InputBufferBase, BinkBlocksData, BinkBlocksSize)
Definition MathUtils.h:14
static ULANGCORE_API int64_t FloatRanking(double Value)
Definition MathUtils.cpp:168
static ULANGCORE_API bool FloatLess(double Left, double Right)
Definition MathUtils.cpp:144
static ULANG_FORCEINLINE bool FloatGreater(double Left, double Right)
Definition MathUtils.h:92
static ULANGCORE_API double FloatAdd(double Left, double Right)
Definition MathUtils.cpp:80
static ULANGCORE_API double ToFloat(int64_t Value)
Definition MathUtils.cpp:74
static ULANG_FORCEINLINE float Loge(float Value)
Computes natural logarithm.
Definition MathUtils.h:46
static ULANGCORE_API bool FloatIsInfinite(double Value)
Definition MathUtils.cpp:107
static ULANGCORE_API bool FloatIsFinite(double Value)
Definition MathUtils.cpp:102
static ULANG_FORCEINLINE constexpr bool IsPowerOf2(const T X)
Checks if a number is a power of two.
Definition MathUtils.h:40
static ULANG_FORCEINLINE double FloatNaN()
Definition MathUtils.h:71
static ULANG_FORCEINLINE constexpr T Clamp(const T X, const T Min, const T Max)
Clamps X to be between Min and Max, inclusive.
Definition MathUtils.h:33
static ULANG_FORCEINLINE bool FloatGreaterEqual(double Left, double Right)
Definition MathUtils.h:97
static ULANG_FORCEINLINE constexpr T Max(const T A, const T B)
Returns higher value in a generic way.
Definition MathUtils.h:19
static ULANGCORE_API bool FloatIsNaN(double Value)
Definition MathUtils.cpp:116
static ULANG_FORCEINLINE bool FloatNotEqual(double Left, double Right)
Definition MathUtils.h:87
static ULANGCORE_API bool FloatLessEqual(double Left, double Right)
Definition MathUtils.cpp:152
static ULANGCORE_API double FloatMultiply(double Left, double Right)
Definition MathUtils.cpp:90
static ULANG_FORCEINLINE double FloatInfinity()
Definition MathUtils.h:72
static ULANG_FORCEINLINE double ReinterpretInt64AsDouble(uint64_t Int)
Definition MathUtils.h:64
static ULANGCORE_API bool FloatEqual(double Left, double Right)
Definition MathUtils.cpp:131
static ULANGCORE_API double FloatDivide(double Left, double Right)
Definition MathUtils.cpp:95
static ULANGCORE_API double Extensionalize(double Value)
Definition MathUtils.cpp:69
static ULANG_FORCEINLINE constexpr T Min(const T A, const T B)
Returns lower value in a generic way.
Definition MathUtils.h:26
static ULANGCORE_API double FloatSubtract(double Left, double Right)
Definition MathUtils.cpp:85
Definition VVMEngineEnvironment.h:23