View mode: basic / threaded / horizontal-split · Log in · Help
February 11, 2013
Boost.units ported to D
Greetings,

source: https://github.com/Arlen/phobos/blob/std_units/std/units.d
docs: http://arlen.github.com/phobos/std_units.html

you will also need the rational module,

source: https://github.com/Arlen/phobos/blob/std_rational/std/rational.d
docs: http://arlen.github.com/phobos/std_rational.html

There is still some work to be done, so I'm calling it an alpha release.  I
don't know if it belongs in Phobos or not, but I hope others will find it
useful as I have.

External Issues:
1. Many of the definitions of the systems have been commented out (starting
line 3217) because during compilation DMD runs out of memory.  As it is, on
my system DMD consume nearly 2GB of ram.  As a side note, the definitions
should be in their own separate modules, but for now they are placed in
structs.

2. Very long and not so helpful error messages.  When I say long I'm
talking hundreds and sometimes thousands of lines of error messages
produced by DMD.

Bugs in D:
If you grep for "D_BUG_" you will find several potential bugs that I've
marked.  I would appreciate it if someone could take a look.

FIXMEs:
There are a number of FIXMEs, and at the moment I don't have the best
solution for them.  You will find them if you grep for "FIXME_", with
FIXME_6 being the most important one.

And then there are the potential bugs in Boost.units, which you will find
in the very last part of the units.d module.  I have posted several of them
on stackoverflow.  I have also emailed the original authors and waiting to
hear from them.  #4 is holding back the completion of Absolute, which also
affect Quantity.  I'm considering removing Absolute altogether.

And finally, there is the function convert (line 3666).  It needs some
cleanup, but this is the function used to explicitly convert from a
quantity to another.  I haven't decided on the actual syntax yet, but in
Boost.units casting is used (e.g., quantity<cgs::length> L1 =
static_cast<quantity<cgs::length> >(L2)).  Any ideas?

Arlen
February 11, 2013
Re: Boost.units ported to D
> And finally, there is the function convert (line 3666).  It needs some
> cleanup, but this is the function used to explicitly convert from a
> quantity to another.  I haven't decided on the actual syntax yet, but in
> Boost.units casting is used (e.g., quantity<cgs::length> L1 =
> static_cast<quantity<cgs::length> >(L2)).  Any ideas?

You could make: to!unit(12) possible (to from std.conv)
February 11, 2013
Re: Boost.units ported to D
Arlen:

> https://github.com/Arlen/phobos/blob/std_units/std/units.d
> docs: http://arlen.github.com/phobos/std_units.html

It seems a lot of work.
But code like this is not encouraging:


   Quantity!(si.Energy) work(Quantity!(si.Force) F, 
Quantity!(si.Length) dx)
   {
       return F * dx;
   }

   void main()
   {
       Quantity!(si.Force)  F  = 2.0 * si.newton;
       Quantity!(si.Length) dx = 2.0 * si.meter;
       Quantity!(si.Energy) E  = work(F, dx);
       writefln("F  = %s\ndx = %s\nE  = %s\n", F, dx, E);

       alias Complex!double ComplexType;

       Quantity!(si.ElectricPotential, ComplexType) v = 
complex(12.5, 0) * si.volts;
       Quantity!(si.Current, ComplexType)           i = 
complex(3, 4) * si.amperes;
       Quantity!(si.Resistance, ComplexType)        z = 
complex(1.5, -2) * si.ohms;


I think units are something that needs a good syntax to have a 
chance to be used :-(

Bye,
bearophile
February 11, 2013
Re: Boost.units ported to D
It is possible write something like?

>auto force = 2.0 * SI.Newton;
>auto energy = force * 2.0 * SI.Meter;
February 11, 2013
Re: Boost.units ported to D
On 2013-02-11 20:08, bearophile wrote:
> It seems a lot of work.
> But code like this is not encouraging

Nah, it's quite digestible after replacing "Quantity" with "Q".
But would anyone doing numerical processing ever use typed units?
February 11, 2013
Re: Boost.units ported to D
On Mon, Feb 11, 2013 at 1:19 PM, Michael <pr@m1xa.com> wrote:

> It is possible write something like?
>
>  auto force = 2.0 * SI.Newton;
>> auto energy = force * 2.0 * SI.Meter;
>>
>
>

Yes because typeof(2.0 * si.newton) is Quantity!(si.Force) and typeof(force
* 2.0 * si.meter) is Quantity!(si.Energy).  In the future when the unit
systems are placed in their own modules, you could alias away and say:

auto force = 2.0 * newton;
auto energy = force * 2.0 * meters;

Also note, 'newton' and 'meter' are instances of Unit.  You could define
your own and give whatever name you like.  For example:

si.Length myMeter;
auto x = 3.0 * myMeter;
writeln(x);  // prints '3 m'

In std.units I had to make the instances manifest constants (using
instanceOf mixin) because they are in structs.  That will probably change
in the future if each system is placed in its own module.  In that case
std.unit will have to become a package, and I'm not sure if packages are
allowed in Phobos. Anyone know?
February 11, 2013
Re: Boost.units ported to D
How about something like meters!15 or newtons!30? For me it is 
more pleasing and shorter than multiplication.
February 11, 2013
Re: Boost.units ported to D
On Mon, Feb 11, 2013 at 4:18 PM, develop32 <develop32@gmail.com> wrote:

> How about something like meters!15 or newtons!30? For me it is more
> pleasing and shorter than multiplication.
>

'meters!15' is supposed to be '15 * meters', right?  Then how would you
express '15 / meters' ?  You cannot; therefore, 'meters!15' doesn't make
sense.

15 * meters, has an outupt '15 m' which is short for '15 m^1'.  The
exponent is not shown when it's 1.
15 / meters, has an output '15 m^-1'
February 11, 2013
Re: Boost.units ported to D
On Monday, 11 February 2013 at 22:33:02 UTC, Arlen wrote:
> 'meters!15' is supposed to be '15 * meters', right?  Then how 
> would you
> express '15 / meters' ?  You cannot; therefore, 'meters!15' 
> doesn't make
> sense.
>
> 15 * meters, has an outupt '15 m' which is short for '15 m^1'.  
> The
> exponent is not shown when it's 1.
> 15 / meters, has an output '15 m^-1'

Oh, somehow that skipped my mind.
But 15 / meters could be meters!(15, -1). It suits me more as it 
stands out of the whole expression and I would less likely make a 
mistake while writing it.
February 11, 2013
Re: Boost.units ported to D
On Monday, 11 February 2013 at 22:33:02 UTC, Arlen wrote:
> On Mon, Feb 11, 2013 at 4:18 PM, develop32 
> <develop32@gmail.com> wrote:
>
>> How about something like meters!15 or newtons!30? For me it is 
>> more pleasing and shorter than multiplication.
>
> 'meters!15' is supposed to be '15 * meters', right?  Then how 
> would you express '15 / meters' ?  You cannot; therefore, 
> 'meters!15' doesn't make sense.

 It does if you have a universal base you are working from. Like 
converting currency you convert most likely from the one currency 
to an intermediate, then the intermediate to your desired 
currency, that way you only have 2 steps and a small table vs 
making the ratios for every possible type.

 So if 1 meter suddenly is say 100,000, then 15 * meter and 
meters!15 / meter (or feet!100 / meters) makes perfect sense!

> 15 * meters, has an output '15 m' which is short for '15 m^1'.  
> The exponent is not shown when it's 1. 15 / meters, has an 
> output '15 m^-1'
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home