Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
sci.h
Go to the documentation of this file.
1// Copyright 2010-2021:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Goby Underwater Autonomy Project Libraries
10// ("The Goby Libraries").
11//
12// The Goby Libraries are free software: you can redistribute them and/or modify
13// them under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// The Goby Libraries are distributed in the hope that they will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with Goby. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef GOBY_UTIL_SCI_H
26#define GOBY_UTIL_SCI_H
27
28#include <cmath>
29#include <map>
30
31namespace goby
32{
33namespace util
34{
36
37
44[[deprecated("use std::round() or dccl::round()")]] inline double unbiased_round(double r,
45 double dec)
46{
47 double ex = std::pow(10.0, dec);
48 double final = std::floor(r * ex);
49 double s = (r * ex) - final;
50
51 // remainder less than 0.5 or even number next to it
52 if (s < 0.5 || (s == 0.5 && !(static_cast<unsigned long>(final) & 1)))
53 return final / ex;
54 else
55 return (final + 1) / ex;
56}
57
59inline unsigned ceil_log2(unsigned v)
60{
61 // r will be one greater (ceil) if v is not a power of 2
62 unsigned r = ((v & (v - 1)) == 0) ? 0 : 1;
63 while (v >>= 1) r++;
64 return r;
65}
66
67inline unsigned ceil_log2(double d) { return ceil_log2(static_cast<unsigned>(std::ceil(d))); }
68
69inline unsigned ceil_log2(int i) { return ceil_log2(static_cast<unsigned>(i)); }
70
71[[deprecated("use std::log2()")]] inline double log2(double d) { return std::log2(d); }
72
78template <typename N1, typename N2> N2 linear_interpolate(N1 a, const std::map<N1, N2>& table)
79{
80 auto l_it = table.upper_bound(a);
81
82 // clip to max value
83 if (l_it == table.end())
84 {
85 return (--l_it)->second;
86 }
87 // clip to min value
88 else if (l_it == table.begin())
89 {
90 return l_it->second;
91 }
92 // linear interpolation
93 else
94 {
95 auto u_it = l_it;
96 --l_it;
97 auto a_u = u_it->first, a_l = l_it->first;
98 auto b_u = u_it->second, b_l = l_it->second;
99 return static_cast<double>((a - a_l) / (a_l - a_u)) * (b_l - b_u) + b_l;
100 }
101}
102
103} // namespace util
104
106
107} // namespace goby
108
109#endif
double unbiased_round(double r, double dec)
Definition sci.h:44
unsigned ceil_log2(unsigned v)
Definition sci.h:59
double log2(double d)
Definition sci.h:71
N2 linear_interpolate(N1 a, const std::map< N1, N2 > &table)
Linear interpolation function.
Definition sci.h:78
The global namespace for the Goby project.