-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
95 lines (82 loc) · 4.46 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <iomanip>
#include "studis.hpp"
using namespace studis::literals;
using namespace studis::constants;
int main () {
std::cout << std::setprecision(12);
std::cout << std::boolalpha;
auto l1 = 1.5_m, l2 = 2_cm;
auto t = 3_s;
auto l3 = l1 + l2; // fine
std::cout << l3 << std::endl; // prints '1.52 m' (yes the unit is printed as well)
std::cout << l1 + l2 << std::endl; // same
std::cout << (l1 < l2) << std::endl; // works
// std::cout << l1 + t << std::endl; // error
// std::cout << (l1 < t) << std::endl; // error
auto speed = l1 / t;
std::cout << speed << std::endl; // prints '0.5 m/s'
// All math functions that make sense for quantities with dimension are overloaded
std::cout << abs (-1_A) << std::endl; // prints '1 A'
std::cout << atan2 (7_m, 1_km) << std::endl; // prints some number
// std::cout << atan2 (1_m, 1_s) << std::endl;// error
std::cout << hypot (3_m, 4_m) << std::endl; // prints '5 m'
// std::cout << hypot (1_m, 1_s) << std::endl;// error
// pow is the only function that has different signature than its std counterpart
// This can't be avoided since the dimension of output depends on the power
auto energy = 0.5 * 1_kg * pow<2> (speed);
std::cout << energy << std::endl; // prints '0.125 J (m2.kg/s2)'
// sqrt, cbrt are overloaded for quantities whose result is not a fractional dimension
// pi, standard_gravity and many other constants are defined in the constants namespace
auto pendulum_frequency = sqrt (standard_gravity / 1_m) / (2*pi);
std::cout << pendulum_frequency << std::endl; // prints '0.498402795329 Hz (1/s)'
std::cout << cbrt (1_litre) << std::endl; // prints '0.1 m'
// fractional power dimensions are not supported
// std::cout << sqrt (1_s) << std::endl; // error
// std::cout << cbrt (1_m2) << std::endl; // error
// Dimensionless quantities can be used with any math function
// Since they implicitly convert to a floating-point
auto pos = 1_cm * cos (2*pi*1_s*pendulum_frequency);
std::cout << pos << std::endl; // prints '-0.00999949644462 m'
// std::cout << cos (1_s) << std::endl; // error
// There are so many units and prefixes here
auto resistance = 1.7_kOhm; // we don't have greek letters, so that's kiloohm
auto inductance = 1_uH; // same, this is microhenry
auto capacitance = 1_pF;
if (resistance > 2*sqrt (inductance/capacitance))
std::cout << "overdamped" << std::endl;
else if (resistance == 2*sqrt (inductance/capacitance))
std::cout << "cricitally damped" << std::endl;
else std::cout << "underdamped" << std::endl;
// You can use STUDIS simply as a unit convertor (to SI units)
std::cout << 10_ly << std::endl; // prints '9.46073047258e+16 m'
std::cout << 30_knot << std::endl; // prints '15.4333333333 m/s'
std::cout << 1_MeV << std::endl; // prints '1.602176634e-13 J (m2.kg/s2)'
std::cout << 2000_kcal << std::endl; // prints '8368000000 J (m2.kg/s2)'
std::cout << 120_mmHg << std::endl; // prints '15998.6864898 Pa (kg/m.s2)'
// And so many constants
auto radiative_power = Stefan_Boltzmann_constant * pow<4>(300_K) * 1_m2;
std::cout << radiative_power << std::endl; // prints '459.300327954 W (m2.kg/s3)'
std::cout << electron_mass << std::endl; // prints '9.1093837015e-31 kg'
// Value of a (non-const) variable can change but its dimension can't
auto mass = 1_kg;
mass = 300_g; // fine
// mass = 1_m3; // error
// you can also read its value
std::cout << "Please enter a value (for mass)" << std::endl;
std::cin >> mass;
std::cout << mass << std::endl;
// If you don't want to specify an initial value (not recommended)
// You have to specify the dimension of the quantity
studis::Density d;
std::cout << "Please enter a value (for density)" << std::endl;
std::cin >> d;
std::cout << d << std::endl;
// Many common dimensions are there, but in the case you can't find it there
// You can specify the power for all 7 base dimensions of the SI yourself.
studis::Quantity<studis::Dimension<1,0,-3,0,0,0,0>> jerk;
std::cout << "Please enter a value (for jerk - change rate of acceleration)" << std::endl;
std::cin >> jerk;
std::cout << jerk << std::endl;
return 0;
}