naming conventions - preserving units for calculations in programming -


i wondering if there sweet languages offer sort of abstraction "feet" vs "inches" or "cm" etc. considering doing following in java:

u(56).feet() + u(26).inches() 

and able like

17.7292 meters result.

one possible approach is, when making new value, convert "base" unit, meters or something, can add them easily.

however, rather have ability preserve units, like

u(799.95555).feet() - u(76).feet()  

returns

723.95555 feet 

and not

243.826452 meters - 23.1648 meters = 220.661652 meters  //220.661652 meters feet returns 723.955551 feet  

since problem seems common, there framework or programming language exists handles elegantly?

i suppose can add units in methods, adding matching units , converting in order +-*/ [add/subtract/multiply/divide] when requested, great adding , subtracting:

//a {     this.inches = 36.2;     this.meters = 1; }  //total length 1.91948 m 

if add object b values

//b {     this.inches = 0.8;     this.meters = 2; }  //total length 2.02032 m 

and new object is

{     this.inches = 37;     this.meters = 3; }  //total length 3.9398 meters 

which totally awesome, can convert whenever want no problem. operations such multiplication fail ...

//a * b = 3.87796383 m^2 {     this.inches = 28.96;     this.meters = 2; }  // ...but multiplying piece-wise , adding // gives 2.01868383 m^2, assuming make 2m*1m give 2 m^2. 

so wanted show example that

( a1 + a2 ) * ( z1 + z2 ) not ( a1 * z1 ) + ( a2 * z2 ) 

and i'm pretty sure means 1 has convert common unit if want multiply or divide.

the example discourage reflexive answer, add or subtract them piece-wise before converting @ last moment, since * , / fail.

tl;dr: there clever ways preserve units in programming? there clever ways name methods/routines such it's easy me understand i'm adding , subtracting, etc?

i know fact there such language, although haven't used myself.
it's called frink.

it not allows mix different units same dimension operate on several different physical measurements. sample calculations on site fun read. particular superman bit.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -