pico-scale
mass.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2022 Daniel Robertson
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef MASS_H_62063E22_421F_4578_BAD8_46AF5C66C4CF
24 #define MASS_H_62063E22_421F_4578_BAD8_46AF5C66C4CF
25 
26 #include <stdbool.h>
27 #include <stddef.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 static const double MASS_RATIOS[] = {
34  1.0,
35  1000.0,
36  1000000.0,
37  1000000000.0,
38  1000000000000.0,
39  1016046908800.0,
40  907184740000.0,
41  6350293180.0,
42  453592370.0,
43  28349523.125
44 };
45 
46 static const char* const MASS_NAMES[] = {
47  "μg",
48  "mg",
49  "g",
50  "kg",
51  "ton",
52  "ton (IMP)",
53  "ton (US)",
54  "st",
55  "lb",
56  "oz"
57 };
58 
59 static const size_t MASS_TO_STRING_BUFF_SIZE = 64;
60 
64 typedef enum {
65  mass_ug = 0,
74  mass_oz
76 
83 const char* const mass_unit_to_string(const mass_unit_t u);
84 
91 const double* const mass_unit_to_ratio(const mass_unit_t u);
92 
93 typedef struct {
94  double ug;
96 } mass_t;
97 
106 void mass_convert(
107  const double* const fromAmount,
108  double* const toAmount,
109  const mass_unit_t fromUnit,
110  const mass_unit_t toUnit);
111 
119 void mass_init(
120  mass_t* const m,
121  const mass_unit_t unit,
122  const double val);
123 
130 void mass_get_value(
131  const mass_t* const m,
132  double* const val);
133 
141 void mass_add(
142  const mass_t* const lhs,
143  const mass_t* const rhs,
144  mass_t* const res);
145 
153 void mass_sub(
154  const mass_t* const lhs,
155  const mass_t* const rhs,
156  mass_t* const res);
157 
165 void mass_mul(
166  const mass_t* const lhs,
167  const mass_t* const rhs,
168  mass_t* const res);
169 
179 bool mass_div(
180  const mass_t* const lhs,
181  const mass_t* const rhs,
182  mass_t* const res);
183 
190 void mass_addeq(
191  mass_t* const self,
192  const mass_t* const rhs);
193 
200 void mass_subeq(
201  mass_t* const self,
202  const mass_t* const rhs);
203 
210 void mass_muleq(
211  mass_t* const self,
212  const mass_t* const rhs);
213 
222 bool mass_diveq(
223  mass_t* const self,
224  const mass_t* const rhs);
225 
234 bool mass_eq(
235  const mass_t* const lhs,
236  const mass_t* const rhs);
237 
246 bool mass_neq(
247  const mass_t* const lhs,
248  const mass_t* const rhs);
249 
258 bool mass_lt(
259  const mass_t* const lhs,
260  const mass_t* const rhs);
261 
270 bool mass_gt(
271  const mass_t* const lhs,
272  const mass_t* const rhs);
273 
282 bool mass_lteq(
283  const mass_t* const lhs,
284  const mass_t* const rhs);
285 
294 bool mass_gteq(
295  const mass_t* const lhs,
296  const mass_t* const rhs);
297 
305 int mass_to_string(
306  const mass_t* const m,
307  char* const buff);
308 
309 #ifdef __cplusplus
310 }
311 #endif
312 
313 #endif
static const double MASS_RATIOS[]
Definition: mass.h:33
const double *const mass_unit_to_ratio(const mass_unit_t u)
Returns a pointer to a double with the number of micrograms per unit.
Definition: mass.c:36
bool mass_diveq(mass_t *const self, const mass_t *const rhs)
Divide self by rhs, returns false if rhs is 0.
Definition: mass.c:167
int mass_to_string(const mass_t *const m, char *const buff)
Fills buff with the string representation of the mass_t. eg. "32.4762 mg".
Definition: mass.c:241
bool mass_lteq(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs is less than or equal to rhs.
Definition: mass.c:218
const char *const mass_unit_to_string(const mass_unit_t u)
Returns a pointer to char with the textual representation of the unit.
Definition: mass.c:32
void mass_add(const mass_t *const lhs, const mass_t *const rhs, mass_t *const res)
Add lhs to rhs and store result in res.
Definition: mass.c:87
bool mass_gteq(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs is greater than or equal to rhs.
Definition: mass.c:229
void mass_subeq(mass_t *const self, const mass_t *const rhs)
Subtract rhs from self.
Definition: mass.c:155
void mass_mul(const mass_t *const lhs, const mass_t *const rhs, mass_t *const res)
Multiply lhs by rhs and store result in res.
Definition: mass.c:115
mass_unit_t
Definition: mass.h:64
@ mass_g
Definition: mass.h:67
@ mass_imp_ton
Definition: mass.h:70
@ mass_us_ton
Definition: mass.h:71
@ mass_mg
Definition: mass.h:66
@ mass_oz
Definition: mass.h:74
@ mass_lb
Definition: mass.h:73
@ mass_ton
Definition: mass.h:69
@ mass_ug
Definition: mass.h:65
@ mass_kg
Definition: mass.h:68
@ mass_st
Definition: mass.h:72
void mass_convert(const double *const fromAmount, double *const toAmount, const mass_unit_t fromUnit, const mass_unit_t toUnit)
Converts a floating point value from one unit to another.
Definition: mass.c:40
bool mass_gt(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs is greater than rhs.
Definition: mass.c:207
void mass_init(mass_t *const m, const mass_unit_t unit, const double val)
Initialises a mass_t with the given unit and value.
Definition: mass.c:64
static const char *const MASS_NAMES[]
Definition: mass.h:46
bool mass_neq(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs does not equal rhs.
Definition: mass.c:185
void mass_muleq(mass_t *const self, const mass_t *const rhs)
Multiply self by rhs.
Definition: mass.c:161
void mass_addeq(mass_t *const self, const mass_t *const rhs)
Add rhs to self.
Definition: mass.c:149
void mass_sub(const mass_t *const lhs, const mass_t *const rhs, mass_t *const res)
Substract rhs from lhs and store result in res.
Definition: mass.c:101
bool mass_eq(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs equals rhs.
Definition: mass.c:173
bool mass_div(const mass_t *const lhs, const mass_t *const rhs, mass_t *const res)
Divide lhs by rhs and store result in res, returns false if rhs is 0.
Definition: mass.c:129
bool mass_lt(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs is less than rhs.
Definition: mass.c:196
static const size_t MASS_TO_STRING_BUFF_SIZE
Definition: mass.h:59
void mass_get_value(const mass_t *const m, double *const val)
Sets val to the value representing the mass_t according to its unt.
Definition: mass.c:76
Definition: mass.h:93
double ug
Definition: mass.h:94
mass_unit_t unit
Definition: mass.h:95