• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost test/mul.cpp
2  * test multiplication, division, square and square root on some intervals
3  *
4  * Copyright 2002-2003 Guillaume Melquiond
5  *
6  * Distributed under the Boost Software License, Version 1.0.
7  * (See accompanying file LICENSE_1_0.txt or
8  * copy at http://www.boost.org/LICENSE_1_0.txt)
9  */
10 
11 #include <boost/numeric/interval.hpp>
12 #include <boost/test/minimal.hpp>
13 #include "bugs.hpp"
14 
15 typedef boost::numeric::interval<double> I;
16 
BOOST_PREVENT_MACRO_SUBSTITUTION(double a,double b,double c,double d)17 static double min BOOST_PREVENT_MACRO_SUBSTITUTION (double a, double b, double c, double d) {
18   return (std::min)((std::min)(a, b), (std::min)(c, d));
19 }
20 
BOOST_PREVENT_MACRO_SUBSTITUTION(double a,double b,double c,double d)21 static double max BOOST_PREVENT_MACRO_SUBSTITUTION (double a, double b, double c, double d) {
22   return (std::max)((std::max)(a, b), (std::max)(c, d));
23 }
24 
test_mul(double al,double au,double bl,double bu)25 static bool test_mul(double al, double au, double bl, double bu) {
26   I a(al, au), b(bl, bu);
27   I c = a * b;
28   return c.lower() == (min)(al*bl, al*bu, au*bl, au*bu)
29       && c.upper() == (max)(al*bl, al*bu, au*bl, au*bu);
30 }
31 
test_mul1(double ac,double bl,double bu)32 static bool test_mul1(double ac, double bl, double bu) {
33   I a(ac), b(bl, bu);
34   I c = ac * b;
35   I d = b * ac;
36   I e = a * b;
37   return equal(c, d) && equal(d, e);
38 }
39 
test_div(double al,double au,double bl,double bu)40 static bool test_div(double al, double au, double bl, double bu) {
41   I a(al, au), b(bl, bu);
42   I c = a / b;
43   return c.lower() == (min)(al/bl, al/bu, au/bl, au/bu)
44       && c.upper() == (max)(al/bl, al/bu, au/bl, au/bu);
45 }
46 
test_div1(double al,double au,double bc)47 static bool test_div1(double al, double au, double bc) {
48   I a(al, au), b(bc);
49   I c = a / bc;
50   I d = a / b;
51   return equal(c, d);
52 }
53 
test_div2(double ac,double bl,double bu)54 static bool test_div2(double ac, double bl, double bu) {
55   I a(ac), b(bl, bu);
56   I c = ac / b;
57   I d = a / b;
58   return equal(c, d);
59 }
60 
test_square(double al,double au)61 static bool test_square(double al, double au) {
62   I a(al, au);
63   I b = square(a);
64   I c = a * a;
65   return b.upper() == c.upper() &&
66          (b.lower() == c.lower() || (c.lower() <= 0 && b.lower() == 0));
67 }
68 
test_sqrt(double al,double au)69 static bool test_sqrt(double al, double au) {
70   I a(al, au);
71   I b = square(sqrt(a));
72   return subset(abs(a), b);
73 }
74 
test_main(int,char * [])75 int test_main(int, char*[]) {
76   BOOST_CHECK(test_mul(2, 3, 5, 7));
77   BOOST_CHECK(test_mul(2, 3, -5, 7));
78   BOOST_CHECK(test_mul(2, 3, -7, -5));
79   BOOST_CHECK(test_mul(-2, 3, 5, 7));
80   BOOST_CHECK(test_mul(-2, 3, -5, 7));
81   BOOST_CHECK(test_mul(-2, 3, -7, -5));
82   BOOST_CHECK(test_mul(-3, -2, 5, 7));
83   BOOST_CHECK(test_mul(-3, -2, -5, 7));
84   BOOST_CHECK(test_mul(-3, -2, -7, -5));
85 
86   BOOST_CHECK(test_mul1(3, 5, 7));
87   BOOST_CHECK(test_mul1(3, -5, 7));
88   BOOST_CHECK(test_mul1(3, -7, -5));
89   BOOST_CHECK(test_mul1(-3, 5, 7));
90   BOOST_CHECK(test_mul1(-3, -5, 7));
91   BOOST_CHECK(test_mul1(-3, -7, -5));
92 
93   BOOST_CHECK(test_div(30, 42, 2, 3));
94   BOOST_CHECK(test_div(30, 42, -3, -2));
95   BOOST_CHECK(test_div(-30, 42, 2, 3));
96   BOOST_CHECK(test_div(-30, 42, -3, -2));
97   BOOST_CHECK(test_div(-42, -30, 2, 3));
98   BOOST_CHECK(test_div(-42, -30, -3, -2));
99 
100   BOOST_CHECK(test_div1(30, 42, 3));
101   BOOST_CHECK(test_div1(30, 42, -3));
102   BOOST_CHECK(test_div1(-30, 42, 3));
103   BOOST_CHECK(test_div1(-30, 42, -3));
104   BOOST_CHECK(test_div1(-42, -30, 3));
105   BOOST_CHECK(test_div1(-42, -30, -3));
106 
107   BOOST_CHECK(test_div2(30, 2, 3));
108   BOOST_CHECK(test_div2(30, -3, -2));
109   BOOST_CHECK(test_div2(-30, 2, 3));
110   BOOST_CHECK(test_div2(-30, -3, -2));
111 
112   BOOST_CHECK(test_square(2, 3));
113   BOOST_CHECK(test_square(-2, 3));
114   BOOST_CHECK(test_square(-3, 2));
115 
116   BOOST_CHECK(test_sqrt(2, 3));
117   BOOST_CHECK(test_sqrt(5, 7));
118   BOOST_CHECK(test_sqrt(-1, 2));
119 
120 # ifdef BOOST_BORLANDC
121   ::detail::ignore_warnings();
122 # endif
123   return 0;
124 }
125