Skip to content

FloatExtensions

Namespace: SideXP.Core

public static class FloatExtensions

Extension functions for float values.

Methods

Approximately(float, float)

public static bool Approximately(float a, float b)

Checks the two float values are close enough to be considered equal. This is meant to prevent float imprecisions.

Parameters

  • a: The first value to compare.
  • b: The second value to compare.

Returns

Returns true if b is equal (or very close) to a.

Remarks

This overload relies on Approximately, which uses a relative tolerance that scales with the magnitude of the compared values. For an explicit, absolute tolerance, use Approximately.

Approximately(float, float, float)

public static bool Approximately(float a, float b, float epsilon)

Checks the two float values are within a given absolute tolerance of each other.

Parameters

  • a: The first value to compare.
  • b: The second value to compare.
  • epsilon: The absolute tolerance. Its absolute value is used, so its sign doesn't matter.

Returns

Returns true if a is within epsilon of b (bounds included).

Remarks

Unlike Approximately, this overload uses an absolute tolerance: it returns true if a is within epsilon of b, bounds included. The sign of epsilon is ignored, and an epsilon of 0 still considers strictly equal values as approximately equal.

Ratio(float, float, float)

public static float Ratio(float value, float min, float max)

Remaps the given value from its range to a range between 0 and 1. If the input value is out of the given range, it's clamped between 0 and 1.

Parameters

  • value: The value to remap.
  • min: The lower bound of the value's current range.
  • max: The upper bound of the value's current range.

Returns

Returns the remapped value, clamped between 0 and 1.

Percents(float, float, float)

public static float Percents(float value, float min, float max)

Remaps the given value from its range to a range between 0 and 1. If the input value is out of the given range, it's clamped between 0 and 1.

Parameters

  • value: The value to remap.
  • min: The lower bound of the value's current range.
  • max: The upper bound of the value's current range.

Returns

Returns the remapped value, clamped between 0 and 1.

Clamp(float, float, float)

public static float Clamp(float value, float min, float max)

Clamps this value between given min and max.

Parameters

  • value: The value to clamp.
  • min: The lower bound of the value.
  • max: The upper bound of the value.

Returns

Returns the clamped value.

Pad(float, int, int, char)

public static string Pad(float value, int length, int decimals = 0, char padChar = '0')

Pads a value as string with leading characters.

Parameters

  • value: The value to pad.
  • length: The expected length of the string.
  • decimals: The number of decimals to display.
  • padChar: The character used to pad the value.

Returns

Returns the padded value as string.