View source

class thx.Floats

Available on all platforms

Floats contains helper methods to work with Float values.

Class Fields

static inline var EPSILON(default,null):Float

Constant value employed to see if two Float values are very close.

static inline var TOLERANCE(default,null):Float

static function angleDifference(a:Float, b:Float, turn:Float = 360.0):Float

Returns the angular distance between 2 angles.

static function canParse(s:String):Bool

canParse checks if a string value can be safely converted into a Float value.

static function ceilTo(f:Float, decimals:Int):Float

Rounds a number up to the specified number of decimals.

static inline function clamp(v:Float, min:Float, max:Float):Float

clamp restricts a value within the specified range.

trace(1.3.clamp(0, 1)); // prints 1
trace(0.8.clamp(0, 1)); // prints 0.8
trace(-0.5.clamp(0, 1)); // prints 0.0

static inline function clampSym(v:Float, max:Float):Float

Like clamp but you only pass one argument (max) that is used as the upper limit and the opposite (additive inverse or -max) as the lower limit.

static inline function compare(a:Float, b:Float):Int

It returns the comparison value (an integer number) between two float values.

static function floorTo(f:Float, decimals:Int):Float

Rounds a number down to the specified number of decimals.

static inline function ftrunc(value:Float):Float

static function interpolate(f:Float, a:Float, b:Float):Float

interpolate returns a value between a and b for any value of f between 0 and 1.

static function interpolateAngle(f:Float, a:Float, b:Float, turn:Float = 360):Float

Interpolates values in a polar coordinate system looking for the narrowest delta angle.

It can be either clock-wise or counter-clock-wise.

static function interpolateAngleCCW(f:Float, a:Float, b:Float, turn:Float = 360):Float

Interpolates values in a polar coordinate system always in counter-clock-wise direction.

static function interpolateAngleCW(f:Float, a:Float, b:Float, turn:Float = 360):Float

Interpolates values in a polar coordinate system always in clock-wise direction.

static function interpolateAngleWidest(f:Float, a:Float, b:Float, turn:Float = 360):Float

Interpolates values in a polar coordinate system looking for the wideset delta angle.

It can be either clock-wise or counter-clock-wise.

static inline function max<T>(a:T, b:T):T

Return the maximum value between two integers or floats.

static inline function min<T>(a:T, b:T):T

Return the minimum value between two integers or floats.

static inline function nearEqualAngles(a:Float, b:Float, turn:Float = 360.0, tollerance:Float = EPSILON):Bool

Float numbers can sometime introduce tiny errors even for simple operations. nearEqualAngles compares two angles (default is 360deg) using a tiny tollerance (last optional argument). By default the tollerance is defined as EPSILON.

static function nearEquals(a:Float, b:Float, tollerance:Float = EPSILON):Bool

Float numbers can sometime introduce tiny errors even for simple operations. nearEquals compares two floats using a tiny tollerance (last optional argument). By default it is defined as EPSILON.

static inline function nearZero(n:Float, tollerance:Float = EPSILON):Bool

nearZero finds if the passed number is zero or very close to it. By default EPSILON is used as the tollerance value.

static inline function normalize(v:Float):Float

normalize clamps the passwed value between 0 and 1.

static function parse(s:String):Float

parse can parse a string and tranform it into a Float value.

static inline function root(base:Float, index:Float):Float

Computes the nth root (index) of base.

static function roundTo(f:Float, decimals:Int):Float

Rounds a number to the specified number of decimals.

static inline function sign<T>(value:T):Int

sign returns -1 if value is a negative number, 1 otherwise.

static inline function toFloat(s:String):Float

Alias for parse, mainly for disambiguation with other parses using mega Thx.

static inline function toString(v:Float):String

static inline function trunc(value:Float):Int

static function wrap(v:Float, min:Float, max:Float):Float

Passed two boundaries values (min, max), wrap ensures that the passed value v will be included in the boundaries. If the value exceeds max, the value is reduced by min repeatedely until it falls within the range. Similar and inverted treatment is performed if the value is below min.

static function wrapCircular(v:Float, max:Float):Float

Similar to wrap, it works for numbers between 0 and max.

Toggle inherited fields