1// Boost.Units - A C++ library for zero-overhead dimensional analysis and
2// unit/quantity manipulation and conversion
3//
4// Copyright (C) 2003-2008 Matthias Christian Schabel
5// Copyright (C) 2008 Steven Watanabe
6//
7// Distributed under the Boost Software License, Version 1.0. (See
8// accompanying file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10
11/**
12\file
13
14\brief kitchen_sink.cpp
15
16\details
17More extensive quantity tests.
18
19Output:
20@verbatim
21
22//[kitchen_sink_output_1
23S1 : 2
24X1 : 2
25X2 : (4/3)
26U1 : N
27U2 : J
28Q1 : 1 N
29Q2 : 2 J
30//]
31
32//[kitchen_sink_output_2
33U1*S1 : 2 N
34S1*U1 : 2 N
35U1/S1 : 0.5 N
36S1/U1 : 2 m^-1 kg^-1 s^2
37//]
38
39//[kitchen_sink_output_3
40U1+U1 : N
41U1-U1 : N
42U1*U1 : m^2 kg^2 s^-4
43U1/U1 : dimensionless
44U1*U2 : m^3 kg^2 s^-4
45U1/U2 : m^-1
46U1^X : m^2 kg^2 s^-4
47X1vU1 : m^(1/2) kg^(1/2) s^-1
48U1^X2 : m^(4/3) kg^(4/3) s^(-8/3)
49X2vU1 : m^(3/4) kg^(3/4) s^(-3/2)
50//]
51
52//[kitchen_sink_output_4
53Q1*S1 : 2 N
54S1*Q1 : 2 N
55Q1/S1 : 0.5 N
56S1/Q1 : 2 m^-1 kg^-1 s^2
57//]
58
59//[kitchen_sink_output_5
60U1*Q1 : 1 m^2 kg^2 s^-4
61Q1*U1 : 1 m^2 kg^2 s^-4
62U1/Q1 : 1 dimensionless
63Q1/U1 : 1 dimensionless
64//]
65
66//[kitchen_sink_output_6
67+Q1 : 1 N
68-Q1 : -1 N
69Q1+Q1 : 2 N
70Q1-Q1 : 0 N
71Q1*Q1 : 1 m^2 kg^2 s^-4
72Q1/Q1 : 1 dimensionless
73Q1*Q2 : 2 m^3 kg^2 s^-4
74Q1/Q2 : 0.5 m^-1
75Q1^X1 : 1 m^2 kg^2 s^-4
76X1vQ1 : 1 m^(1/2) kg^(1/2) s^-1
77Q1^X2 : 1 m^(4/3) kg^(4/3) s^(-8/3)
78X2vQ1 : 1 m^(3/4) kg^(3/4) s^(-3/2)
79//]
80
81//[kitchen_sink_output_7
82l1 == l2 false
83l1 != l2 true
84l1 <= l2 true
85l1 < l2 true
86l1 >= l2 false
87l1 > l2 false
88//]
89
90dimless = 1
91
92//[kitchen_sink_output_8
93v1 = 2 m s^-1
94//]
95
96//[kitchen_sink_output_9
97F = 1 N
98dx = 1 m
99E = 1 J
100//]
101
102//[kitchen_sink_output_10
103r = 5e-07 m
104P = 101325 Pa
105V = 5.23599e-19 m^3
106T = 310 K
107n = 2.05835e-17 mol
108R = 8.314472 m^2 kg s^-2 K^-1 mol^-1 (rel. unc. = 1.8e-06)
109//]
110
111//[kitchen_sink_output_11
112theta = 0.375 rd
113sin(theta) = 0.366273 dimensionless
114asin(sin(theta)) = 0.375 rd
115//]
116
117//[kitchen_sink_output_12
118V = (12.5,0) V
119I = (3,4) A
120Z = (1.5,-2) Ohm
121I*Z = (12.5,0) V
122//]
123
124//[kitchen_sink_output_13
125x+y-w = 0.48(+/-0.632772) m
126w*x = 9.04(+/-0.904885) m^2
127x/y = 0.666667(+/-0.149071) dimensionless
128//]
129
130//[kitchen_sink_output_14
131w*y^2/(u*x)^2 = 10.17(+/-3.52328) m^-1
132w/(u*x)^(1/2) = 3.19612(+/-0.160431) dimensionless
133//]
134
135//[kitchen_sink_output_15
136I*w = m^2 kg s^-1 rad^-1
137I*w/L = dimensionless
138I*w^2 = J
139//]
140
141//[kitchen_sink_output_16
1421 F
1431 kat
1441 S
1451 C
1461 V
1471 J
1481 N
1491 Hz
1501 lx
1511 H
1521 lm
1531 Wb
1541 T
1551 W
1561 Pa
1571 Ohm
158//]
159
160//[kitchen_sink_output_18
1611 farad
1621 katal
1631 siemen
1641 coulomb
1651 volt
1661 joule
1671 newton
1681 hertz
1691 lux
1701 henry
1711 lumen
1721 weber
1731 tesla
1741 watt
1751 pascal
1761 ohm
177//]
178
179@endverbatim
180**/
181
182#include <cmath>
183#include <complex>
184#include <iostream>
185
186#include <boost/typeof/std/complex.hpp>
187
188#include <boost/units/cmath.hpp>
189#include <boost/units/io.hpp>
190#include <boost/units/systems/si.hpp>
191#include <boost/units/systems/si/codata/physico-chemical_constants.hpp>
192#include <boost/units/systems/si/io.hpp>
193
194#include "measurement.hpp"
195
196namespace boost {
197
198namespace units {
199
200//[kitchen_sink_function_snippet_3
201/// the physical definition of work - computed for an arbitrary unit system
202template<class System,class Y>
203constexpr
204quantity<unit<energy_dimension,System>,Y>
205work(quantity<unit<force_dimension,System>,Y> F,
206 quantity<unit<length_dimension,System>,Y> dx)
207{
208 return F*dx;
209}
210//]
211
212//[kitchen_sink_function_snippet_4
213/// the ideal gas law in si units
214template<class Y>
215constexpr
216quantity<si::amount,Y>
217idealGasLaw(const quantity<si::pressure,Y>& P,
218 const quantity<si::volume,Y>& V,
219 const quantity<si::temperature,Y>& T)
220{
221 using namespace boost::units::si;
222
223 using namespace constants::codata;
224 return (P*V/(R*T));
225}
226//]
227
228} // namespace units
229
230} // namespace boost
231
232int main()
233{
234 using namespace boost::units;
235 using namespace boost::units::si;
236
237 {
238 //[kitchen_sink_snippet_1
239 /// scalar
240 const double s1 = 2;
241
242 const long x1 = 2;
243 const static_rational<4,3> x2;
244
245 /// define some units
246 force u1 = newton;
247 energy u2 = joule;
248
249 /// define some quantities
250 quantity<force> q1(1.0*u1);
251 quantity<energy> q2(2.0*u2);
252 //]
253
254 /// check scalar, unit, and quantity io
255 std::cout << "S1 : " << s1 << std::endl
256 << "X1 : " << x1 << std::endl
257 << "X2 : " << x2 << std::endl
258 << "U1 : " << u1 << std::endl
259 << "U2 : " << u2 << std::endl
260 << "Q1 : " << q1 << std::endl
261 << "Q2 : " << q2 << std::endl
262 << std::endl;
263
264 /// check scalar-unit algebra
265 std::cout //<< "U1+S1 : " << u1+s1 << std::endl // illegal
266 //<< "S1+U1 : " << s1+u1 << std::endl // illegal
267 //<< "U1-S1 : " << u1-s1 << std::endl // illegal
268 //<< "S1-U1 : " << s1-u1 << std::endl // illegal
269 << "U1*S1 : " << u1*s1 << std::endl
270 << "S1*U1 : " << s1*u1 << std::endl
271 << "U1/S1 : " << u1/s1 << std::endl
272 << "S1/U1 : " << s1/u1 << std::endl
273 << std::endl;
274
275 /// check unit-unit algebra
276 std::cout << "U1+U1 : " << u1+u1 << std::endl
277 << "U1-U1 : " << u1-u1 << std::endl
278 << "U1*U1 : " << u1*u1 << std::endl
279 << "U1/U1 : " << u1/u1 << std::endl
280 //<< "U1+U2 : " << u1+u2 << std::endl // illegal
281 //<< "U1-U2 : " << u1-u2 << std::endl // illegal
282 << "U1*U2 : " << u1*u2 << std::endl
283 << "U1/U2 : " << u1/u2 << std::endl
284 << "U1^X : " << pow<2>(x: u1) << std::endl
285 << "X1vU1 : " << root<2>(x: u1) << std::endl
286 << "U1^X2 : " << pow<static_rational<4,3> >(x: u1) << std::endl
287 << "X2vU1 : " << root<static_rational<4,3> >(x: u1) << std::endl
288 << std::endl;
289
290 /// check scalar-quantity algebra
291 std::cout //<< "Q1+S1 : " << q1+s1 << std::endl // illegal
292 //<< "S1+Q1 : " << s1+q1 << std::endl // illegal
293 //<< "Q1-S1 : " << q1-s1 << std::endl // illegal
294 //<< "S1-Q1 : " << s1-q1 << std::endl // illegal
295 << "Q1*S1 : " << q1*s1 << std::endl
296 << "S1*Q1 : " << s1*q1 << std::endl
297 << "Q1/S1 : " << q1/s1 << std::endl
298 << "S1/Q1 : " << s1/q1 << std::endl
299 << std::endl;
300
301 /// check unit-quantity algebra
302 std::cout //<< "U1+Q1 : " << u1+q1 << std::endl // illegal
303 //<< "Q1+U1 : " << q1+u1 << std::endl // illegal
304 //<< "U1-Q1 : " << u1-q1 << std::endl // illegal
305 //<< "Q1-U1 : " << q1-u1 << std::endl // illegal
306 << "U1*Q1 : " << u1*q1 << std::endl
307 << "Q1*U1 : " << q1*u1 << std::endl
308 << "U1/Q1 : " << u1/q1 << std::endl
309 << "Q1/U1 : " << q1/u1 << std::endl
310 << std::endl;
311
312 /// check quantity-quantity algebra
313 std::cout << "+Q1 : " << +q1 << std::endl
314 << "-Q1 : " << -q1 << std::endl
315 << "Q1+Q1 : " << q1+q1 << std::endl
316 << "Q1-Q1 : " << q1-q1 << std::endl
317 << "Q1*Q1 : " << q1*q1 << std::endl
318 << "Q1/Q1 : " << q1/q1 << std::endl
319 //<< "Q1+Q2 : " << q1+q2 << std::endl // illegal
320 //<< "Q1-Q2 : " << q1-q2 << std::endl // illegal
321 << "Q1*Q2 : " << q1*q2 << std::endl
322 << "Q1/Q2 : " << q1/q2 << std::endl
323 << "Q1^X1 : " << pow<2>(x: q1) << std::endl
324 << "X1vQ1 : " << root<2>(x: q1) << std::endl
325 << "Q1^X2 : " << pow<static_rational<4,3> >(x: q1) << std::endl
326 << "X2vQ1 : " << root<static_rational<4,3> >(x: q1) << std::endl
327 << std::endl;
328
329 //[kitchen_sink_snippet_2
330 /// check comparison tests
331 quantity<length> l1(1.0*meter),
332 l2(2.0*meters);
333 //]
334
335 std::cout << std::boolalpha
336 << "l1 == l2" << "\t" << (l1 == l2) << std::endl
337 << "l1 != l2" << "\t" << (l1 != l2) << std::endl
338 << "l1 <= l2" << "\t" << (l1 <= l2) << std::endl
339 << "l1 < l2 " << "\t" << (l1 < l2) << std::endl
340 << "l1 >= l2" << "\t" << (l1 >= l2) << std::endl
341 << "l1 > l2 " << "\t" << (l1 > l2) << std::endl
342 << std::endl;
343
344 //[kitchen_sink_snippet_3
345 /// check implicit unit conversion from dimensionless to value_type
346 const double dimless = (q1/q1);
347 //]
348
349 std::cout << "dimless = " << dimless << std::endl
350 << std::endl;
351
352 quantity<velocity> v1 = 2.0*meters/second;
353
354 std::cout << "v1 = " << v1 << std::endl
355 << std::endl;
356
357 //[kitchen_sink_snippet_4
358 /// test calcuation of work
359 quantity<force> F(1.0*newton);
360 quantity<length> dx(1.0*meter);
361 quantity<energy> E(work(F,dx));
362 //]
363
364 std::cout << "F = " << F << std::endl
365 << "dx = " << dx << std::endl
366 << "E = " << E << std::endl
367 << std::endl;
368
369 {
370 //[kitchen_sink_snippet_5
371 /// test ideal gas law
372 quantity<temperature> T = (273.+37.)*kelvin;
373 quantity<pressure> P = 1.01325e5*pascals;
374 quantity<length> r = 0.5e-6*meters;
375 quantity<volume> V = (4.0/3.0)*3.141592*pow<3>(x: r);
376 quantity<amount> n(idealGasLaw(P,V,T));
377 //]
378
379 std::cout << "r = " << r << std::endl
380 << "P = " << P << std::endl
381 << "V = " << V << std::endl
382 << "T = " << T << std::endl
383 << "n = " << n << std::endl
384 #if BOOST_UNITS_HAS_TYPEOF
385 << "R = " << constants::codata::R << std::endl
386 #else
387 << "no typeof" << std::endl
388 #endif // BOOST_UNITS_HAS_TYPEOF
389 << std::endl;
390 }
391
392 //[kitchen_sink_snippet_6
393 /// test trig stuff
394 quantity<plane_angle> theta = 0.375*radians;
395 quantity<dimensionless> sin_theta = sin(theta);
396 quantity<plane_angle> thetap = asin(val: sin_theta);
397 //]
398
399 std::cout << "theta = " << theta << std::endl
400 << "sin(theta) = " << sin_theta << std::endl
401 << "asin(sin(theta)) = " << thetap << std::endl
402 << std::endl;
403
404 /// test implicit conversion of dimensionless to value
405 double tmp = sin_theta;
406
407 tmp = sin_theta;
408
409 /// test implicit conversion from value to dimensionless
410 quantity<dimensionless> tmpp = tmp;
411
412 tmpp = tmp;
413
414 /// check complex quantities
415 typedef std::complex<double> complex_type;
416
417 //[kitchen_sink_snippet_7
418 quantity<electric_potential,complex_type> v = complex_type(12.5,0.0)*volts;
419 quantity<current,complex_type> i = complex_type(3.0,4.0)*amperes;
420 quantity<resistance,complex_type> z = complex_type(1.5,-2.0)*ohms;
421 //]
422
423 std::cout << "V = " << v << std::endl
424 << "I = " << i << std::endl
425 << "Z = " << z << std::endl
426 << "I*Z = " << i*z << std::endl
427 << std::endl;
428
429 /// check quantities using user-defined type encapsulating error propagation
430
431 //[kitchen_sink_snippet_8
432 quantity<length,measurement<double> >
433 u(measurement<double>(1.0,0.0)*meters),
434 w(measurement<double>(4.52,0.02)*meters),
435 x(measurement<double>(2.0,0.2)*meters),
436 y(measurement<double>(3.0,0.6)*meters);
437 //]
438
439 std::cout << "x+y-w = " << x+y-w << std::endl
440 << "w*x = " << w*x << std::endl
441 << "x/y = " << x/y << std::endl
442 << "w*y^2/(u*x)^2 = " << w*y*y/pow<2>(x: u*x) << std::endl
443 << "w/(u*x)^(1/2) = " << w/pow< static_rational<1,2> >(x: u*x)
444 << std::endl << std::endl;
445 }
446
447 /// check moment of inertia/angular momentum/rotational energy
448
449 //[kitchen_sink_snippet_9
450 std::cout << symbol_format
451 << "I*w = " << moment_of_inertia()*angular_velocity() << std::endl
452 << "I*w/L = " << moment_of_inertia()*angular_velocity()/angular_momentum() << std::endl
453 << "I*w^2 = " << moment_of_inertia()*pow<2>(x: angular_velocity()) << std::endl
454 << std::endl;
455 //]
456
457 //[kitchen_sink_snippet_10
458// std::cout << typename_format
459// << quantity<capacitance>(1.0*farad) << std::endl
460// << quantity<catalytic_activity>(1.0*katal) << std::endl
461// << quantity<conductance>(1.0*siemen) << std::endl
462// << quantity<electric_charge>(1.0*coulomb) << std::endl
463// << quantity<electric_potential>(1.0*volt) << std::endl
464// << quantity<energy>(1.0*joule) << std::endl
465// << quantity<force>(1.0*newton) << std::endl
466// << quantity<frequency>(1.0*hertz) << std::endl
467// << quantity<illuminance>(1.0*lux) << std::endl
468// << quantity<inductance>(1.0*henry) << std::endl
469// << quantity<luminous_flux>(1.0*lumen) << std::endl
470// << quantity<magnetic_flux>(1.0*weber) << std::endl
471// << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
472// << quantity<power>(1.0*watt) << std::endl
473// << quantity<pressure>(1.0*pascals) << std::endl
474// << quantity<resistance>(1.0*ohm) << std::endl
475// << std::endl;
476 //]
477
478 //[kitchen_sink_snippet_11
479// std::cout << raw_format
480// << quantity<capacitance>(1.0*farad) << std::endl
481// << quantity<catalytic_activity>(1.0*katal) << std::endl
482// << quantity<conductance>(1.0*siemen) << std::endl
483// << quantity<electric_charge>(1.0*coulomb) << std::endl
484// << quantity<electric_potential>(1.0*volt) << std::endl
485// << quantity<energy>(1.0*joule) << std::endl
486// << quantity<force>(1.0*newton) << std::endl
487// << quantity<frequency>(1.0*hertz) << std::endl
488// << quantity<illuminance>(1.0*lux) << std::endl
489// << quantity<inductance>(1.0*henry) << std::endl
490// << quantity<luminous_flux>(1.0*lumen) << std::endl
491// << quantity<magnetic_flux>(1.0*weber) << std::endl
492// << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
493// << quantity<power>(1.0*watt) << std::endl
494// << quantity<pressure>(1.0*pascals) << std::endl
495// << quantity<resistance>(1.0*ohm) << std::endl
496// << std::endl;
497 //]
498
499 //[kitchen_sink_snippet_12
500 std::cout << symbol_format
501 << quantity<capacitance>(1.0*farad) << std::endl
502 << quantity<catalytic_activity>(1.0*katal) << std::endl
503 << quantity<conductance>(1.0*siemen) << std::endl
504 << quantity<electric_charge>(1.0*coulomb) << std::endl
505 << quantity<electric_potential>(1.0*volt) << std::endl
506 << quantity<energy>(1.0*joule) << std::endl
507 << quantity<force>(1.0*newton) << std::endl
508 << quantity<frequency>(1.0*hertz) << std::endl
509 << quantity<illuminance>(1.0*lux) << std::endl
510 << quantity<inductance>(1.0*henry) << std::endl
511 << quantity<luminous_flux>(1.0*lumen) << std::endl
512 << quantity<magnetic_flux>(1.0*weber) << std::endl
513 << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
514 << quantity<power>(1.0*watt) << std::endl
515 << quantity<pressure>(1.0*pascals) << std::endl
516 << quantity<resistance>(1.0*ohm) << std::endl
517 << std::endl;
518 //]
519
520 //[kitchen_sink_snippet_13
521 std::cout << name_format
522 << quantity<capacitance>(1.0*farad) << std::endl
523 << quantity<catalytic_activity>(1.0*katal) << std::endl
524 << quantity<conductance>(1.0*siemen) << std::endl
525 << quantity<electric_charge>(1.0*coulomb) << std::endl
526 << quantity<electric_potential>(1.0*volt) << std::endl
527 << quantity<energy>(1.0*joule) << std::endl
528 << quantity<force>(1.0*newton) << std::endl
529 << quantity<frequency>(1.0*hertz) << std::endl
530 << quantity<illuminance>(1.0*lux) << std::endl
531 << quantity<inductance>(1.0*henry) << std::endl
532 << quantity<luminous_flux>(1.0*lumen) << std::endl
533 << quantity<magnetic_flux>(1.0*weber) << std::endl
534 << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
535 << quantity<power>(1.0*watt) << std::endl
536 << quantity<pressure>(1.0*pascals) << std::endl
537 << quantity<resistance>(1.0*ohm) << std::endl
538 << std::endl;
539 //]
540
541 return 0;
542}
543

source code of boost/libs/units/example/kitchen_sink.cpp