OpenTTD Source  1.11.0-beta2
math_func.hpp File Reference

Go to the source code of this file.

Functions

template<typename T >
static T abs (const T a)
 Returns the absolute value of (scalar) variable. More...
 
template<typename T >
static T Align (const T x, uint n)
 Return the smallest multiple of n equal or greater than x. More...
 
template<typename T >
static T * AlignPtr (T *x, uint n)
 Return the smallest multiple of n equal or greater than x Applies to pointers only. More...
 
template<typename T >
static T Clamp (const T a, const T min, const T max)
 Clamp a value between an interval. More...
 
static int Clamp (const int a, const int min, const int max)
 Clamp an integer between an interval. More...
 
static uint ClampU (const uint a, const uint min, const uint max)
 Clamp an unsigned integer between an interval. More...
 
static int32 ClampToI32 (const int64 a)
 Reduce a signed 64-bit int to a signed 32-bit one. More...
 
static uint16 ClampToU16 (const uint64 a)
 Reduce an unsigned 64-bit int to an unsigned 16-bit one. More...
 
template<typename T >
static T Delta (const T a, const T b)
 Returns the (absolute) difference between two (scalar) variables. More...
 
template<typename T >
static bool IsInsideBS (const T x, const size_t base, const size_t size)
 Checks if a value is between a window started at some base point. More...
 
template<typename T >
static bool IsInsideMM (const T x, const size_t min, const size_t max)
 Checks if a value is in an interval. More...
 
template<typename T >
static void Swap (T &a, T &b)
 Type safe swap operation. More...
 
static uint ToPercent8 (uint i)
 Converts a "fract" value 0..255 to "percent" value 0..100. More...
 
static uint ToPercent16 (uint i)
 Converts a "fract" value 0..65535 to "percent" value 0..100. More...
 
int LeastCommonMultiple (int a, int b)
 Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multiple of both a and b. More...
 
int GreatestCommonDivisor (int a, int b)
 Compute greatest common divisor (gcd) of a and b. More...
 
int DivideApprox (int a, int b)
 Deterministic approximate division. More...
 
static uint CeilDiv (uint a, uint b)
 Computes ceil(a / b) for non-negative a and b. More...
 
static uint Ceil (uint a, uint b)
 Computes ceil(a / b) * b for non-negative a and b. More...
 
static int RoundDivSU (int a, uint b)
 Computes round(a / b) for signed a and unsigned b. More...
 
static int DivAwayFromZero (int a, uint b)
 Computes (a / b) rounded away from zero. More...
 
uint32 IntSqrt (uint32 num)
 Compute the integer square root. More...
 

Detailed Description

Integer math functions

Definition in file math_func.hpp.

Function Documentation

◆ abs()

◆ Align()

template<typename T >
static T Align ( const T  x,
uint  n 
)
inlinestatic

Return the smallest multiple of n equal or greater than x.

Note
n must be a power of 2
Parameters
xThe min value
nThe base of the number we are searching
Returns
The smallest multiple of n equal or greater than x

Definition at line 35 of file math_func.hpp.

Referenced by OpenGLSprite::Update().

◆ AlignPtr()

template<typename T >
static T* AlignPtr ( T *  x,
uint  n 
)
inlinestatic

Return the smallest multiple of n equal or greater than x Applies to pointers only.

Note
n must be a power of 2
Parameters
xThe min value
nThe base of the number we are searching
Returns
The smallest multiple of n equal or greater than x
See also
Align()

Definition at line 53 of file math_func.hpp.

◆ Ceil()

static uint Ceil ( uint  a,
uint  b 
)
inlinestatic

Computes ceil(a / b) * b for non-negative a and b.

Parameters
aNumerator
bDenominator
Returns
a rounded up to the nearest multiple of b.

Definition at line 265 of file math_func.hpp.

References CeilDiv().

Referenced by VehicleGroupWindow::UpdateWidgetSize().

◆ CeilDiv()

static uint CeilDiv ( uint  a,
uint  b 
)
inlinestatic

◆ Clamp() [1/2]

static int Clamp ( const int  a,
const int  min,
const int  max 
)
inlinestatic

Clamp an integer between an interval.

This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.

Note
The min value must be less or equal of max or you get some unexpected results.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.
See also
ClampU(uint, uint, uint)

Definition at line 101 of file math_func.hpp.

◆ Clamp() [2/2]

template<typename T >
static T Clamp ( const T  a,
const T  min,
const T  max 
)
inlinestatic

Clamp a value between an interval.

This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.

Note
The min value must be less or equal of max or you get some unexpected results.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.
See also
ClampU(uint, uint, uint)
Clamp(int, int, int)

Definition at line 77 of file math_func.hpp.

Referenced by AnimationBase< IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback >::AnimateTile(), BridgeChangeInfo(), ClickChangeDateCheat(), ClickChangeMaxHlCheat(), EnsureVisibleCaption(), GetAmplitude(), Path::GetCapacityRatio(), GetGlobalVariable(), GetNearbyTileInformation(), GetSavegameFormat(), GetServiceIntervalClamped(), GetTileHeightBelowAircraft(), IndustriesScopeResolver::GetVariable(), AirportScopeResolver::GetVariable(), VehicleScopeResolver::GetVariable(), GenerateLandscapeWindow::OnDropdownSelect(), BuildBridgeWindow::OnInitialPosition(), TooltipsWindow::OnInitialPosition(), CreateScenarioWindow::OnQueryTextFinished(), OrdersWindow::OnQueryTextFinished(), MultiCommodityFlow::PushFlow(), NetworkUDPSocketHandler::ReceiveNetworkGameInfo(), SetDateWindow::SetDateWindow(), AIConfig::SetSetting(), ScriptConfig::SetSetting(), SetStartingYear(), SmallMapWindow::SetZoomLevel(), Train::UpdateAcceleration(), CursorVars::UpdateCursorPositionRelative(), and Scrollbar::UpdatePosition().

◆ ClampToI32()

static int32 ClampToI32 ( const int64  a)
inlinestatic

Reduce a signed 64-bit int to a signed 32-bit one.

This function clamps a 64-bit integer to a 32-bit integer. If the 64-bit value is smaller than the smallest 32-bit integer value 0x80000000 this value is returned (the left one bit is the sign bit). If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF this value is returned. In all other cases the 64-bit value 'fits' in a 32-bits integer field and so the value is casted to int32 and returned.

Parameters
aThe 64-bit value to clamps
Returns
The 64-bit value reduced to a 32-bit value
See also
Clamp(int, int, int)

Definition at line 141 of file math_func.hpp.

Referenced by EngineCostSorter(), EngineRunningCostSorter(), HandleBankruptcyTakeover(), VehicleProfitLastYearSorter(), VehicleProfitThisYearSorter(), VehicleTimeToLiveSorter(), and VehicleValueSorter().

◆ ClampToU16()

static uint16 ClampToU16 ( const uint64  a)
inlinestatic

Reduce an unsigned 64-bit int to an unsigned 16-bit one.

Parameters
aThe 64-bit value to clamp
Returns
The 64-bit value reduced to a 16-bit value
See also
ClampU(uint, uint, uint)

Definition at line 153 of file math_func.hpp.

Referenced by TownScopeResolver::GetVariable(), and FileStringReader::ReadLine().

◆ ClampU()

static uint ClampU ( const uint  a,
const uint  min,
const uint  max 
)
inlinestatic

Clamp an unsigned integer between an interval.

This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.

Note
The min value must be less or equal of max or you get some unexpected results.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.
See also
Clamp(int, int, int)

Definition at line 122 of file math_func.hpp.

Referenced by ConvertFromOldCompanyManagerFace(), VideoDriver::UpdateAutoResolution(), and SavePresetWindow::UpdateWidgetSize().

◆ Delta()

template<typename T >
static T Delta ( const T  a,
const T  b 
)
inlinestatic

Returns the (absolute) difference between two (scalar) variables.

Parameters
aThe first scalar
bThe second scalar
Returns
The absolute difference between the given scalars

Definition at line 170 of file math_func.hpp.

Referenced by DistanceManhattan(), DistanceMax(), DistanceMaxPlusManhattan(), and NPFDistanceTrack().

◆ DivAwayFromZero()

static int DivAwayFromZero ( int  a,
uint  b 
)
inlinestatic

Computes (a / b) rounded away from zero.

Parameters
aNumerator
bDenominator
Returns
Quotient, rounded away from zero

Definition at line 293 of file math_func.hpp.

◆ DivideApprox()

int DivideApprox ( int  a,
int  b 
)

Deterministic approximate division.

Cancels out division errors stemming from the integer nature of the division over multiple runs.

Parameters
aDividend.
bDivisor.
Returns
a/b or (a/b)+1.

Definition at line 57 of file math_func.cpp.

References abs().

◆ GreatestCommonDivisor()

int GreatestCommonDivisor ( int  a,
int  b 
)

Compute greatest common divisor (gcd) of a and b.

Parameters
aFirst number.
bsecond number.
Returns
Greatest common divisor of a and b.

Definition at line 39 of file math_func.cpp.

Referenced by LeastCommonMultiple().

◆ IntSqrt()

uint32 IntSqrt ( uint32  num)

Compute the integer square root.

Parameters
numRadicand.
Returns
Rounded integer square root.
Note
Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

Definition at line 77 of file math_func.cpp.

◆ IsInsideBS()

template<typename T >
static bool IsInsideBS ( const T  x,
const size_t  base,
const size_t  size 
)
inlinestatic

Checks if a value is between a window started at some base point.

This function checks if the value x is between the value of base and base+size. If x equals base this returns true. If x equals base+size this returns false.

Parameters
xThe value to check
baseThe base value of the interval
sizeThe size of the interval
Returns
True if the value is in the interval, false else.

Definition at line 188 of file math_func.hpp.

Referenced by CmdPlantTree(), OrthogonalTileArea::Contains(), NewGRFInspectWindow::HasVariableParameter(), Scrollbar::IsVisible(), MoveBuoysToWaypoints(), and IndustryViewWindow::OnClick().

◆ IsInsideMM()

template<typename T >
static bool IsInsideMM ( const T  x,
const size_t  min,
const size_t  max 
)
inlinestatic

◆ LeastCommonMultiple()

int LeastCommonMultiple ( int  a,
int  b 
)

Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multiple of both a and b.

Parameters
aFirst number.
bsecond number.
Returns
Least common multiple of values a and b.
Note
This function only works for non-negative values of a and b.

Definition at line 24 of file math_func.cpp.

References GreatestCommonDivisor().

Referenced by NWidgetStacked::SetupSmallestSize(), NWidgetHorizontal::SetupSmallestSize(), NWidgetVertical::SetupSmallestSize(), and NWidgetNewGRFDisplay::SetupSmallestSize().

◆ RoundDivSU()

static int RoundDivSU ( int  a,
uint  b 
)
inlinestatic

Computes round(a / b) for signed a and unsigned b.

Parameters
aNumerator
bDenominator
Returns
Quotient, rounded to nearest

Definition at line 276 of file math_func.hpp.

Referenced by ScenarioEditorLandscapeGenerationWindow::DrawWidget(), and UpdateTownGrowCounter().

◆ Swap()

template<typename T >
static void Swap ( T &  a,
T &  b 
)
inlinestatic

Type safe swap operation.

Parameters
avariable to swap with b
bvariable to swap with a

Definition at line 215 of file math_func.hpp.

Referenced by CalcHeightdiff(), IndustryDirectoryWindow::GetCargoTransportedSortValue(), IsInRangeInclusive(), AirportSpec::IsWithinMapBounds(), OrthogonalTileArea::OrthogonalTileArea(), ReverseTrainSwapVeh(), and FlowStat::SwapShares().

◆ ToPercent16()

static uint ToPercent16 ( uint  i)
inlinestatic

Converts a "fract" value 0..65535 to "percent" value 0..100.

Parameters
ivalue to convert, range 0..65535
Returns
value in range 0..100

Definition at line 238 of file math_func.hpp.

◆ ToPercent8()

static uint ToPercent8 ( uint  i)
inlinestatic

Converts a "fract" value 0..255 to "percent" value 0..100.

Parameters
ivalue to convert, range 0..255
Returns
value in range 0..100

Definition at line 227 of file math_func.hpp.

Referenced by IndustryDirectoryWindow::GetCargoTransportedPercentsIfValid().