digitalmars.D.learn - Need help with units library
- alexander1974 (44/44) Aug 18 2017 I want to write a library for working with units (lengths,
- Joakim (6/10) Aug 18 2017 I have no opinion on your layout, but have you seen these
I want to write a library for working with units (lengths, weights, ...). It should allow maths and converting with/between different units (cm to mm, angstrom to meter, ...). The SI Sytem consists of the base units for length (meter), mass (kg), time (second) ,... and the prefixes like yotta (10²⁴) to femto (10⁻¹⁵). enum for the prefixes enum prefix { ... c = -2, /// for centi m = -3, /// for mili ... } enum for different units enum unit { length, weight, ... } I take a struct to define the units struct units (T) { T _value; /// the value (eg 10) prefix _pre; /// the SI-prefix (eg. c for cm) unit _unit; /// the unit like length or weight to keep them apart }; to keep it simple maybe functions could help (also with ifti) auto cm (T) (T value) { return units!T(value, prefix.c, unit.length); } conversion within a unit is simple comparing the prefixes: real conv (prefix lhs, prefix rhs) { import std.math:pow; int c = rhs - lhs; return pow(10.0,c); } math could be done with opBinary overloading in the struct auto opBinary(string op)(length rhs) { return mixin("units(_value "~op~" rhs._value*conv(pre,rhs.pre),_pre, _unit)"); } auto opBinary(string op)(T rhs) if (isNumeric!T) { return mixin("units(_value "~op~" rhs,_pre,_unit)"); } What do you think about the layout so far? Is there a better way? How to implement non-SI-units?
Aug 18 2017
On Friday, 18 August 2017 at 13:21:06 UTC, alexander1974 wrote:I want to write a library for working with units (lengths, weights, ...). It should allow maths and converting with/between different units (cm to mm, angstrom to meter, ...). [...]I have no opinion on your layout, but have you seen these existing libraries on the D package repository? http://code.dlang.org/packages/units-d http://code.dlang.org/packages/quantities Perhaps you'd be better off contributing to one of them.
Aug 18 2017