Difference between revisions of "Units"
Line 79: | Line 79: | ||
b=a<>(mi); | b=a<>(mi); | ||
− | |||
− | |||
− | |||
− | |||
(1..100)<>m | (1..100)<>m | ||
Line 290: | Line 286: | ||
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Now b has the values 1..100 in miles. | Now b has the values 1..100 in miles. |
Revision as of 12:46, 13 June 2016
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.
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.
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 100. a=1..100;
b=a<>(mi);
(1..100)<>m
Numeric | Units | UNITSOF |
---|---|---|
1 | m | 1m |
2 | m | 2m |
3 | m | 3m |
4 | m | 4m |
5 | m | 5m |
6 | m | 6m |
7 | m | 7m |
8 | m | 8m |
9 | m | 9m |
10 | m | 10m |
11 | m | 11m |
12 | m | 12m |
13 | m | 13m |
14 | m | 14m |
15 | m | 15m |
16 | m | 16m |
17 | m | 17m |
18 | m | 18m |
19 | m | 19m |
20 | m | 20m |
21 | m | 21m |
22 | m | 22m |
23 | m | 23m |
24 | m | 24m |
25 | m | 25m |
26 | m | 26m |
27 | m | 27m |
28 | m | 28m |
29 | m | 29m |
30 | m | 30m |
31 | m | 31m |
32 | m | 32m |
33 | m | 33m |
34 | m | 34m |
35 | m | 35m |
36 | m | 36m |
37 | m | 37m |
38 | m | 38m |
39 | m | 39m |
40 | m | 40m |
41 | m | 41m |
42 | m | 42m |
43 | m | 43m |
44 | m | 44m |
45 | m | 45m |
46 | m | 46m |
47 | m | 47m |
48 | m | 48m |
49 | m | 49m |
50 | m | 50m |
51 | m | 51m |
52 | m | 52m |
53 | m | 53m |
54 | m | 54m |
55 | m | 55m |
56 | m | 56m |
57 | m | 57m |
58 | m | 58m |
59 | m | 59m |
60 | m | 60m |
61 | m | 61m |
62 | m | 62m |
63 | m | 63m |
64 | m | 64m |
65 | m | 65m |
66 | m | 66m |
67 | m | 67m |
68 | m | 68m |
69 | m | 69m |
70 | m | 70m |
71 | m | 71m |
72 | m | 72m |
73 | m | 73m |
74 | m | 74m |
75 | m | 75m |
76 | m | 76m |
77 | m | 77m |
78 | m | 78m |
79 | m | 79m |
80 | m | 80m |
81 | m | 81m |
82 | m | 82m |
83 | m | 83m |
84 | m | 84m |
85 | m | 85m |
86 | m | 86m |
87 | m | 87m |
88 | m | 88m |
89 | m | 89m |
90 | m | 90m |
91 | m | 91m |
92 | m | 92m |
93 | m | 93m |
94 | m | 94m |
95 | m | 95m |
96 | m | 96m |
97 | m | 97m |
98 | m | 98m |
99 | m | 99m |
100 | m | 100m |
Now b has the values 1..100 in miles.
Let us convert all of these values to km. b<>km;
The answer is given as:
Numeric,Units | UNITSOF |
---|---|
1,km | 1.6093439999999999 |
2,km | 3.2186879999999998 |
3,km | 4.828031999999999 |
4,km | 6.4373759999999995 |
5,km | 8.046719999999999 |
6,km | 9.656063999999999 |
7,km | 11.265407999999999 |
8,km | 12.874751999999999 |
9,km | 14.484096 |
10,km | 16.093439999999998 |
a=(1..100)<>℃
b=a<>℉
E:=(m<>kg)*(SPEEDOFLIGHT()<^>2); E(2kg)
a=1mi; b=10km; [a,b,(a<+>b)<>mi]
10mi<>km
Pi seems to be obfuscated.
SIN(90<>deg)
(rad2piby10)<>rad<>deg
((rad2piby10)<>rad<>deg)@SIN .graphin() //did not work in safari. (1..10)<>kg
b=(100$<+>3020¢); b<>$;
SETCONVERSION(1<>euro,1<>dollar,1.1)
SETCONVERSION(1$,1€,0.9); PRINTCONVERSION(); 345$<>€
1$<>1₹ // without conversion will fail.
(1..100)<>℃<>℉
SETCONVERSION(1<>€,1<>$,1.1); (1..100)<>€<>$
SETCONVERSION(1<>€,1<>$,1.10); (1..1000..10)<>€<>$
SETCONVERSION(1<>€,1<>$,1.10); (0..1000..20)<>€<>$
UNITIFY([1m,2m,3cm])
SUM(UNITIFY([1m,2m,3mm]))
(100$<+>300¢<+>30¢)<>$ PMT(4%,12,1000$,120000¢,0)
PMT(4%,12,1000$,1200.00¢,0) PMT(4%,12,1000$,120000¢,0)
1<>gal<>l 1<>l<>oz
(1..100)<>l<>ul 1Pa<>atm
E:=(m<>kg)<*>(SPEEDOFLIGHT()<^>2); b=E(2g); ConvertToUnitOfNature(b,"energy")
1m<>>1cm;
[1m,2m]<>>[1cm,200cm];
[1m,2m]<>=>[1cm,200cm];
[1m,2m]<<>[1cm,200cm];
[1m,2m]<<>[1cm,200cm];
[1m,2m]<==>[1cm,200cm];
[1m,2m]<!=>[1cm,200cm];
[1m,2m]<<=>[1cm,199.99cm];
[1m,2m]<<=>[1cm,200.01cm];
((1..10)<>m)<>>3m;
((0.1..10)<>m)<>>30cm
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. ##weight of zone=3m; ++(##weight of zone);
##weight of zone=3m; a=3.4mm<+>(##weight of zone);
//%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)
%planck %boltzmann; %planck length;
PRINTCONVERSIONS() CONSTANTS() 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