TR-mbed 1.0
Loading...
Searching...
No Matches
AnnoyingScalar.h
Go to the documentation of this file.
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2011-2018 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#ifndef EIGEN_TEST_ANNOYING_SCALAR_H
11#define EIGEN_TEST_ANNOYING_SCALAR_H
12
13#include <ostream>
14
15#if EIGEN_COMP_GNUC
16#pragma GCC diagnostic ignored "-Wshadow"
17#endif
18
19#ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
25#endif
26
27// An AnnoyingScalar is a pseudo scalar type that:
28// - can randomly through an exception in operator +
29// - randomly allocate on the heap or initialize a reference to itself making it non trivially copyable, nor movable, nor relocatable.
30
32{
33 public:
34 AnnoyingScalar() { init(); *v = 0; }
35 AnnoyingScalar(long double _v) { init(); *v = _v; }
36 AnnoyingScalar(double _v) { init(); *v = _v; }
37 AnnoyingScalar(float _v) { init(); *v = _v; }
38 AnnoyingScalar(int _v) { init(); *v = _v; }
39 AnnoyingScalar(long _v) { init(); *v = _v; }
40 #if EIGEN_HAS_CXX11
41 AnnoyingScalar(long long _v) { init(); *v = _v; }
42 #endif
43 AnnoyingScalar(const AnnoyingScalar& other) { init(); *v = *(other.v); }
45 if(v!=&data)
46 delete v;
47 instances--;
48 }
49
50 void init() {
51 if(internal::random<bool>())
52 v = new float;
53 else
54 v = &data;
55 instances++;
56 }
57
59 {
60 #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
61 countdown--;
62 if(countdown<=0 && !dont_throw)
63 throw my_exception();
64 #endif
65 return AnnoyingScalar(*v+*other.v);
66 }
67
69 { return AnnoyingScalar(-*v); }
70
72 { return AnnoyingScalar(*v-*other.v); }
73
75 { return AnnoyingScalar((*v)*(*other.v)); }
76
78 { return AnnoyingScalar((*v)/(*other.v)); }
79
80 AnnoyingScalar& operator+=(const AnnoyingScalar& other) { *v += *other.v; return *this; }
81 AnnoyingScalar& operator-=(const AnnoyingScalar& other) { *v -= *other.v; return *this; }
82 AnnoyingScalar& operator*=(const AnnoyingScalar& other) { *v *= *other.v; return *this; }
83 AnnoyingScalar& operator/=(const AnnoyingScalar& other) { *v /= *other.v; return *this; }
84 AnnoyingScalar& operator= (const AnnoyingScalar& other) { *v = *other.v; return *this; }
85
86 bool operator==(const AnnoyingScalar& other) const { return *v == *other.v; }
87 bool operator!=(const AnnoyingScalar& other) const { return *v != *other.v; }
88 bool operator<=(const AnnoyingScalar& other) const { return *v <= *other.v; }
89 bool operator< (const AnnoyingScalar& other) const { return *v < *other.v; }
90 bool operator>=(const AnnoyingScalar& other) const { return *v >= *other.v; }
91 bool operator> (const AnnoyingScalar& other) const { return *v > *other.v; }
92
93 float* v;
94 float data;
95 static int instances;
96#ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
97 static int countdown;
98 static bool dont_throw;
99#endif
100};
101
103AnnoyingScalar imag(const AnnoyingScalar & ) { return 0; }
105AnnoyingScalar sqrt(const AnnoyingScalar &x) { return std::sqrt(*x.v); }
106AnnoyingScalar abs (const AnnoyingScalar &x) { return std::abs(*x.v); }
107AnnoyingScalar cos (const AnnoyingScalar &x) { return std::cos(*x.v); }
108AnnoyingScalar sin (const AnnoyingScalar &x) { return std::sin(*x.v); }
109AnnoyingScalar acos(const AnnoyingScalar &x) { return std::acos(*x.v); }
110AnnoyingScalar atan2(const AnnoyingScalar &y,const AnnoyingScalar &x) { return std::atan2(*y.v,*x.v); }
111
112std::ostream& operator<<(std::ostream& stream,const AnnoyingScalar& x) {
113 stream << (*(x.v));
114 return stream;
115}
116
118
119#ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
121bool AnnoyingScalar::dont_throw = false;
122#endif
123
124namespace Eigen {
125template<>
136
138
139namespace numext {
140template<>
142bool (isfinite)(const AnnoyingScalar& x) {
143 return (numext::isfinite)(*x.v);
144}
145}
146
147namespace internal {
148 template<> EIGEN_STRONG_INLINE double cast(const AnnoyingScalar& x) { return double(*x.v); }
149 template<> EIGEN_STRONG_INLINE float cast(const AnnoyingScalar& x) { return *x.v; }
150}
151} // namespace Eigen
152
155
158
159inline bool test_isApprox(const AnnoyingScalar &a, const AnnoyingScalar &b)
160{ return internal::isApprox(*a.v, *b.v, test_precision<float>()); }
161
163{ return test_isMuchSmallerThan(*a.v, *b.v); }
164
165#endif // EIGEN_TEST_ANNOYING_SCALAR_H
bool test_isApprox(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition AnnoyingScalar.h:159
std::ostream & operator<<(std::ostream &stream, const AnnoyingScalar &x)
Definition AnnoyingScalar.h:112
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
Definition AnnoyingScalar.h:110
AnnoyingScalar get_test_precision(const AnnoyingScalar &)
Definition AnnoyingScalar.h:153
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition AnnoyingScalar.h:104
bool test_isMuchSmallerThan(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition AnnoyingScalar.h:162
AnnoyingScalar test_relative_error(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition AnnoyingScalar.h:156
EIGEN_DEVICE_FUNC const AcosReturnType acos() const
Definition ArrayCwiseUnaryOps.h:297
ArrayXXi a
Definition Array_initializer_list_23_cxx11.cpp:1
EIGEN_DEVICE_FUNC CastXpr< NewType >::Type cast() const
Definition CommonCwiseUnaryOps.h:62
#define EIGEN_ALWAYS_INLINE
Definition Macros.h:932
#define EIGEN_DEVICE_FUNC
Definition Macros.h:976
#define EIGEN_STRONG_INLINE
Definition Macros.h:917
Scalar * b
Definition benchVecAdd.cpp:17
Definition AnnoyingScalar.h:32
float data
Definition AnnoyingScalar.h:94
static int instances
Definition AnnoyingScalar.h:95
AnnoyingScalar operator/(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:77
AnnoyingScalar operator+(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:58
AnnoyingScalar & operator+=(const AnnoyingScalar &other)
Definition AnnoyingScalar.h:80
AnnoyingScalar()
Definition AnnoyingScalar.h:34
bool operator>(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:91
AnnoyingScalar & operator*=(const AnnoyingScalar &other)
Definition AnnoyingScalar.h:82
AnnoyingScalar(double _v)
Definition AnnoyingScalar.h:36
void init()
Definition AnnoyingScalar.h:50
~AnnoyingScalar()
Definition AnnoyingScalar.h:44
float * v
Definition AnnoyingScalar.h:93
AnnoyingScalar(long _v)
Definition AnnoyingScalar.h:39
AnnoyingScalar(float _v)
Definition AnnoyingScalar.h:37
AnnoyingScalar(int _v)
Definition AnnoyingScalar.h:38
AnnoyingScalar(long double _v)
Definition AnnoyingScalar.h:35
static int countdown
Definition AnnoyingScalar.h:97
AnnoyingScalar operator-() const
Definition AnnoyingScalar.h:68
AnnoyingScalar & operator=(const AnnoyingScalar &other)
Definition AnnoyingScalar.h:84
bool operator==(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:86
AnnoyingScalar & operator-=(const AnnoyingScalar &other)
Definition AnnoyingScalar.h:81
AnnoyingScalar & operator/=(const AnnoyingScalar &other)
Definition AnnoyingScalar.h:83
static bool dont_throw
Definition AnnoyingScalar.h:98
AnnoyingScalar(const AnnoyingScalar &other)
Definition AnnoyingScalar.h:43
bool operator<=(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:88
bool operator<(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:89
bool operator>=(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:90
AnnoyingScalar operator-(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:71
AnnoyingScalar operator*(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:74
bool operator!=(const AnnoyingScalar &other) const
Definition AnnoyingScalar.h:87
#define isfinite(X)
Definition main.h:95
#define abs(x)
Definition datatypes.h:17
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition gnuplot_common_settings.hh:12
Scalar * y
Definition level1_cplx_impl.h:124
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool() isfinite(const Eigen::bfloat16 &h)
Definition BFloat16.h:671
EIGEN_DEVICE_FUNC const Scalar & x
Definition SpecialFunctionsImpl.h:1990
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
float test_precision< float >()
Definition main.h:416
AnnoyingScalar test_precision< AnnoyingScalar >()
Definition AnnoyingScalar.h:137
Definition BandTriangularSolver.h:13
float test_precision< float >()
Definition spbenchsolver.h:91
@ RequireInitialization
Definition NumTraits.h:158
AnnoyingScalar NonInteger
Definition AnnoyingScalar.h:134
AnnoyingScalar Nested
Definition AnnoyingScalar.h:132
AnnoyingScalar Real
Definition AnnoyingScalar.h:131
AnnoyingScalar Literal
Definition AnnoyingScalar.h:133
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:233
Definition ForwardDeclarations.h:17
Definition main.h:101
Definition AnnoyingScalar.h:21
~my_exception()
Definition AnnoyingScalar.h:23
my_exception()
Definition AnnoyingScalar.h:22
Definition main.h:100