ff_i_maths

Math functions.

Extended mathematical helpers not available in the standard NWScript API: clamp, wrap, integer power, and sign.

int clamp(int nValue, int nMin, int nMax)
Parameters:
  • nValue – Value to clamp.

  • nMin – Lower bound.

  • nMax – Upper bound.

Clamp nValue to [nMin, nMax].

float fclamp(float fValue, float fMin, float fMax)
Parameters:
  • fValue – Value to clamp.

  • fMin – Lower bound.

  • fMax – Upper bound.

Clamp fValue to [fMin, fMax].

int max(int a, int b)
Parameters:
  • a – First integer.

  • b – Second integer.

Returns the larger of two integers.

int min(int a, int b)
Parameters:
  • a – First integer.

  • b – Second integer.

Returns the smaller of two integers.

float fmax(float a, float b)
Parameters:
  • a – First float.

  • b – Second float.

Returns the larger of two floats.

float fmin(float a, float b)
Parameters:
  • a – First float.

  • b – Second float.

Returns the smaller of two floats.

int sign(int x)
Parameters:
  • x – An integer.

Returns 1 if x is positive, -1 if negative, or 0 if zero.

int fsign(float f)
Parameters:
  • f – A float.

Returns 1 if f is positive, -1 if negative, or 0 if zero.

int round(float f)
Parameters:
  • f – A float.

Returns f rounded to the nearest whole number.

int floor(float f)
Parameters:
  • f – A float.

Returns f rounded down to the nearest whole number.

int ceil(float f)
Parameters:
  • f – A float.

Returns f rounded up to the nearest whole number.

int trunc(float f)
Parameters:
  • f – A float.

Returns f truncated toward zero.

float frac(float f)
Parameters:
  • f – A float.

Returns the fractional part of f.

float fmod(float a, float b)
Parameters:
  • a – Dividend.

  • b – Divisor.

Returns a % b.

Source code

// @code


// -----------------------------------------------------------------------------

//! @brief Clamp nValue to [nMin, nMax].
//! @param nValue Value to clamp.
//! @param nMin Lower bound.
//! @param nMax Upper bound.
int clamp(int nValue, int nMin, int nMax);
int clamp(int nValue, int nMin, int nMax)
{
  return (nValue < nMin) ? nMin : ((nValue > nMax) ? nMax : nValue);
}


// -----------------------------------------------------------------------------

//! @brief Clamp fValue to [fMin, fMax].
//! @param fValue Value to clamp.
//! @param fMin Lower bound.
//! @param fMax Upper bound.
float fclamp(float fValue, float fMin, float fMax);
float fclamp(float fValue, float fMin, float fMax)
{
  return (fValue < fMin) ? fMin : ((fValue > fMax) ? fMax : fValue);
}


// -----------------------------------------------------------------------------

//! @brief Returns the larger of two integers.
//! @param a First integer.
//! @param b Second integer.
int max(int a, int b);
int max(int a, int b)
{
  return (b > a) ? b : a;
}


// -----------------------------------------------------------------------------

//! @brief Returns the smaller of two integers.
//! @param a First integer.
//! @param b Second integer.
int min(int a, int b);
int min(int a, int b)
{
  return (b > a) ? a : b;
}


// -----------------------------------------------------------------------------

//! @brief Returns the larger of two floats.
//! @param a First float.
//! @param b Second float.
float fmax(float a, float b);
float fmax(float a, float b)
{
  return (b > a) ? b : a;
}


// -----------------------------------------------------------------------------

//! @brief Returns the smaller of two floats.
//! @param a First float.
//! @param b Second float.
float fmin(float a, float b);
float fmin(float a, float b)
{
  return (b > a) ? a : b;
}


// -----------------------------------------------------------------------------

//! @brief Returns 1 if x is positive, -1 if negative, or 0 if zero.
//! @param x An integer.
int sign(int x);
int sign(int x)
{
  return (x > 0 ? 1 : (x < 0 ? -1 : 0));
}


// -----------------------------------------------------------------------------

//! @brief Returns 1 if f is positive, -1 if negative, or 0 if zero.
//! @param f A float.
int fsign(float f);
int fsign(float f)
{
  return (f > 0.0f ? 1 : (f < 0.0f ? -1 : 0));
}


// -----------------------------------------------------------------------------

//! @brief Returns f rounded to the nearest whole number.
//! @param f A float.
int round(float f);
int round(float f)
{
  return FloatToInt(f + (f < 0.0f ? -0.5f : 0.5f));
}


// -----------------------------------------------------------------------------

//! @brief Returns f rounded down to the nearest whole number.
//! @param f A float.
int floor(float f);
int floor(float f)
{
  if (f >= 0.0f)
    return FloatToInt(f);

  if (IntToFloat(FloatToInt(f)) == f)
    return FloatToInt(f);

  return round(f) - (f < IntToFloat(round(f)) ? 1 : 0);
}


// -----------------------------------------------------------------------------

//! @brief Returns f rounded up to the nearest whole number.
//! @param f A float.
int ceil(float f);
int ceil(float f)
{
  if (f <= 0.0f)
    return FloatToInt(f);

  if (IntToFloat(FloatToInt(f)) == f)
    return FloatToInt(f);

  return FloatToInt(f) + 1;
}


// -----------------------------------------------------------------------------

//! @brief Returns f truncated toward zero.
//! @param f A float.
int trunc(float f);
int trunc(float f)
{
  return (f >= 0.0f) ? floor(f) : ceil(f);
}


// -----------------------------------------------------------------------------

//! @brief Returns the fractional part of f.
//! @param f A float.
float frac(float f);
float frac(float f)
{
  return f - trunc(f);
  return (f >= 0.0f) ? (f - floor(f)) : (f - ceil(f));
}


// -----------------------------------------------------------------------------

//! @brief Returns a % b.
//! @param a Dividend.
//! @param b Divisor.
float fmod(float a, float b);
float fmod(float a, float b)
{
  return a - b * trunc(a / b);
}