pico-scale
mass.h File Reference
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  mass_t
 

Enumerations

enum  mass_unit_t {
  mass_ug = 0 , mass_mg , mass_g , mass_kg ,
  mass_ton , mass_imp_ton , mass_us_ton , mass_st ,
  mass_lb , mass_oz
}
 

Functions

const char *const mass_unit_to_string (const mass_unit_t u)
 Returns a pointer to char with the textual representation of the unit. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
void mass_addeq (mass_t *const self, const mass_t *const rhs)
 Add rhs to self. More...
 
void mass_subeq (mass_t *const self, const mass_t *const rhs)
 Subtract rhs from self. More...
 
void mass_muleq (mass_t *const self, const mass_t *const rhs)
 Multiply self by rhs. More...
 
bool mass_diveq (mass_t *const self, const mass_t *const rhs)
 Divide self by rhs, returns false if rhs is 0. More...
 
bool mass_eq (const mass_t *const lhs, const mass_t *const rhs)
 Returns true if lhs equals rhs. More...
 
bool mass_neq (const mass_t *const lhs, const mass_t *const rhs)
 Returns true if lhs does not equal rhs. More...
 
bool mass_lt (const mass_t *const lhs, const mass_t *const rhs)
 Returns true if lhs is less than rhs. More...
 
bool mass_gt (const mass_t *const lhs, const mass_t *const rhs)
 Returns true if lhs is greater than rhs. More...
 
bool mass_lteq (const mass_t *const lhs, const mass_t *const rhs)
 Returns true if lhs is less than or equal to rhs. More...
 
bool mass_gteq (const mass_t *const lhs, const mass_t *const rhs)
 Returns true if lhs is greater than or equal to rhs. More...
 
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". More...
 

Variables

static const double MASS_RATIOS []
 
static const char *const MASS_NAMES []
 
static const size_t MASS_TO_STRING_BUFF_SIZE = 64
 

Enumeration Type Documentation

◆ mass_unit_t

Enum values are mapped to MASS_RATIOS and MASS_NAMES

Enumerator
mass_ug 
mass_mg 
mass_g 
mass_kg 
mass_ton 
mass_imp_ton 
mass_us_ton 
mass_st 
mass_lb 
mass_oz 

Definition at line 64 of file mass.h.

64  {
65  mass_ug = 0,
66  mass_mg,
67  mass_g,
68  mass_kg,
69  mass_ton,
72  mass_st,
73  mass_lb,
74  mass_oz
75 } mass_unit_t;
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

Function Documentation

◆ mass_add()

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.

Parameters
lhs
rhs
res

Definition at line 87 of file mass.c.

90  {
91 
92  assert(lhs != NULL);
93  assert(rhs != NULL);
94  assert(res != NULL);
95 
96  res->ug = lhs->ug + rhs->ug;
97  res->unit = lhs->unit;
98 
99 }
double ug
Definition: mass.h:94
mass_unit_t unit
Definition: mass.h:95

References mass_t::ug, and mass_t::unit.

Referenced by mass_addeq().

◆ mass_addeq()

void mass_addeq ( mass_t *const  self,
const mass_t *const  rhs 
)

Add rhs to self.

Parameters
self
rhs

Definition at line 149 of file mass.c.

151  {
152  mass_add(self, rhs, self);
153 }
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

References mass_add().

◆ mass_convert()

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.

Parameters
fromAmount
toAmount
fromUnit
toUnit

Definition at line 40 of file mass.c.

44  {
45 
46  assert(fromAmount != NULL);
47  assert(toAmount != NULL);
48 
49  if(fromUnit == toUnit) {
50  *toAmount = *fromAmount;
51  }
52  else if(toUnit == mass_ug) {
53  *toAmount = *fromAmount * *mass_unit_to_ratio(fromUnit);
54  }
55  else if(fromUnit == mass_ug) {
56  *toAmount = *fromAmount / *mass_unit_to_ratio(toUnit);
57  }
58  else {
59  mass_convert(fromAmount, toAmount, toUnit, mass_ug);
60  }
61 
62 }
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
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

References mass_convert(), mass_ug, and mass_unit_to_ratio().

Referenced by mass_convert(), mass_get_value(), and mass_init().

◆ mass_div()

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.

Parameters
lhs
rhs
res
Returns
true
false

Definition at line 129 of file mass.c.

132  {
133 
134  assert(lhs != NULL);
135  assert(rhs != NULL);
136  assert(res != NULL);
137 
138  //if ~0; protect against div / 0
139  if(fabs(rhs->ug) < DBL_EPSILON) {
140  return false;
141  }
142 
143  res->ug = lhs->ug / rhs->ug;
144  res->unit = lhs->unit;
145  return true;
146 
147 }

References mass_t::ug, and mass_t::unit.

Referenced by mass_diveq().

◆ mass_diveq()

bool mass_diveq ( mass_t *const  self,
const mass_t *const  rhs 
)

Divide self by rhs, returns false if rhs is 0.

Parameters
self
rhs
Returns
true
false

Definition at line 167 of file mass.c.

169  {
170  return mass_div(self, rhs, self);
171 }
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

References mass_div().

◆ mass_eq()

bool mass_eq ( const mass_t *const  lhs,
const mass_t *const  rhs 
)

Returns true if lhs equals rhs.

Parameters
lhs
rhs
Returns
true
false

Definition at line 173 of file mass.c.

175  {
176 
177  assert(lhs != NULL);
178  assert(rhs != NULL);
179 
180  //if ~==; if approx ~0
181  return fabs(lhs->ug - rhs->ug) < DBL_EPSILON;
182 
183 }

References mass_t::ug.

Referenced by mass_neq().

◆ mass_get_value()

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.

Parameters
m
val

Definition at line 76 of file mass.c.

78  {
79 
80  assert(m != NULL);
81  assert(val != NULL);
82 
83  mass_convert(&m->ug, val, mass_ug, m->unit);
84 
85 }

References mass_convert(), mass_ug, mass_t::ug, and mass_t::unit.

Referenced by mass_to_string().

◆ mass_gt()

bool mass_gt ( const mass_t *const  lhs,
const mass_t *const  rhs 
)

Returns true if lhs is greater than rhs.

Parameters
lhs
rhs
Returns
true
false

Definition at line 207 of file mass.c.

209  {
210 
211  assert(lhs != NULL);
212  assert(rhs != NULL);
213 
214  return mass_lt(rhs, lhs);
215 
216 }
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

References mass_lt().

Referenced by mass_lteq().

◆ mass_gteq()

bool mass_gteq ( const mass_t *const  lhs,
const mass_t *const  rhs 
)

Returns true if lhs is greater than or equal to rhs.

Parameters
lhs
rhs
Returns
true
false

Definition at line 229 of file mass.c.

231  {
232 
233  assert(lhs != NULL);
234  assert(rhs != NULL);
235 
236  return !mass_lt(lhs, rhs);
237 
238 }

References mass_lt().

◆ mass_init()

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.

Parameters
m
unit
val

Definition at line 64 of file mass.c.

67  {
68 
69  assert(m != NULL);
70 
71  mass_convert(&val, &m->ug, unit, mass_ug);
72  m->unit = unit;
73 
74 }

References mass_convert(), mass_ug, mass_t::ug, and mass_t::unit.

Referenced by scale_weight().

◆ mass_lt()

bool mass_lt ( const mass_t *const  lhs,
const mass_t *const  rhs 
)

Returns true if lhs is less than rhs.

Parameters
lhs
rhs
Returns
true
false

Definition at line 196 of file mass.c.

198  {
199 
200  assert(lhs != NULL);
201  assert(rhs != NULL);
202 
203  return lhs->ug < rhs->ug;
204 
205 }

References mass_t::ug.

Referenced by mass_gt(), and mass_gteq().

◆ mass_lteq()

bool mass_lteq ( const mass_t *const  lhs,
const mass_t *const  rhs 
)

Returns true if lhs is less than or equal to rhs.

Parameters
lhs
rhs
Returns
true
false

Definition at line 218 of file mass.c.

220  {
221 
222  assert(lhs != NULL);
223  assert(rhs != NULL);
224 
225  return !mass_gt(lhs, rhs);
226 
227 }
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

References mass_gt().

◆ mass_mul()

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.

Parameters
lhs
rhs
res

Definition at line 115 of file mass.c.

118  {
119 
120  assert(lhs != NULL);
121  assert(rhs != NULL);
122  assert(res != NULL);
123 
124  res->ug = lhs->ug * rhs->ug;
125  res->unit = lhs->unit;
126 
127 }

References mass_t::ug, and mass_t::unit.

Referenced by mass_muleq().

◆ mass_muleq()

void mass_muleq ( mass_t *const  self,
const mass_t *const  rhs 
)

Multiply self by rhs.

Parameters
self
rhs

Definition at line 161 of file mass.c.

163  {
164  mass_mul(self, rhs, self);
165 }
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

References mass_mul().

◆ mass_neq()

bool mass_neq ( const mass_t *const  lhs,
const mass_t *const  rhs 
)

Returns true if lhs does not equal rhs.

Parameters
lhs
rhs
Returns
true
false

Definition at line 185 of file mass.c.

187  {
188 
189  assert(lhs != NULL);
190  assert(rhs != NULL);
191 
192  return !mass_eq(lhs, rhs);
193 
194 }
bool mass_eq(const mass_t *const lhs, const mass_t *const rhs)
Returns true if lhs equals rhs.
Definition: mass.c:173

References mass_eq().

◆ mass_sub()

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.

Parameters
lhs
rhs
res

Definition at line 101 of file mass.c.

104  {
105 
106  assert(lhs != NULL);
107  assert(rhs != NULL);
108  assert(res != NULL);
109 
110  res->ug = lhs->ug - rhs->ug;
111  res->unit = lhs->unit;
112 
113 }

References mass_t::ug, and mass_t::unit.

Referenced by mass_subeq().

◆ mass_subeq()

void mass_subeq ( mass_t *const  self,
const mass_t *const  rhs 
)

Subtract rhs from self.

Parameters
self
rhs

Definition at line 155 of file mass.c.

157  {
158  mass_sub(self, rhs, self);
159 }
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

References mass_sub().

◆ mass_to_string()

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".

Parameters
m
buffMust be at least MASS_TO_STRING_BUFF_SIZE in length
Returns
int Returns result of snprintf

Definition at line 241 of file mass.c.

243  {
244 
245  assert(m != NULL);
246  assert(buff != NULL);
247 
248  double n; //value
249  mass_get_value(m, &n);
250  double i; //int part; discard
251  const double f = fabs(modf(n, &i)); //frac part
252  uint d = 0; //decimal count
253 
254  //if less than the epsilon, then it's ~0
255  if(f >= DBL_EPSILON) {
256  d = (uint)fmax(0, ceil(1 - log10(f)));
257  }
258 
259  return snprintf(
260  buff,
262  "%01.*f %s", //format
263  d, //how many decimals
264  n, //the value
265  mass_unit_to_string(m->unit)); //suffix (ie. "kg")
266 
267 }
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_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
static const size_t MASS_TO_STRING_BUFF_SIZE
Definition: mass.h:59

References mass_get_value(), MASS_TO_STRING_BUFF_SIZE, mass_unit_to_string(), and mass_t::unit.

◆ mass_unit_to_ratio()

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.

Parameters
u
Returns
const double* const

Definition at line 36 of file mass.c.

36  {
37  return &MASS_RATIOS[(uint)u];
38 }
static const double MASS_RATIOS[]
Definition: mass.h:33

References MASS_RATIOS.

Referenced by mass_convert().

◆ mass_unit_to_string()

const char* const mass_unit_to_string ( const mass_unit_t  u)

Returns a pointer to char with the textual representation of the unit.

Parameters
u
Returns
const char* const

Definition at line 32 of file mass.c.

32  {
33  return MASS_NAMES[(uint)u];
34 }
static const char *const MASS_NAMES[]
Definition: mass.h:46

References MASS_NAMES.

Referenced by mass_to_string().

Variable Documentation

◆ MASS_NAMES

const char* const MASS_NAMES[]
static
Initial value:
= {
"μg",
"mg",
"g",
"kg",
"ton",
"ton (IMP)",
"ton (US)",
"st",
"lb",
"oz"
}

Definition at line 46 of file mass.h.

Referenced by mass_unit_to_string().

◆ MASS_RATIOS

const double MASS_RATIOS[]
static
Initial value:
= {
1.0,
1000.0,
1000000.0,
1000000000.0,
1000000000000.0,
1016046908800.0,
907184740000.0,
6350293180.0,
453592370.0,
28349523.125
}

Definition at line 33 of file mass.h.

Referenced by mass_unit_to_ratio().

◆ MASS_TO_STRING_BUFF_SIZE

const size_t MASS_TO_STRING_BUFF_SIZE = 64
static

Definition at line 59 of file mass.h.

Referenced by mass_to_string().