1 /* examples of units calculations from the Frink project
2 * see: http://futureboy.us/frinkdocs/index.html
3 * Many, but not all, of the examples there are included here.
6 (kill (all), load (ezunits), load (physical_constants), fpprec : 12, 0);
9 /* I can never remember which units have built-in definitions .... */
10 map (lambda ([x], is (x # false)),
11 map (get_unit_definition, '[gallon, inch, foot, liter, lbm, fluid_ounce, mile, hour, mph]));
12 [true, true, true, true, true, true, true, true, true];
14 /* "Math Operators" */
16 1 ` gallon^(1/3) `` inch;
19 bfloat ((20000 ` gallon)^(1/3) `` foot);
20 1.387925257499022b1 ` foot;
22 (rho_water : 1 ` kg/liter,
23 (20000 ` gallon)*(rho_water) `` lbm);
24 98322384000/589081 ` lbm;
26 block ([rho_sugar : 1.25b0 ` kg/liter],
27 declare_unit_conversion (1 ` cup = 8 ` fluid_ounce),
28 (250 ` gram)/(rho_sugar) `` cup);
29 8.453505675460749b-1 ` cup;
31 (1/4 ` mile) / (4.23b0 ` s) `` mile/hour;
32 2.127659574468085b2 ` mile/hour;
34 (declare_unit_conversion (1 ` gee = 981 ` cm/s^2, mph = mile/hour),
35 (329 ` mph) / (4.23b0 ` s) `` gee);
36 3.544319855023219b0 ` gee;
38 is (dimensions (foot) = dimensions (meter));
41 (declare_unit_conversion (square_foot = foot^2),
42 3 ` square_foot `` foot^2);
45 (declare_unit_conversion (cubic_foot = foot^3),
46 3 ` cubic_foot `` foot^3);
49 (3 ` foot)^2 `` square_foot;
52 (3 ` foot)^3 `` cubic_foot;
55 /* "Common Functions" */
57 (declare_unit_conversion (arc_degree = %pi/180, arc_minute = arc_degree/60, arc_second = arc_minute/60),
58 declare_unit_conversion (radian = 1),
62 sin (90 ` arc_degree);
65 cos (2 * %pi ` radian);
68 tan (30 ` arc_second);
71 sec (45 ` arc_degree);
77 cot (30 ` arc_second);
80 /* Just leave out these hyperbolic functions.
81 * Arguments for such functions are not typically interpreted as radians,
82 * from what I've seen. I suppose I could be mistaken about that.
84 sinh (90 ` arc_degree);
87 cosh (2 * %pi ` radian);
90 tanh (30 ` arc_second);
95 asin (0.1) `` arc_degree;
96 18.03013580908076/%pi ` arc_degree;
101 acsc (3/2) `` radian, numer;
102 0.7297276562269662 ` radian;
104 asec (3/2) `` radian, numer;
105 0.8410686705679303 ` radian;
107 acot (1/2) `` radian, numer;
108 1.107148717794091 ` radian;
110 atan ((3 ` inch) / (1 ` foot)) `` arc_minute, numer;
111 842.1746080755889 ` arc_minute;
113 atan2 (3 ` inch, 1 ` foot) `` arc_degree, numer;
114 14.03624346792648 ` arc_degree;
116 /* "Temperature Scales" */
133 /* "Sample Calculations" */
135 /* "Mass and Volume" */
137 (A_room : (10 ` foot)*(12 ` foot),
138 V_room : (10 ` foot) * (12 ` foot) * (8 ` foot) `` gallon);
141 V_room * rho_water `` lbm;
142 2718417272832/45359237 ` lbm;
144 (if get_unit_conversion ('ton) = false
145 then declare_unit_conversion (1 ` ton = 2000 ` lbm),
146 (2 ` ton)/(A_room * rho_water) `` foot);
147 5669904625/10618817472 ` foot;
152 (rho_alcohol : rho_water / rat (1.267),
153 declare_unit_conversion (percent = 1/100),
154 declare_units (volume_alcohol_beer, fluid_ounce),
155 volume_alcohol_beer * rho_alcohol = (rat (3.2) ` percent) * ((12 ` fluid_ounce - volume_alcohol_beer) * rho_water + volume_alcohol_beer * rho_alcohol),
156 expand_dimensional (%%),
157 map (expand, %%), /* HMM, EQUATION SIMPLIFIES DIFFERENTLY IN THE PRESENCE OF MRAT ... */
158 map (lambda ([x], x `` g), %%),
159 solve (map (first, expand (%%)), qty (volume_alcohol_beer)));
160 ['qty(volume_alcohol_beer) = 15204/31517];
162 volume_alcohol_beer : rhs (first (foo)) ` units (volume_alcohol_beer);
163 15204/31517 ` fluid_ounce;
165 (declare_unit_conversion (1 ` magnum = 60 ` fluid_ounce), /* NEED CORRECT DEFN OF MAGNUM HERE */
166 declare_unit_conversion (1 ` beer_equivalent = volume_alcohol_beer),
167 (1 ` magnum) * ((13.5b0 ` percent) ` liter/liter) `` beer_equivalent);
168 1.679082478295186b1 ` beer_equivalent;
170 jungle_juice_percent :
171 (declare_unit_conversion (1 ` proof = (1/2 ` percent) ` liter/liter),
172 (1.75b0 ` liter) * (190 ` proof) / (5 ` gallon) `` percent);
173 8.783720740908435b0 ` percent;
175 5 * (12 ` fluid_ounce) * jungle_juice_percent `` beer_equivalent;
176 1.092488265947952b1 ` beer_equivalent;
180 (declare_unit_conversion (1 ` beer_barrel = 31 ` gallon),
181 declare_unit_conversion (1 ` keg = 1/2 ` beer_barrel),
182 declare_unit_conversion (1 ` pony_key = 1/2 ` keg),
183 beer_case : 24 * (12 ` fluid_ounce),
184 (1 ` keg)/beer_case `` 1);
187 /* TODO: I'd like to have x `` percent => (100*x) ` percent when x is a scalar,
188 * but "``" can't handle that now. I'll have to work on that.
190 (volume_alcohol_beer_fraction : volume_alcohol_beer / (12 ` fluid_ounce),
191 (60 ` dollar) / ((1 ` keg) * volume_alcohol_beer_fraction) `` dollar/fluid_ounce);
192 472755/628432 ` dollar/fluid_ounce;
194 (6.99b0 ` dollar) / ((750 ` ml) * (13 ` percent)) `` dollar/fluid_ounce;
195 2.120194580942308b0 ` dollar/fluid_ounce;
197 (13.99b0 ` dollar) / ((1750 ` ml) * (80 ` proof)) `` dollar/fluid_ounce;
198 5.9104811225625b-1 ` dollar/fluid_ounce;
202 foo : (mass_moon : 7.36b22 ` kg,
203 ((1/4) * mass_moon) / ((4/3) * %pi * (500/2 ` km)^3) `` kg/m^3);
204 8.832b5/%pi ` kg/m^3;
206 foo / rho_water `` 1;
209 ev (%G * (1/4) * mass_moon / (500/2 ` km)^2, constvalue) `` gee;
210 2.002964354740061b0 ` gee;
212 /* "Fiscal Calculations" -- skip it, ezunits lacks date parsing stuff */
216 (declare_unit_conversion (1 ` TNT_gram_equivalent = 4184 ` J),
217 (51 ` TNT_gram_equivalent) / ((185 ` lbm) * (1 ` gee)) `` foot);
218 177820000000000000/209093336624403 ` foot;
220 (energy_density_gasoline : 35 ` MJ/liter,
221 declare_unit_conversion (1 ` gasoline_teaspoon_equivalent = energy_density_gasoline * (1 ` teaspoon) `` MJ),
222 51 ` TNT_gram_equivalent `` gasoline_teaspoon_equivalent);
223 1365657600/1104078437 ` gasoline_teaspoon_equivalent;
225 /* "Sniping Ebay Auctions" -- skip it, ezunits lacks date parsing stuff */
227 /* "Junkyard Wars" */
229 (declare_unit_conversion (1 ` barrel = 55 ` gallon),
230 ((1/2) ` short_ton) / ((1 ` barrel) * rho_water) `` 1);
233 (declare_unit_conversion (1 ` fathom = 6 ` foot),
234 ((2 ` fathom) * rho_water * (1 ` barrel) * (1 ` gee)) / ((40 ` W) * (1 ` minute)) `` 1);
235 1945404028974483/625000000000000;
237 (declare_unit_conversion (1 ` calorie = 4.184 ` J, 1 ` Calorie = 1000 ` calorie),
238 ((2 ` fathom) * rho_water * (1 ` barrel) * (1 ` gee)) `` Calorie);
239 5836212086923449/3268750000000000 ` Calorie;
243 2000 ` Calorie/day `` W;
246 /* "Microwave Cookery" */
248 (1100 ` W) * (30 ` s) / ((27 ` ounce) * (1 ` calorie/gram/K)) `` R;
249 40000000000/2156625541 ` R;
251 /* "Why is Superman so Lazy?" */
253 (sun_earth_distance : 150b6 ` km,
254 sun_power : 4 * %pi * sun_earth_distance^2 * (1353 ` W/m^2) `` GW,
255 earth_power : sun_power / (4 * %pi * sun_earth_distance^2) `` W/m^2,
256 charge_rate : earth_power * (12 ` foot^2) `` W);
259 (2 ` ton) * (7 ` foot) * (1 ` gee) / charge_rate `` s;
260 2.51766982027399b1 ` s;
262 ((225 ` lbm) + (135 ` lbm)) * (15000 ` foot) * (1 ` gee) / charge_rate `` minute;
263 8.092510136594969b1 ` minute;
267 (declare_unit_conversion (1 ` kiloton = 1000 * 1000 * 1000 ` TNT_gram_equivalent),
268 (12.5b0 ` kiloton) / (6 ` year + 9 ` month `` year) `` horsepower);
269 3.292531060702169b2 ` horsepower;
271 (12.5b0 ` kiloton) / (6 ` year + 9 ` month `` year) `` Calorie/day;
272 5.070094050244632b6 ` Calorie/day;
274 (12.5b0 ` kiloton) / (2000 ` Calorie/day) `` year;
275 1.711156741957563b4 ` year;
277 (declare_unit_conversion
278 (1 ` therm = 10^5 ` Btu,
279 1 ` ccf = 100 ` foot^3,
280 1 ` natural_gas_equivalent = (1 ` therm) / (1 ` ccf)),
281 (12.5b0 ` kiloton) / (1 ` natural_gas_equivalent) / ((6 ` year + 9 ` month) `` s) / (%pi * (0.5b0 ` inch)^2) * 10 `` mph,
283 2.909260954481533b2 ` mph;
285 /* "Advanced Farting" */
287 (molar_volume : 22.4b0 ` liter/mol,
288 h2_energy : (2000 ` ml) / molar_volume * (19 ` percent) * (285.8 ` kJ/mol) `` kJ);
289 4.848392857142857b0 ` kJ;
291 methane_energy: (2000 ` ml) / molar_volume * (3.2 ` percent) * (890.8 ` kJ/mol) `` kJ;
292 2.545142857142857b0 ` kJ;
294 (h2_energy + methane_energy) `` Calorie;
295 1.767097446052991b0 ` Calorie;
297 (12.5b0 ` kiloton) / ((h2_energy + methane_energy) / (1 ` day)) `` year;
298 1.936686339261734b7 ` year;
300 /* "More Incorrect Facts" */
304 (declare_unit_conversion (1 ` knot = 6000 ` foot/hour), /* PROBABLY NEED A MORE ACCURATE CONVERSION HERE */
305 (18 ` ton/hour) / (28 ` knot) / (0.85b0 ` kg/liter) `` gallon/foot,
307 3.310343765967668b1 ` foot/gallon;
309 /* "Hamburgers and Cars" */
311 (48055 ` dollar) / (3115 ` lbm);
312 1373/89 ` dollar/lbm;
314 /* "Get The Provisions" -- skip it, ezunits lacks currency conversions */
316 /* "Typing" -- skip it, ezunits lacks dictionary stuff */
318 /* "Biblical References" */
320 (declare_unit_conversion (1 ` biblical_cubit = 10 ` inch), /* NEED A MORE ACCURATE CONVERSION HERE */
321 depth : (29030 ` foot + 15 ` biblical_cubit - (2.4b0 ` inch/year) * (4000 ` year)) `` foot);
324 rainfall : depth / (40 ` day) `` foot/hour;
325 2.941927083333333b1 ` foot/hour;
329 (declare_unit_conversion (1 ` gasoline_gallon_equivalent = energy_density_gasoline * (1 ` gallon) `` MJ),
330 ev ((1 ` teaspoon) * rho_water * %c^2, constvalue) `` gasoline_gallon_equivalent);
331 3209839924060063/960000000 ` gasoline_gallon_equivalent;
333 /* "Days Old" -- omit date calculations since ezunits lacks it */
335 (declare_unit_conversion
336 (1 ` milli_arc_second = 1/1000 ` arc_second,
337 1 ` au = 93*10^6 ` mile), /* close enough ... ?? */
338 (1 ` au) / (96.74b0 ` milli_arc_second) / constvalue (%c) `` day);
339 3.870493221188194b4/%pi ` day;
341 (declare_unit_conversion (1 ` light_year = constvalue (%c) * 365.25b0 ` day),
342 (1 ` au) / (0.7723b0 ` arc_second) `` light_year);
343 13.27382643963228b0/%pi ` light_year;
345 /* "Finding Your Own Star" -- no equations here */
347 /* "Model Solar System" */
349 (earth_radius : (7980 ` mile) / 2, /* close enough ... ?? */
350 scale : earth_radius / (3.5b0 ` inch) `` 1);
353 (moon_radius : (2150 ` mile) / 2, /* close enough ... ?? */
354 moon_radius / scale `` inch);
355 9.429824561403509b-1 ` inch;
357 (moon_distance : 235b3 ` mile, /* close enough ... ?? */
358 moon_distance / scale `` feet);
359 1.717836257309942b1 ` feet;
361 earth_radius / moon_radius `` 1;
364 (%pi * earth_radius^2) / (%pi * moon_radius^2) `` 1;
367 (1.9b0 ` inch) / (17.5b0 ` foot) `` arc_minute;
368 9.771428571428571b1/%pi ` arc_minute;
370 2 * asin (moon_radius / (moon_radius + moon_distance) `` 1) `` arc_degree;
371 1.639315206860877b0/%pi ` arc_degree;
373 (jupiter_radius : 88b3 ` mile, /* close enough ... ?? */
374 jupiter_distance : 400b6 ` mile, /* close enough ... ?? */
375 [jupiter_radius / scale `` feet, jupiter_distance / scale `` mile]);
376 [6.432748538011696b0 ` feet, 5.537834485202906b0 ` mile];
378 /* altitude of the space shuttle in model */
379 (300 ` km) / scale `` inch;
380 1.635187347992984b-1 ` inch;
382 /* altitude of geosynchronous satellite in model */
383 (42164 ` km) / scale `` inch;
384 2.298201311359206b1 ` inch;
386 /* size of sun, other planets, and some moons in model */
387 (sun_radius : 800b3 ` mile, /* close enough ... ?? */
388 mercury_radius : 2500 ` mile, /* close enough ... ?? */
389 venus_radius : 7900 ` mile, /* close enough ... ?? */
390 mars_radius : 4000 ` mile, /* close enough ... ?? */
391 saturn_radius : 69000 ` mile, /* close enough ... ?? */
392 uranus_radius : 30000 ` mile, /* close enough ... ?? */
393 neptune_radius : 30000 ` mile, /* close enough ... ?? */
394 pluto_radius : 3000 ` mile, /* close enough ... ?? */
395 titan_radius : 3000 ` km, /* close enough ... ?? */
396 phobos_radius : 20 ` mile, /* close enough ... ?? */
397 map (lambda ([x], x / scale `` m, if qty (%%) > 100 then %% `` km elseif qty (%%) < 1/10 then %% `` mm else %%),
398 [sun_radius, mercury_radius, venus_radius, mars_radius, saturn_radius, uranus_radius, neptune_radius, pluto_radius, titan_radius, phobos_radius]));
399 [1.782456140350877b1 ` m, 5.570175438596491b1 ` mm, 1.760175438596491b-1 ` m,
400 8.912280701754386b1 ` mm, 1.537368421052632b0 ` m, 6.684210526315789b-1 ` m,
401 6.684210526315789b-1 ` m, 6.68421052631579b1 ` mm, 4.15337586390218b1 ` mm, 4.456140350877193b-1 ` mm];
403 /* no further calculations in frink-index.html */