diff options
| author | Joseph Elliott Hand <jhand@hawaii.edu> | 2026-04-30 23:00:13 -1000 |
|---|---|---|
| committer | Joseph Elliott Hand <jhand@hawaii.edu> | 2026-04-30 23:00:13 -1000 |
| commit | 8ea29c87205ec37a07026c9cde262a9572d91450 (patch) | |
| tree | 16ac32aa2f33879942d42ef835069eda1d2fcfeb /README.md | |
| download | unit.h-8ea29c87205ec37a07026c9cde262a9572d91450.tar.gz | |
Initial commit
Signed-off-by: Joseph Elliott Hand <jhand@hawaii.edu>
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2e82a5 --- /dev/null +++ b/README.md @@ -0,0 +1,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.) + |
