RFC: Units of measurement for D (Phobos?)
Nicolas Sicard
dransic at gmail.com
Tue Mar 4 08:30:10 PST 2014
On Tuesday, 4 March 2014 at 13:20:02 UTC, Nordlöw wrote:
>> Thanks! I think David Nadlingers' is more elaborate (scaled and
>> affine units) and less prone to rounding errors. I haven't used
>> it yet. Mine is more basic but has a parsing system that I use
>> to
>
> So you cannot defined things lika kPa from Pa in compile-time?
enum kPa = kilo(pascal);
which is equivalent to
enum kPa = 1000 * pascal;
Note that my module defines no real unit type, just quantities: a
unit is only a special quantity with a value of 1 and a set of
dimensions (a "base unit" in the sense of the SI). I think it
better fits what physical units really are. kPa would then be a
quantity that stores 1000 and a set of dimensions corresponding
to a pressure.
If you declare:
auto p = 101.3 * kPa;
p stores a value that is 101.3 * 1000 internally, not 101.3. With
David's library, I think you can declare kilopascal as a scaled
unit derived from pascal, and then a quantity in kilopascal would
actually store 101.3. The result is globally the same, but in
fact the representations of the numeric values would be a bit
different, and so would the rounding errors, when using floating
point types.
>> define units quickly at compile-time
>
> Can't David's package also quickly define in compile-time?
I think it can.
>
> Have you thought about merging your effort vid Davids and
> propose it to Phobos? This issue has stale for quite some time
> know.
I just think that David's package could have been proposed for
Phobos a long time ago. I don't know what has stopped it then. I
developed mine afterwards, not being aware of David's one.
> I would like to have support add support for geometric units
> like angles, coordinate systems, etc. Have you thought about
> integrating these with your module?
My package is extendable. The SI module is just a predefined set
of common quantities and prefixes. You can use your own. Some
angle units are defined in quantites.si. I am not sure of what
you call a coordinate systems in this respect.
--
Nicolas
More information about the Digitalmars-d
mailing list