Units

From ZCubes Wiki
Jump to: navigation, search

How to specify units

Units are a unique, inherent and simple feature of the z^3 language.

Using units is simple and easy. For example:

a=1m;

indicates that a is now a variable containing the value of 1 meter. For more complex units we can enclose them in brackets as in 3(m/sec).

If a variable needs to be specified in specific units, simply use the <> operator. Such as: a<>(m/sec);

In z^3 language, <> is the unit conversion operator. Also, any arithmetic operator such as +, -, *, /, ^, % etc. should be enclosed within < and > to allow unit conversions to propagate through these operations. These conversion operations can be done on arrays as parameters also. Hence to add a meter to 2 centimeters, do the following: a=1m<+>2cm; and z^3 will give the answer: 102 cm.

Note, that this result now carries the units with it for further operations. For example: a=(1m<+>2cm)<^>3; gives the answer as 1061208 cm3.

To convert the current value of a variable to a different unit, simply use the <> operator: a<>cm;

Notice that if a already had a unit, it will be appropriately converted using the units. If it did not have units inherently, it will simply be given the units.


a=1

1

a<>cm;

1 cm.

a=1m;

1 m.

a<>cm;

100 cm.

In the second case, 1m is converted to 100cm.

Variables with units can be operated on with or without unit awareness. The unit aware operators are enclosed in angular brackets and are listed in Unit Operators.


<>()- Any multiunit indication should be in () to avoid confusion with normal code that may mean something else. For 1 meter/second, 1(m/s) or 1<>(m/s) should be used. 1m etc is enough if it is a simple unit.

Unit Prefixes

Similarly, scales such as milli, mega, etc. are supported using conventional SI units.

a=1m;

1 m.

a<>ym;

1.0000000000000001e+24 ym.

Here a now has the unit yoctometer.

Using uppercase Y, we get the answer in Yottameter.

a=1m;

1 m.

a<>Ym;

1.0000000000000001e-24 Ym.

These conversions of scale follow the SI unit prefix standards.

Metricscales.png

Metric Prefixes in Every day Use

Text Symbol Factor Power
yotta Y 1000000000000000000000000 1024
zetta Z 1 000 000 000 000 000 000 000 1021
exa E 1 000 000 000 000 000 000 1018
peta P 1 000 000 000 000 000 1015
tera T 1 000 000 000 000 1012
giga G 1 000 000 000 109
mega M 1 000 000 106
kilo k 1 000 103
hecto h 100 102
deka e 10 101
none none 0 100
deci d 0.1 10-1
centi c 0.01 10-2
milli m 0.001 10-3
micro u 0.000 001 10-6
nano n 0.000 000 001 10-9
pico p 0.000 000 000 001 10-12
femto f 0.000 000 000 000 001 10-15
atto a 0.000 000 000 000 000 001 10-18
zepto z 0.000 000 000 000 000 000 001 10-21
yocto y 0.000 000 000 000 000 000 000 001 10-24

Conversion among Unit Systems

z^3 supports conversion among unit systems, such as SI Units and FPS systems.

For example:

a=1mi;

1 mi.

a<>km;

1.6093439999999999 km.

Notice that the mi (miles) unit of variable a is converted to km.

Units on Array of Values

Units work on variables as simple numerical values or as arrays.

Let us consider numbers 1 to 10.

a=1..10;

Then say this is in miles.

b=a<>(mi);

Convert to kilometers.

b<>km

Numeric Units UNITSOF
1mi km 1.6093439999999999km
2mi km 3.2186879999999998km
3mi km 4.828031999999999km
4mi km 6.4373759999999995km
5mi km 8.046719999999999km
6mi km 9.656063999999999km
7mi km 11.265407999999999km
8mi km 12.874751999999999km
9mi km 14.484096km
10mi km 16.093439999999998km

We can convert arrays of Unit values to converted values as in the following:

a=(1..10)<>℃;

b=a<>℉;


Numeric Units UNITSOF
1℃ degF 33.8℉
2℃ degF 35.6℉
3℃ degF 37.4℉
4℃ degF 39.2℉
5℃ degF 41℉
6℃ degF 42.8℉
7℃ degF 44.6℉
8℃ degF 46.4℉
9℃ degF 48.2℉
10℃ degF 50℉

Units inside functions

Units can be highly effective inside functions. These, along with constants, provide a fantastic way to express your logic beautifully.

Let us consider how we can write E=mc^2. E:=(m<>kg)<*>(SPEEDOFLIGHT()<^>2);

E(2kg)

gives: 179751035747363520kg.m2.s-2

Note the use of <*> and <^> for the operators in unit operations.

In case you wanted to give the output in Joules.

E(2kg)<>J

gives: 179751035747363520J


Let us add variables intermixing miles and kilometer.

a=1mi;

b=10km;

[a,b,(a<+>b)<>mi]

[a,b,(a<+>b)<>mi]

1mi 10km 7.21371192237334mi

10mi<>km

can convert miles to kilometers.


Units Aware Internal Functions

In most sytems, functions like SIN operate on radians. Most users are unaware of this, and get confused when they get SIN(90) to be something unexpected.

With z^3 unit aware functions, this is not an issue.

For example,

a=90deg;

SIN(a)

will automatically convert the variable a into radians before calculations. To see the difference, try SIN(90) vs. SIN(90<>deg).

The answers given are: 0.8939966636005579 and 1.

Unit awareness is built into functions in z^3. (Note: Some functions are being transitioned).

Generation of Arrays with Units

rad2piby10

0㎭
0.6283185307179586㎭
1.2566370614359172㎭
1.8849555921538759㎭
2.5132741228718345㎭
3.141592653589793㎭
3.7699111843077517㎭
4.39822971502571㎭
5.026548245743669㎭
5.654866776461628㎭
6.283185307179586㎭

Note the results from rad2piby10 is automatically in radians.

Similarly array results using series in degrees are also marked with units of degrees.

deg110by10

10°
20°
30°
40°
50°
60°
70°
80°
90°
100°
110°

(rad2piby10)<>rad<>deg

Numeric Units UNITSOF
0㎭ deg
0.6283185307179586㎭ deg 36°
1.2566370614359172㎭ deg 72°
1.8849555921538759㎭ deg 108°
2.5132741228718345㎭ deg 144°
3.141592653589793㎭ deg 180°
3.7699111843077517㎭ deg 216°
4.39822971502571㎭ deg 252°
5.026548245743669㎭ deg 288°
5.654866776461628㎭ deg 324°
6.283185307179586㎭ deg 360°

shows the series in radians converted to degrees.

Currency Conversions

z^3 can automatically convert currencies.

b=(100$<+>3020¢);

gives the result

13020 cent.

b<>$;

gives

130.2 dollar.

With internal and external mappings to currency conversion dates, z^3 is able to do international currency conversions also.

(100..1000..100)<>USD<>INR

Numeric Units UNITSOF
100USD INR 6680.820948336871INR
200USD INR 13361.641896673742INR
300USD INR 20042.462845010614INR
400USD INR 26723.283793347484INR
500USD INR 33404.10474168436INR
600USD INR 40084.92569002123INR
700USD INR 46765.7466383581INR
800USD INR 53446.56758669497INR
900USD INR 60127.388535031845INR
1000USD INR 66808.20948336872INR


We can override internal conversion rates by using SETCONVERSION.

SETCONVERSION(1<>euro,1<>dollar,1.1)

PRINTCONVERSION();

can give the recently set conversion rates.

1<>€<>$

gives 1 euro to convert to 1$.

SETCONVERSION(1$,1€,0.9);

shows the following on PRINTCONVERSION(): {

  dollar:euro: 0.9,
  euro:dollar: 1.1111111111111112

}


Adding currencies is demonstrated further below:

(100$<+>300¢<+>30¢)<>$

gives:

103.3 dollar.


Array Unit Normalization

If an array holds a set of values from varied units, UNITIFY can convert all of them to the same units.

UNITIFY([1cm,2m,3cm])

will give: 1cm 200cm 3cm

Likewise: SUM(UNITIFY([1m,2m,3mm]))

will give the answer:

3.003 m.

SUM(UNITIFY([1m,2m,3mm]))<>cm

will give answer in cm :

300.3cm

PMT(4%,12,1000$,1200¢,0)

gives the result as:

-107.35079875828919 dollar.

since PMT is a unit aware function.



A gallon is converted to liter below.

1gal<>l

3.786235392 l.

A liter is converted to oz. below:

1<>l<>oz

33.806667242732274 oz.

Shown below are conversions to micro liters.

(1..10)<>l<>ul

Numeric Units UNITSOF
1l ul 999999.9999999999ul
2l ul 1999999.9999999998ul
3l ul 2999999.9999999995ul
4l ul 3999999.9999999995ul
5l ul 4999999.999999999ul
6l ul 5999999.999999999ul
7l ul 6999999.999999999ul
8l ul 7999999.999999999ul
9l ul 8999999.999999998ul
10l ul 9999999.999999998ul

Unit Aware Logical Operations

Unit aware variables and arrays can also be operated on with Unit Aware Logical Operators and Unit Aware Mathematical Operators. Such operators are listed in Unit_Operators.

Is 1m greater than 1cm?

1m<>>1cm;

true

Is 1m greater than 1cm, and is 2m greater than 200cm?

[1m,2m]<>>[1cm,200cm];

Answer is:

[true, false]. Note that an array of answers is obtained here.

Is 1m greater than or equal to 1cm, and is 2m greater or equal to than 200cm?

[1m,2m]<>=>[1cm,200cm];

Answer is:

[true, true].

Is 1m less than 1cm, and is 2m less than 200cm?

[1m,2m]<<>[1cm,200cm];

Answer is:

[false,false]

Is 1m equal to 1cm, and is 2m equal to 200cm?

[1m,2m]<==>[1cm,200cm];

Answer is:

[false,true]

Is 1m not equal to 1cm, and is 2m not equal to 200cm?

[1m,2m]<!=>[1cm,200cm];

Answer is:

[true,false]

Is 1m less than 1cm, and is 2m less than 199.99cm?

[1m,2m]<<=>[1cm,199.99cm];

Answer is:

[false,false]

Is 1m less than or equal to 1cm, and is 2m less than or equal to 199.99cm?

[1m,2m]<<=>[1cm,200.01cm];

Answer is:

[false,true]

Also note that it is not necessary for both sides of these to be the same size. For example, the following is a check to see which all numbers between 1m to 10m are greater than 3m.

((1..10)<>m)<>>3m;

And the answer is:


false
false
false
true
true
true
true
true
true
true

The following is a check to see which all numbers in the series 0.1m, 1.1m, upto 10m are greater than 30cm.

((0.1..10)<>m)<>>30cm

((0.1..10)<>m)

Numeric Units UNITSOF
0.1 m 0.1m
1.1 m 1.1m
2.1 m 2.1m
3.1 m 3.1m
4.1 m 4.1m
5.1 m 5.1m
6.1 m 6.1m
7.1 m 7.1m
8.1 m 8.1m
9.1 m 9.1m

((0.1..10)<>m)<>>30cm

The answer is:

false
true
true
true
true
true
true
true
true
true

Types of Supported Unit Conversions

z^3 unit conversions cover a large number of units, and is highly flexible in handling new units that are not known.

Basic Conversions

The command CONVERT() gives the list of basic units supported within z^3.

MassUnits:g,sg,lbm,u,ozm,

DistanceUnits:m,mi,Nmi,in,ft,yd,ang,Pica,

TimeUnits:yr,day,hr,mn,sec,year,s,min,hour,

PressureUnits:Pa,atm,mmHg,

ForceUnits:N,dyn,lbf,

EnergyUnits:J,e,c,cal,eV,HPh,Wh,flb,BTU,

PowerUnits:HP,W,

MagnetismUnits:T,ga,

LiquidUnits:tsp,tbs,oz,cup,pt,qt,gal,l,

Prefixes:Y,Z,E,P,T,G,M,k,h,e,d,c,m,u,n,p,FValue,a,z,y,

Others:rad,deg,F,C,K,dollar,euro,cent,rupee

Any custom user supplied conversions would also be listed.

Derived Conversions

The command SUPPORTEDUNITS() gives the list of derived and related units supported by z^3. More details are given in Derived Units.

Behavior on Unit Conversion Failures

In general, if a conversion fails, the original value with units are returned. This behavior can be changed so that a unit conversion if not possible, would return a null value. To do this, change the global variable UNITSFAILTONULL to true.

The following will give 1m as the result.

1m<>eor

1 m

UNITSFAILTONULL=true

1m<>eor

will now give null as the result.

UNITSFAILTONULL=false

1m<>eor

1 m

will be shown as the result.

Interesting Techniques

Long Variable Names can be used in the expressions, including units as given below.

##weight of zone=3m; 

++(##weight of zone);

weightofzone+4;

Please note that ##weight of zone is the same as weightofzone.

Constants

Several useful constants are already defined in z^3, and can be accessed by prefixing % to the name of the constant. While doing this, you can simply give a few of the letters to indicate the constant, rather than the full name. These names can be space separated also.

For example:

%planck;

6.62607004081e-34 J s.

%boltzmann;

1.380648813e-23 J K-1.

%planck length;

1.61619997e-35 m.

To get a listing of all the CONSTANTS currently supported (along with units the numbers are represented in), simply give the command to z^3:

CONSTANTS()

speed of light in vacuum 299792458 m·s−1
speed of sound in dry air 343.2 m·s−1
newtonian constant of gravitation 6.6740831e−11 m3·kg−1·s−2
planck constant 6.62607004081e−34 J·s
reduced planck constant 1.05457180013e−34 J·s
magnetic constant vacuum permeability 1.256637061e−6 N·A−2
electric constant vacuum permittivity 8.854187817e−12 F·m−1
characteristic impedance of vacuum 376.730313461 Ω
coulomb's constant 8.987551787e9 N·m2·C−2
elementary charge 1.60217656535e−19 C
bohr magneton 9.2740096820e−24 J·T−1
conductance quantum 7.748091734625e−5 S
inverse conductance quantum 12906.403721742 Ω
josephson constant 4.8359787011e14 Hz·V−1
magnetic flux quantum 2.06783375846e−15 Wb
nuclear magneton 5.0507835311e−27 J·T−1
von klitzing constant 25812.807443484 Ω
bohr radius 5.291772109217e−11 m
classical electron radius 2.817940326727e−15 m
electron mass 9.1093829140e−31 kg
fermi coupling constant 1.1663645e−5 GeV−2
fine-structure constant 7.297352569824e−3
hartree energy 4.3597443419e−18 J
proton mass 1.67262177774e−27 kg
quantum of circulation 3.636947552024e−4 m2 s−1
rydberg constant 10973731.56853955 m−1
thomson cross section 6.65245873413e−29 m2
weak mixing angle 0.222321
efimov factor 22.7
atomic mass constant 1.66053892173e−27 kg
avogadro's number 6.0221412927e23 mol−1
boltzmann constant 1.380648813e−23 J·K−1
faraday constant 96485.336521C·mol−1
first radiation constant 3.7417715317e−16 W·m2
first radiation constant for spectral radiance 1.19104286953e−16 W·m2·sr−1
loschmidt constant 2.686780524e25 m−3
gas constant 8.314462175 J·K−1·mol−1
molar planck constant 3.990312717628e−10 J·s·mol−1
molar volume of an ideal gas at T=273.15K and p=100kPa 2.271095321e−2 m3·mol−1
molar volume of an ideal gas at T=273.15K and p=101.325 kPa 2.241396820e−2 m3·mol−1
sackur-tetrode constant at T=1 K and p=100kPa −1.151707823
sackur-tetrode constant at T=1 K and p=101.325 kPa −1.164870823
second radiation constant 1.438777013e−2 m·K
stefan–boltzmann constant 5.67037321e−8 W·m−2·K−4
wien displacement law constant 2.897772126e−3 m·K
conventional value of josephson constant 4.835979e14 Hz·V−1
conventional value of von klitzing constant 25812.807 Ω
molar mass constant 1e−3 kg·mol−1
molar mass constant of carbon-12 1.2e−2 kg·mol−1
standard acceleration of gravity (gee, free-fall on earth) 9.80665 m·s−2
gee 9.80665 m·s−2
standard atmosphere 101325 Pa
planck length 1.61619997e−35 m
planck mass 2.1765113e−8 kg
planck time 5.3910632e−44 s
planck charge 1.87554595641e−18 C
planck temperature 1.41683385e32 K

MORE

/* THESE ARE BEING DOCUMENTED NOW */

(1..100)<>l<>ul 1Pa<>atm

E:=(m<>kg)<*>(SPEEDOFLIGHT()<^>2); b=E(2g); ConvertToUnitOfNature(b,"energy")


CONSTANT("G") CONSTANT("G")<*>1kg<*>110kg</>(6000m<^>2) // did not work.

(CONSTANT("G")<*>1kg<*>110kg)</>((6000m)<^>2) // how to bracket the units to be inside and ignore < etc? // ## can be used to name variables with spaces in them. // the actual variable is named by removing the spaces. // otherwise it goes into code as it is.


//%G is gravitational constant GF:=%G*m1*m2/r^2; GF(1,2,300)

v:=u+%g*t; // this uses %g as default g value v(1,2)


PRINTCONVERSIONS()

      CONVERT()

A¹=34; A=[23,4,5,5]; /* no array indexing on subscript yet. Will decide on that later. */ A¹=[23,4,5,5] A¹[2]

a=%gravit; b=30<*>a

a=%gravit; b=(1..10)<*>a

E:=m<>kg<*>%c<^>2; E(1000kg) E(1000kg<>lbm) // - will gave same result as 1000kg E(1000lbm) //lbm is used for units of lb mass.

// %G<*>1kg<*>110kg(6000m<^>2) // %G<*>1kg<*>110kg((6000m)<^>2) // -did not work. Said 7.34149141e-9Nm3UNITPOWERUNITSOF %G<*>1kg<*>110kg<*>((6000m)<^>2)

%G<*>1kg<*>110kg</>((6000m)<^>2)


// make this work. 1..100 @> x.txt; // send output to a <@ x.txt; // read input from a;

1.3J<>GeV 1.3J<>eV

1J<>l.atm; // 0.0098692latm 1J<>(l.atm-1) // this should fail. and will keep original units. maybe safer. 1m2<>in2 1m²<>in²

10m2<>are 1m2<>are

100m2<>acre

E:=m<>kg<*>%c<^>2; b=E(1000kg)<>J

   c=b<>eV;

[b,c]

1..100.$("[SIN(x),COS(x),TAN(x)]") 1..100.$([SIN,COS,TAN]) worked. 1..100.$("[x,x^2,x^3,x^4,x^3+x^4,SIN(x)]") works 1..100.$("[SIN(x),COS(x),TAN(x)]") 1..100.$([SIN,COS,TAN]) x=SERIESOF("1/x",9,10) //this is to from in parameter sequence. x=SERIESOF("1/x",19,10) SERIESOF("1/x",10,5) x=SERIESOF "1/LOG(x,2)" 100 1 x=SERIESOF "1/log2x" 100 91 //removed: 1..10.$_([+,-]) 1..10.$_([SUM,MINUS]) //removed: 1..10.$_|+| EXPOF(5) [SIN,COS]@[1..100] 1..10.$_("+") 1..10.$_("+") 1..10@["+"] 1..10@"+"

(1..100)<>m<+>10cm

MAGICSQUARE(3).$$(SUM) MAGICSQUARE(3).$$$(SUM) MAGICSQUARE(3).$_(SUM)

a=1m<>mm<+>400km 1m<>eor // should give nothing as there is no such conversion. (((1..100)<>m)<^>2)<>are

(1..1000)<>$ (((1..100)<>m)<^>2)<>km2 // why chinese headers here?

(1..100)m<>cm

F=%G<*>100kg<*>10g</>((100km)<^>2);

1.3J<>GeV //check this

velocity=3<>(m/sec); time=1hr; velocity<*>time;

1(g.m)<>(mg.cm)

a=LAZYRANGE(1,100) a(1..100) // generate numbers in the range at that interval, without having to generate the array ahead of time. Useful in things like INTEGRALS etc. a() a=LAZYOBJECT(1,100,1000) // can even take a(0.1); a.islazyrange()


1(kg.m2s-2)<>(N.m) 1(kg.m2s-2)<>J 1(kg.m2s-2)<>(N) // also should work as we want to get rid of the /s here in this case. 1(kg.m2s-2)<>(N.m2) // should fail. 1(kg.m2s-2)<>(J/s) // should fail. 1(kg.m2s-3)<>(J/s) 1(kg.m2.s)<>(J/s) // should this fail? it works now.

12(g.m-1/s)<>(g.cm) // should fail. 12(g.m)<>(g.mm) // should not fail. 12(g.m)<>(g.cm) // should not fail.

12(g.m)<>(g.s) // should fail. original should be returned. 12(g.m)<>(g.mm) 12(g.m)<>(g.s) 12(g.m-1)<>(g.mm-1) 12(g.m-1/s)<>(g.cm-1) // converts, but this is ok, since /s on left sometimes we want to get rid off for some reason for using it on another calculation. Say N/s but you only want to use the N on it. 1(kg.m2/s2)<>e // how to make this conversion?

1(kg.m2/s2)<>J<>e

1.3(kg.m2/s2)<>Jo // would fail and original will be kept.

(((1..100)<>m)<^>2)<>are (1..100)<>are [1cm2,2m2]<>are %pi*34; %e; %avo;

q=1(mol/s); heat=%avo<*>q;

1Pa<>atm 1atm<>psi 1atm<>hPa

a=1<>(kg/m.s-2)<>Pa<>torr 1J<>BTU 1ly<>m 10ly<>m<>km

1atm<>inHg<>mmHg

1atm<+>1bar (1atm<+>1bar)<>atm 1km<>m<>lightyear

1lightyear<>km

1lightyear<>pc

1m<>ly

1km<>ly //seemed off. this is possibly correct. (1km<+>1ly)<>km

1Å<>km

1Å<>m

(1..100)<>AU<>m

1degree<>arcminute

1Tsec<>sec

1(m/s)<>(km/hr) 1(km/s)<>(km/hr)

1(cal/mn)<>(J/sec) // d

1(kg/m3)<>(slug.ft-3)

2(rad/sec)<>rpm // did not convert. not sure why.

(1..100)<>USD<>EUR

SUPPORTEDUNITS()

1nibble<>bit 102400000000bit<>MB 1kB<>MB // seems wrong. 1kB<>Mb 1(kb)<>(bit) Pi<>(rad/sec)<>rpm 1(rad/sec)<>rpm 1(rad/mn)<>rpm (1(rad/mn)<>rpm)*2*Pi // should give 1. 1(rotation/mn)<>rpm 1(cycle/mn)<>(rad/mn)

1(cycle/mn)<>rpm

60rad<>deg 60rad<>deg<>rotation

1MB<>kB 1B<>kB 1MB<>B 1kB<>B

a=1(kg/m/s-2/t3) a=1(kg/m/s-2/t3) a=1(kg/m/s-2/t3) a=1(kg/m/s-2/t3) a=1<>(kg/m.s-2) // this worked though.

1(kg/m3)<>(slug/ft^3) // will not work due to ^. 1(kg/m3)<>(slug/ft3) //works 1(kg/m3)<>(slug.ft-3) //works

1(s-1)<>Hz 1(mn-1)<>Hz

velocity=3<>(m/s); time=1hr; velocity<*>time;

(((1..100)<>m)<^>2)<>acre

((1..100)<>m)<^>2