File: test_cmath.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (175 lines) | stat: -rw-r--r-- 5,979 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Boost.Units - A C++ library for zero-overhead dimensional analysis and 
// unit/quantity manipulation and conversion
//
// Copyright (C) 2003-2008 Matthias Christian Schabel
// Copyright (C) 2008 Steven Watanabe
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

/** 
\file
    
\brief test_units_1.cpp

\detailed
Test unit class.

Output:
@verbatim
@endverbatim
**/

#include <limits>

#include <boost/units/cmath.hpp>

#include "test_header.hpp"

namespace bu = boost::units;

static volatile double  zero = 0;

int test_main(int,char *[])
{
    double  inf = std::numeric_limits<double>::infinity(),
            nan = 0.0/zero;
    
    // default constructor
    const bu::quantity<bu::energy>          E1(0.0*bu::joules),
                                            E2(inf*bu::joules),
                                            E3(nan*bu::joules); 
    
    BOOST_CHECK((bu::isfinite)(E1) == true);
    BOOST_CHECK((bu::isfinite)(E2) == false);
    BOOST_CHECK((bu::isfinite)(E3) == false);

    BOOST_CHECK((bu::isinf)(E1) == false);
    BOOST_CHECK((bu::isinf)(E2) == true);
    BOOST_CHECK((bu::isinf)(E3) == false);
    
    BOOST_CHECK((bu::isnan)(E1) == false);
    BOOST_CHECK((bu::isnan)(E2) == false);
    BOOST_CHECK((bu::isnan)(E3) == true);
    
    BOOST_CHECK((bu::isnormal)(E1) == false);
    BOOST_CHECK((bu::isnormal)(E2) == false);
    BOOST_CHECK((bu::isnormal)(E3) == false);

    const bu::quantity<bu::energy>          E4(-2.5*bu::joules),
                                            E5(2.5*bu::joules); 
                                            
    BOOST_CHECK((bu::isgreater)(E4,E5) == false);
    BOOST_CHECK((bu::isgreater)(E5,E4) == true);
    BOOST_CHECK((bu::isgreater)(E4,E4) == false);
    BOOST_CHECK((bu::isgreater)(E3,E4) == false);
    BOOST_CHECK((bu::isgreater)(E4,E3) == false);

    BOOST_CHECK((bu::isgreaterequal)(E4,E5) == false);
    BOOST_CHECK((bu::isgreaterequal)(E5,E4) == true);
    BOOST_CHECK((bu::isgreaterequal)(E4,E4) == true);
    BOOST_CHECK((bu::isgreaterequal)(E3,E4) == false);
    BOOST_CHECK((bu::isgreaterequal)(E4,E3) == false);

    BOOST_CHECK((bu::isless)(E4,E5) == true);
    BOOST_CHECK((bu::isless)(E5,E4) == false);
    BOOST_CHECK((bu::isless)(E4,E4) == false);
    BOOST_CHECK((bu::isless)(E3,E4) == false);
    BOOST_CHECK((bu::isless)(E4,E3) == false);

    BOOST_CHECK((bu::islessequal)(E4,E5) == true);
    BOOST_CHECK((bu::islessequal)(E5,E4) == false);
    BOOST_CHECK((bu::islessequal)(E4,E4) == true);
    BOOST_CHECK((bu::islessequal)(E3,E4) == false);
    BOOST_CHECK((bu::islessequal)(E4,E3) == false);

    BOOST_CHECK((bu::islessgreater)(E4,E5) == true);
    BOOST_CHECK((bu::islessgreater)(E5,E4) == true);
    BOOST_CHECK((bu::islessgreater)(E4,E4) == false);
    BOOST_CHECK((bu::islessgreater)(E3,E4) == false);
    BOOST_CHECK((bu::islessgreater)(E4,E3) == false);

    BOOST_CHECK((bu::isunordered)(E4,E5) == false);
    BOOST_CHECK((bu::isunordered)(E5,E4) == false);
    BOOST_CHECK((bu::isunordered)(E4,E4) == false);
    BOOST_CHECK((bu::isunordered)(E3,E4) == true);
    BOOST_CHECK((bu::isunordered)(E4,E3) == true);

    BOOST_CHECK((bu::abs)(E4) == E5);
    BOOST_CHECK((bu::ceil)(E4) == -2.0*bu::joules);
    BOOST_CHECK((bu::copysign)(E4,E5) == E5);
    BOOST_CHECK((bu::fabs)(E4) == E5);
    BOOST_CHECK((bu::floor)(E4) == -3.0*bu::joules);
    BOOST_CHECK((bu::fdim)(E4,E5) == 0.0*bu::joules);
    BOOST_CHECK((bu::fdim)(E5,E4) == E5-E4);
    
    const bu::quantity<bu::length>  L1(3.0*bu::meters),
                                    L2(4.0*bu::meters);
    const bu::quantity<bu::area>    A1(4.0*bu::square_meters),
                                    A2(L1*L2+A1);

#if 0
    BOOST_CHECK((bu::fma)(L1,L2,A1) == A2);
#endif
       
    BOOST_CHECK((bu::fmax)(E4,E5) == E5);
    BOOST_CHECK((bu::fmin)(E4,E5) == E4);
    
    // need to test fpclassify
    
    BOOST_CHECK(bu::hypot(L1,L2) == 5.0*bu::meters);
    
#if 0

//    BOOST_CHECK(bu::llrint(E4).value() == bu::detail::llrint(E4.value()));
//    BOOST_CHECK(bu::llround(E4).value() == bu::detail::llround(E4.value()));
    BOOST_CHECK((bu::nearbyint)(E4).value() == (bu::detail::nearbyint)(E4.value()));
    BOOST_CHECK((bu::rint)(E4).value() == (bu::detail::rint)(E4.value()));

#endif

    BOOST_CHECK((bu::nextafter)(E4,E5).value() == (boost::math::nextafter)(E4.value(),E5.value()));
    BOOST_CHECK((bu::nextafter)(E5,E4).value() == (boost::math::nextafter)(E5.value(),E4.value()));

    BOOST_CHECK((bu::nexttoward)(E4,E5).value() == (boost::math::nextafter)(E4.value(),E5.value()));
    BOOST_CHECK((bu::nexttoward)(E5,E4).value() == (boost::math::nextafter)(E5.value(),E4.value()));

    BOOST_CHECK((bu::round)(E4 - 0.00000000001 * bu::joules) == -3.0*bu::joules);
    BOOST_CHECK((bu::round)(E5 + 0.00000000001 * bu::joules) == 3.0*bu::joules);    
    BOOST_CHECK((bu::signbit)(E4) != 0);
    BOOST_CHECK((bu::signbit)(E5) == 0);
    BOOST_CHECK((bu::trunc)(E4) == -2.0*bu::joules);
    BOOST_CHECK((bu::trunc)(E5) == 2.0*bu::joules);

    BOOST_CHECK((bu::fmod)(E4,E5) == -0.0*bu::joules);

    bu::quantity<bu::energy>    pint;
    
    BOOST_CHECK((bu::modf)(E4,&pint) == -0.5*bu::joules);
    BOOST_CHECK(pint == -2.0*bu::joules);
    
    int ex;
    const bu::quantity<bu::energy>  E6((bu::frexp)(E4,&ex));
    
    BOOST_CHECK(E6 == -0.625*bu::joules);
    BOOST_CHECK(ex == 2);
    BOOST_CHECK((bu::ldexp)(E6,ex) == E4);
    
    const bu::quantity<bu::dimensionless>   E7(1.0);
    
    BOOST_CHECK(bu::pow(E7,E7) == 1.0*1.0);
    
    const bu::quantity<bu::dimensionless>   E8((bu::exp)(E7));
    
    BOOST_CHECK(std::abs(E8 - std::exp(1.0)) < .000001);
    BOOST_CHECK(bu::log(E8) == E7);
    
    const bu::quantity<bu::dimensionless>   E9(100.0);
    
    BOOST_CHECK(bu::log10(E9) == 2.0);
    
    BOOST_CHECK(bu::sqrt(A1) == 2.0*bu::meters);
    
    return 0;
}