Goby v2
sci.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef SCI20100713H
24 #define SCI20100713H
25 
26 #include <cmath>
27 
28 namespace goby
29 {
30 namespace util
31 {
33 
34 
41 inline double unbiased_round(double r, double dec)
42 {
43  double ex = std::pow(10.0, dec);
44  double final = std::floor(r * ex);
45  double s = (r * ex) - final;
46 
47  // remainder less than 0.5 or even number next to it
48  if (s < 0.5 || (s == 0.5 && !(static_cast<unsigned long>(final) & 1)))
49  return final / ex;
50  else
51  return (final + 1) / ex;
52 }
53 
60 inline double mackenzie_soundspeed(double T, double S, double D)
61 {
62  return 1448.96 + 4.591 * T - 5.304e-2 * T * T + 2.374e-4 * T * T * T + 1.340 * (S - 35) +
63  1.630e-2 * D + 1.675e-7 * D * D - 1.025e-2 * T * (S - 35) - 7.139e-13 * T * D * D * D;
64 }
65 
67 inline unsigned ceil_log2(unsigned v)
68 {
69  // r will be one greater (ceil) if v is not a power of 2
70  unsigned r = ((v & (v - 1)) == 0) ? 0 : 1;
71  while (v >>= 1) r++;
72  return r;
73 }
74 
75 inline unsigned ceil_log2(double d) { return ceil_log2(static_cast<unsigned>(std::ceil(d))); }
76 
77 inline unsigned ceil_log2(int i) { return ceil_log2(static_cast<unsigned>(i)); }
78 
79 inline double log2(double d)
80 {
81  static double log_2 = log(2);
82  return log(d) / log_2;
83 }
84 
85 } // namespace util
86 
88 
89 } // namespace goby
90 
91 #endif
The global namespace for the Goby project.