Horizon
footol.h
1 /*
2  * Copyright 2018 Martin Ã…berg
3  *
4  * This file is part of Footag.
5  *
6  * Footag is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * Footag is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef FOOTAG_FOOTOL_H
21 #define FOOTAG_FOOTOL_H
22 
23 #include <math.h>
24 
25 /* interface for working with tolerances */
26 
27 struct footol {
28  double nom;
29  double min;
30  double max;
31 };
32 
33 static inline void footol_nom(
34  struct footol *tol,
35  double nom
36 )
37 {
38  tol->min = tol->max = tol->nom = nom;
39 }
40 
41 static inline void footol_minmax(
42  struct footol *tol,
43  double min,
44  double max
45 )
46 {
47  tol->min = min;
48  tol->max = max;
49  tol->nom = min + fabs(max - min) / 2;
50 }
51 
52 static inline void footol_minmaxnom(
53  struct footol *tol,
54  double min,
55  double max,
56  double nom
57 )
58 {
59  tol->min = min;
60  tol->max = max;
61  tol->nom = nom;
62 }
63 
64 static inline void footol_pm(
65  struct footol *tol,
66  double nom,
67  double plusminus
68 )
69 {
70  tol->nom = nom;
71  tol->min = nom - fabs(plusminus);
72  tol->max = nom + fabs(plusminus);
73 }
74 
75 static inline struct footol footol_auto(
76  double a,
77  double b
78 )
79 {
80  struct footol ret;
81  if (fabs(b) < fabs(a)) {
82  footol_pm(&ret, a, b);
83  } else {
84  footol_minmax(&ret, a, b);
85  }
86  return ret;
87 }
88 
89 static inline double footol_range(
90  const struct footol *const tol
91 )
92 {
93  return tol->max - tol->min;
94 }
95 
96 static const double FOOTOL_MM_PER_INCH = 25.4;
97 
98 static inline double footol_inchtomm(
99  double mmval
100 )
101 {
102  return mmval * FOOTOL_MM_PER_INCH;
103 }
104 
105 static inline struct footol footol_autoi(
106  double a,
107  double b
108 )
109 {
110  return footol_auto(footol_inchtomm(a), footol_inchtomm(b));
111 }
112 
113 #endif
114 
footol
Definition: footol.h:27