www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Boost.units ported to D

reply Arlen <arlen.ng gmx.com> writes:
--047d7bea3f14d089c604d5762cff
Content-Type: text/plain; charset=ISO-8859-1

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

--047d7bea3f14d089c604d5762cff
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Greetings,<br><br>source: <a href=3D"https://github.com/Arlen/phobos/blob/s=
td_units/std/units.d" target=3D"_blank">https://github.com/Arlen/phobos/blo=
b/std_units/std/units.d</a><br>docs: <a href=3D"http://arlen.github.com/pho=
bos/std_units.html" target=3D"_blank">http://arlen.github.com/phobos/std_un=
its.html</a><br>


<br>you will also need the rational module,<br><br>source: <a href=3D"https=
://github.com/Arlen/phobos/blob/std_rational/std/rational.d" target=3D"_bla=
nk">https://github.com/Arlen/phobos/blob/std_rational/std/rational.d</a><br=

docs: <a href=3D"http://arlen.github.com/phobos/std_rational.html" target= =3D"_blank">http://arlen.github.com/phobos/std_rational.html</a><br> <br>There is still some work to be done, so I&#39;m calling it an alpha rel= ease.=A0 I don&#39;t know if it belongs in Phobos or not, but I hope others= will find it useful as I have.<br><br>External Issues:<br>1. Many of the d= efinitions of the systems have been commented out (starting line 3217) beca= use during compilation DMD runs out of memory.=A0 As it is, on my system DM= D consume nearly 2GB of ram.=A0 As a side note, the definitions should be i= n their own separate modules, but for now they are placed in structs.<br> <br>2. Very long and not so helpful error messages.=A0 When I say long I&#3= 9;m talking hundreds and sometimes thousands of lines of error messages pro= duced by DMD.=A0 <br> <br>Bugs in D:<br>If you grep for &quot;D_BUG_&quot; you will find several = potential bugs that I&#39;ve marked.=A0 I would appreciate it if someone co= uld take a look.<br><br>FIXMEs:<br>There are a number of FIXMEs, and at the= moment I don&#39;t have the best solution for them.=A0 You will find them = if you grep for &quot;FIXME_&quot;, with FIXME_6 being the most important o= ne.<br> <br>And then there are the potential bugs in Boost.units, which you will fi= nd in the very last part of the units.d module.=A0 I have posted several of= them on stackoverflow.=A0 I have also emailed the original authors and wai= ting to hear from them.=A0 #4 is holding back the completion of Absolute, w= hich also affect Quantity.=A0 I&#39;m considering removing Absolute altoget= her.<br> <br>And finally, there is the function convert (line 3666).=A0 It needs som= e cleanup, but this is the function used to explicitly convert from a quant= ity to another.=A0 I haven&#39;t decided on the actual syntax yet, but in B= oost.units casting is used (e.g., quantity&lt;cgs::length&gt; L1 =3D static= _cast&lt;quantity&lt;cgs::length&gt; &gt;(L2)).=A0 Any ideas?<br> <br>Arlen<br> --047d7bea3f14d089c604d5762cff--
Feb 11 2013
next sibling parent David <d dav1d.de> writes:
 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)
Feb 11 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Feb 11 2013
parent FG <home fgda.pl> writes:
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?
Feb 11 2013
prev sibling next sibling parent "Michael" <pr m1xa.com> writes:
It is possible write something like?

auto force = 2.0 * SI.Newton;
auto energy = force * 2.0 * SI.Meter;

Feb 11 2013
prev sibling next sibling parent Arlen <arlen.ng gmx.com> writes:
--20cf3074b45889058b04d57874e3
Content-Type: text/plain; charset=ISO-8859-1

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? --20cf3074b45889058b04d57874e3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Mon, Feb 11, 2013 at 1:19 PM, Michael= <span dir=3D"ltr">&lt;<a href=3D"mailto:pr m1xa.com" target=3D"_blank">pr = m1xa.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> It is possible write something like?<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> auto force =3D 2.0 * SI.Newton;<br> auto energy =3D force * 2.0 * SI.Meter;<br> </blockquote> <br> </blockquote></div><br><br>Yes because typeof(2.0 * si.newton) is Quantity!= (si.Force) and typeof(force * 2.0 * si.meter) is Quantity!(si.Energy).=A0 I= n the future when the unit systems are placed in their own modules, you cou= ld alias away and say:<br> <br>auto force =3D 2.0 * newton;<br>auto energy =3D force * 2.0 * meters;<b= r><br>Also note, &#39;newton&#39; and &#39;meter&#39; are instances of Unit= .=A0 You could define your own and give whatever name you like.=A0 For exam= ple:<br> <br>si.Length myMeter;<br>auto x =3D 3.0 * myMeter;<br>writeln(x);=A0 // pr= ints &#39;3 m&#39;<br><br>In std.units I had to make the instances manifest= constants (using instanceOf mixin) because they are in structs.=A0 That wi= ll probably change in the future if each system is placed in its own module= .=A0 In that case std.unit will have to become a package, and I&#39;m not s= ure if packages are allowed in Phobos. Anyone know?<br> --20cf3074b45889058b04d57874e3--
Feb 11 2013
prev sibling next sibling parent "develop32" <develop32 gmail.com> writes:
How about something like meters!15 or newtons!30? For me it is 
more pleasing and shorter than multiplication.
Feb 11 2013
prev sibling next sibling parent Arlen <arlen.ng gmx.com> writes:
--047d7b6dcee06fe24504d57a7d5f
Content-Type: text/plain; charset=ISO-8859-1

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' --047d7b6dcee06fe24504d57a7d5f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Mon, Feb 11, 2013 at 4:18 PM, develop= 32 <span dir=3D"ltr">&lt;<a href=3D"mailto:develop32 gmail.com" target=3D"_= blank">develop32 gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gm= ail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-le= ft:1ex"> How about something like meters!15 or newtons!30? For me it is more pleasin= g and shorter than multiplication.<br> </blockquote></div><br>&#39;meters!15&#39; is supposed to be &#39;15 * mete= rs&#39;, right?=A0 Then how would you express &#39;15 / meters&#39; ?=A0 Yo= u cannot; therefore, &#39;meters!15&#39; doesn&#39;t make sense.<br><br>15 = * meters, has an outupt &#39;15 m&#39; which is short for &#39;15 m^1&#39;.= =A0 The exponent is not shown when it&#39;s 1.<br> 15 / meters, has an output &#39;15 m^-1&#39;<br><br> --047d7b6dcee06fe24504d57a7d5f--
Feb 11 2013
prev sibling next sibling parent "develop32" <develop32 gmail.com> writes:
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.
Feb 11 2013
prev sibling next sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
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'

Feb 11 2013
prev sibling parent "Dicebot" <m.strashun gmail.com> writes:
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.

What is the problem with "15 / meters!1" ?
Feb 11 2013