Floats contains helper methods to work with Float values.

Static variables

@:value(1e-9)staticinlineread onlyEPSILON:Float = 1e-9

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

@:value(10e-5)staticinlineread onlyTOLERANCE:Float = 10e-5

@:value({ zero : 0.0, append : function(a:Float, b:Float) return a + b })staticread onlymonoid:Monoid<Float> = { zero : 0.0, append : function(a:Float, b:Float) return a + b }

@:value(Ord.fromIntComparison(compare))staticread onlyorder:Ord<Float> = Ord.fromIntComparison(compare)

The ordering instance for floating-point values.

Static methods

@:value({ turn : 360.0 })staticangleDifference(a:Float, b:Float, turn:Float = 360.0):Float

Returns the angular distance between 2 angles.

staticcanParse(s:String):Bool

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

staticceilTo(f:Float, decimals:Int):Float

Rounds a number up to the specified number of decimals.

staticinlineclamp(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

staticinlineclampSym(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.

staticinlinecompare(a:Float, b:Float):Int

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

staticfloorTo(f:Float, decimals:Int):Float

Rounds a number down to the specified number of decimals.

staticinlineftrunc(value:Float):Float

staticinterpolate(f:Float, a:Float, b:Float):Float

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

@:value({ turn : 360 })staticinterpolateAngle(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.

@:value({ turn : 360 })staticinterpolateAngleCCW(f:Float, a:Float, b:Float, turn:Float = 360):Float

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

@:value({ turn : 360 })staticinterpolateAngleCW(f:Float, a:Float, b:Float, turn:Float = 360):Float

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

@:value({ turn : 360 })staticinterpolateAngleWidest(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.

staticinlinemax<T>(a:T, b:T):T

Return the maximum value between two integers or floats.

staticinlinemin<T>(a:T, b:T):T

Return the minimum value between two integers or floats.

@:value({ tollerance : EPSILON, turn : 360.0 })staticinlinenearEqualAngles(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.

@:value({ tollerance : EPSILON })staticnearEquals(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.

@:value({ tollerance : EPSILON })staticinlinenearZero(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.

staticinlinenormalize(v:Float):Float

normalize clamps the passwed value between 0 and 1.

staticparse(s:String):Float

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

staticinlineroot(base:Float, index:Float):Float

Computes the nth root (index) of base.

staticroundTo(f:Float, decimals:Int):Float

Rounds a number to the specified number of decimals.

staticinlinesign<T>(value:T):Int

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

staticinlinetoFloat(s:String):Float

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

staticinlinetoString(v:Float):String

staticinlinetrunc(value:Float):Int

staticwrap(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.

staticwrapCircular(v:Float, max:Float):Float

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