Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
test_floatepsilon01.cpp
Go to the documentation of this file.
1 /*
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include <iostream>
25 #include <cassert>
26 #include <cinttypes>
27 #include <cstring>
28 
29 #define CATCH_CONFIG_MAIN
30 #include <catch2/catch_amalgamated.hpp>
31 #include <jau/test/catch2_ext.hpp>
32 
33 #include <jau/float_math.hpp>
34 #include <jau/basic_types.hpp>
35 
36 using namespace jau;
37 
38 TEST_CASE( "Float Epsilon Test 01", "[datatype][float][epsilon]" ) {
39  static float epsilon_f0 = std::numeric_limits<float>::epsilon();
40  static double epsilon_d0 = std::numeric_limits<double>::epsilon();
41 
42  static float epsilon_f1 = jau::machineEpsilon<float>();
43  static double epsilon_d1 = jau::machineEpsilon<double>();
44 
45  float epsilon_fd = epsilon_f1 - epsilon_f0;
46  float epsilon_dd = epsilon_d1 - epsilon_d0;
47 
48  fprintf(stderr, "std::numeric_limits<float>::epsilon() : %e\n", epsilon_f0);
49  fprintf(stderr, "std::numeric_limits<double>::epsilon() : %le\n", epsilon_d0);
50  fprintf(stderr, "jau::machineEpsilon<float>() : %e\n", epsilon_f1);
51  fprintf(stderr, "jau::machineEpsilon<double>() : %le\n", epsilon_d1);
52 
53  fprintf(stderr, "float: approximation - numeric_limits : %e\n", epsilon_fd);
54  fprintf(stderr, "double: approximation - numeric_limits : %le\n", epsilon_dd);
55 
56  REQUIRE(jau::machine_equal(epsilon_f1, epsilon_f0, 5));
57  REQUIRE(jau::machine_equal(epsilon_d1, epsilon_d0, 5));
58 }
59 
60 
float_math.hpp
jau
Definition: basic_algos.hpp:34
TEST_CASE
TEST_CASE("Float Epsilon Test 01", "[datatype][float][epsilon]")
Definition: test_floatepsilon01.cpp:38
basic_types.hpp
catch2_ext.hpp
jau::machine_equal
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type machine_equal(const T &a, const T &b, int ulp=1, const T &epsilon=std::numeric_limits< T >::epsilon())
Returns true, if both floating point values are equal in the sense that their potential difference is...
Definition: float_math.hpp:83