From 8ea29c87205ec37a07026c9cde262a9572d91450 Mon Sep 17 00:00:00 2001 From: Joseph Elliott Hand Date: Thu, 30 Apr 2026 23:00:13 -1000 Subject: Initial commit Signed-off-by: Joseph Elliott Hand --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 README.md (limited to 'README.md') 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.) + -- cgit v1.2.3