blob: c2e82a5e85b941c7f8e703806ed77a09fdb3177c (
plain) (
blame)
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
|
# unit.h
unit.h is a simple library for working with units and dimensioned quantities in C.
To use it, simply copy the `unit.h` file into your project and include it where necessary.
You must also define the `UNITH_IMPL` macro before importing it in one file so the
implementation is compiled.
## Working with dimensioned quantities
unit.h provides the U-macro, which accepts a string representing a unit or compound
unit and returns the conversion ratio between it and the corresponding SI unit. This
conversion ratio can be used to construct dimensioned quantities like so:
```
double water_density = 1.0 * U("g/cm^3");
```
Likewise, the UFMT and UARG macros can be used with `printf` to display these quantities
like so:
```
printf("The density of water is " UFMT ".\n", UARG(water_density, "kg/m^3"));
```
## Unit Strings
Units are defined as c-strings. Each string consists of a series of unit names separated
by whitespace, each unit may be raised to an arbitrary (non-)integer power with a
`^`. There may also be a single slash among the units, which negates the exponenents
of all following units.
In the case where there are no units before the slash, the number 1 must be used
before it instead: "1/cm^3".
## Misc. Notes
Angular measurements are internally represented using radians and steradians so they
can work with trigonometric functions.
SI prefixes may be used before unit names. (E.g. "Mm" is a megameter.)
|