src/Utils.cpp

Sat, 19 Feb 2022 22:17:09 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 19 Feb 2022 22:17:09 +0100
changeset 20
fcbbddcc22c1
child 29
76846c99f827
permissions
-rw-r--r--

Completed the Fermentables editor.

20
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * Utils.cpp is part of bmsapp.
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * bmsapp is free software: you can redistribute it and/or modify
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * the Free Software Foundation, either version 3 of the License, or
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 * (at your option) any later version.
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 *
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * bmsapp is distributed in the hope that it will be useful,
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 * GNU General Public License for more details.
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 *
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 */
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 #include "Utils.h"
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 #include <math.h>
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 double Utils::Round(double n, int d)
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 {
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 int m;
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 for (int i = 0, m = 1; i < d; i++, m *= 10);
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 return round(n * m) / m;
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 }
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 double Utils::lintner_to_kolbach(double lintner)
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 {
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 double wk = (3.5 * lintner) - 16;
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 if (wk < 0)
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 return 0.0;
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 return wk;
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 }
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 double Utils::kolbach_to_lintner(double kolbach)
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 {
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 return Round((kolbach + 16) / 3.5, 3);
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 }
fcbbddcc22c1 Completed the Fermentables editor.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43

mercurial