lenny 1.0.0
lenny: ^1.0.0 copied to clipboard
Lengths for the Visionary Software Solutions Measurement API, me_sure_meant
import 'package:lenny/lenny.dart';
import 'package:me_sure_meant/me_sure_meant.dart';
void main() {
final inches = 12.inches();
/// Some units are supported but not added as extensions...others no.
/// Because, honestly who measures in Imperial Hands?
/// Extensions seek to make the straightforward thing easy
/// while the API makes more complex things possible.
final hands = Hands.fromNum(22);
final added = inches + hands;
assert(added.magnitude.toNum() == 100.000054);
assert(added.unit == const Inch());
/// The author believes everyone should adopt SI, but there's the NFL...
final yards = 100.yards();
final longest = 1.yards();
/// NOTICE SOMETHING HERE
final difference = yards - longest;
assert(difference.magnitude.toNum() != 99);
/// Math is tricky when you support conversion between types.
/// Under the covers, all [Length] are converted to a common type for +/-
/// then converted back to their unit representation.
/// IEEE 754 math doesn't always yield precise results.
/// The library constructs objects whose == and comparison methods are
/// tolerant to within 3 significant figures.
assert(difference == 99.yards());
assert(inches == 12.0000000001.inches());
/// For situations where exactness is desired, an [exactinglyCrude]
/// FACTORY METHOD exists
final precision1 = Inches(exactinglyCrude(12.00001233));
final precision2 = Inches(exactinglyCrude(12.00001235));
assert(precision1 != precision2);
/// speaking of [crude], the library also supports uncertainty in measurement.
final scientific = Metres(uncertain(1.23, .57), Metre.centimetre);
/// Basic uncertainty is supported (+/- add absolute uncertainty)
final scientific2 = Metres(uncertain(1.51, .217), Metre.centimetre);
final result = scientific + scientific2;
print(result);
/// Uncertainty support is still experimental (puns #jokesForNerds)
/// and contributors are welcome to submit pull requests with more advanced
/// forms of uncertainty (like sum of squares of differences).
/// Other esoteric units are also supported
final chains = Chains.fromNum(2);
assertAreEqual(chains, 44.yards());
final furlongs = Furlongs.fromNum(10);
assertAreEqual(furlongs, 2200.yards());
final miles = 3.miles();
final leagues = Leagues.fromNum(1);
assertAreEqual(miles, leagues);
/// Of course we include SI units
final meters = Metres.metres(1);
/// they work as you expect
final inMillis = meters.toUnit(Metre.millimetre);
assert(inMillis.magnitude.toNum() == 1000);
assert(inMillis.unit == Metre.millimetre);
/// extensions make them easy to use
final kilometres = 1.kilometres();
final half = 500.metres();
assert(kilometres - half == half);
/// and the big payoff is that you can do complicated things simply
final mess = (32.inches() + 24.yards() + 3.miles() - 2.kilometres())
.toUnit(Metre.decimetre);
/// Do you have any idea what this is? I didn't!
print(mess);
/// because I'm not a subject of Blind Faith:
/// 32 in = 0.888889 yd
/// 0.888889 + 24 yd = 24.0888889 yd
/// 3 mi = 5280 yd
/// 5280 + 24.0.888889 = 5,304.088889 yd
/// 2 km = 2187.23 yd
/// 5,304.088889 - 2187.23 = 3,116.858889 yd
/// 1 yd = 0.9144 m therefore 3,116.858889 yd * 0.9144 =
/// 2,850.0557681016 m
/// 2,850.0557681016 m to dm = 28,500.557681016 dm
/// again, notice the double floating point arithmetic causes some round off
assert(Metres(crude(28507.94571144595), Metre.decimetre) == mess);
}
void assertAreEqual(final one, final two) {
assert(one == two);
}