Goby v2
sci.cpp
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 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include "goby/util/sci.h"
23 #include <cassert>
24 #include <iostream>
25 
26 using namespace goby::util;
27 
28 bool double_cmp(double a, double b, int precision)
29 {
30  int a_whole = a;
31  int b_whole = b;
32 
33  int a_part = (a - a_whole) * pow(10.0, precision);
34  int b_part = (b - b_whole) * pow(10.0, precision);
35 
36  return (a_whole == b_whole) && (a_part == b_part);
37 }
38 
39 int main()
40 {
41  assert(ceil_log2(1023) == 10);
42  assert(ceil_log2(1024) == 10);
43  assert(ceil_log2(1025) == 11);
44 
45  assert(ceil_log2(15) == 4);
46  assert(ceil_log2(16) == 4);
47  assert(ceil_log2(17) == 5);
48 
49  assert(ceil_log2(328529398) == 29);
50 
51  assert(unbiased_round(5.5, 0) == 6);
52  assert(unbiased_round(4.5, 0) == 4);
53 
54  assert(double_cmp(unbiased_round(4.123, 2), 4.12, 2));
55 
56  // check value for mackenzie
57  assert(double_cmp(unbiased_round(mackenzie_soundspeed(25, 35, 1000), 3), 1550.744, 3));
58 
59  std::cout << "all tests passed" << std::endl;
60  return 0;
61 }