Units
NextFTC has an immutable, type-safe units system that allows you to pass quantities in any unit. It also prevents you from mixing up units accidentally. NextFTC has three types of units: Distance
, Angle
, and TimeSpan
. All units extend the Quantity
abstract class.
Distance
The first unit we'll look at is Distance
. There are six units Distance
accepts: millimeters, centimeters, meters, inches, feet, and yards. Internally it is stored in millimeters.
Creating a Distance
is simple:
val millimeters: Distance = 1000.mm
val centimeters: Distance = 100.cm
val meters: Distance = 1.m
val inches: Distance = 36.inches
val feet: Distance = 3.ft
val yards: Distance = 1.yd
You can convert a distance back to a double in any unit:
val inch: Distance = 1.inch
val millimeters: Double = inch.inMm // 25.4
val centimeters: Double = inch.inCm // 2.54
val meters: Double = inch.inMeters // 0.0254
val meter: Distance = 1.m
val inches: Double = meter.inIn // 39.37
val feet: Double = meter.inFt // 3.2808
val yards: Double = meter.inYd // 1.0936
All operations are easy to use:
val foot = 1.ft
val inch = 1.inch
val sum = foot + inch // 13 in
val difference = foot - inch // 11 in
val product = foot * inch // 12 in (should be in^2 but NextFTC isn't THAT complicated)
val quotient = foot / inch // 12
val bigger = foot * 2 // 24 in
val smaller = foot / 2 // 6 in
val positive = +foot // 12 in
val negative = -foot // -12 in
val abs = foot.abs // 12 in
val otherAbs = abs(foot) // also 12 in!
val sign = foot.sign // 1 (1 for positive numbers, 0 for zero, -1 for negative numbers)
val remainder = foot % 5.in // 2 in
val pureRemainder = foot % 5 // also 2 in
foot > inches // true
foot >= inches // true
foot < inches // false
foot <= inches // false
foot == inches // false
val isNaN = foot.isNaN() // false
TimeSpan
TimeSpan
is very similar to Distance
, only with different units. Supported units are microseconds, milliseconds, and seconds. Internally it is stored in microseconds.
Using TimeSpans
is simple:
val seconds = 1.sec
val milliseconds = 5.ms
val microseconds = 70000000.us
val secondsInMilliseconds = seconds.inMs // 1,000
val millisecondsInMicroseconds = milliseconds.inUs // 5,000
val microsecondsInSeconds = microseconds.inSec // 70
Angle
An Angle
is very similar to Distance
and TimeSpan
, but it also has functionality for wrapping and normalizing. Angles can be in radians, degrees, or full revolutions, and are stored internally as radians.
val fullCircle = 1.rev
val halfCircle = Math.PI.rad
val quarterCircle = 90.deg
val fullDegrees = fullCircle.inDeg // 360
val halfRevolution = halfCircle.inRev // 0.5
val quarterRadians = quarterCircle.inRad // pi/2
You can also wrap and normalize angles. Below is a table of what wrapping and normalizing does for angles in different units.
Unit | Wrapping | Normalizing |
---|---|---|
Revolutions | 0 to 1 | -0.5 to 0.5 |
Radians | 0 to 2pi | -pi to pi |
Degrees | 0 to 360 | -180 to 180 |
Here's an example:
val bigAngle = 550.deg;
val wrapped = bigAngle.wrapped.inDeg; // 190
val normalized = bigAngle.normalized.inDeg; // -170
NOTE
See the units reference for more information.