1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10 #include "main.h"
11
special_numbers()12 template<typename Scalar> void special_numbers()
13 {
14 typedef Matrix<Scalar, Dynamic,Dynamic> MatType;
15 int rows = internal::random<int>(1,300);
16 int cols = internal::random<int>(1,300);
17
18 Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
19 Scalar inf = std::numeric_limits<Scalar>::infinity();
20 Scalar s1 = internal::random<Scalar>();
21
22 MatType m1 = MatType::Random(rows,cols),
23 mnan = MatType::Random(rows,cols),
24 minf = MatType::Random(rows,cols),
25 mboth = MatType::Random(rows,cols);
26
27 int n = internal::random<int>(1,10);
28 for(int k=0; k<n; ++k)
29 {
30 mnan(internal::random<int>(0,rows-1), internal::random<int>(0,cols-1)) = nan;
31 minf(internal::random<int>(0,rows-1), internal::random<int>(0,cols-1)) = inf;
32 }
33 mboth = mnan + minf;
34
35 VERIFY(!m1.hasNaN());
36 VERIFY(m1.allFinite());
37
38 VERIFY(mnan.hasNaN());
39 VERIFY((s1*mnan).hasNaN());
40 VERIFY(!minf.hasNaN());
41 VERIFY(!(2*minf).hasNaN());
42 VERIFY(mboth.hasNaN());
43 VERIFY(mboth.array().hasNaN());
44
45 VERIFY(!mnan.allFinite());
46 VERIFY(!minf.allFinite());
47 VERIFY(!(minf-mboth).allFinite());
48 VERIFY(!mboth.allFinite());
49 VERIFY(!mboth.array().allFinite());
50 }
51
test_special_numbers()52 void test_special_numbers()
53 {
54 for(int i = 0; i < 10*g_repeat; i++) {
55 CALL_SUBTEST_1( special_numbers<float>() );
56 CALL_SUBTEST_1( special_numbers<double>() );
57 }
58 }
59