4.3 test-interval.cc を読む

test-interval.cc に目を通しておくと、 simplified kv がどういう機能を持っているのか、 どうやればそれが使えるのかが分かる。



   1 #include "interval.hpp"
   2 
   3 int main()
   4 {
   5         interval x;
   6         interval y = 1.;
   7         interval z(1.);
   8 
   9         x = 1.;
  10         y = 10.;
  11         z = x / y;
  12 
  13         std::cout << z << "\n";
  14         std::cout.precision(17);
  15         std::cout << z << "\n";
  16 
  17         // copy
  18         x = interval(1., 2.);
  19         y = interval(3., 4.);
  20         // assign
  21         y.assign(3., 4.);
  22 
  23         // basic four operations
  24         std::cout << x + y << "\n";
  25         std::cout << x - y << "\n";
  26         std::cout << x * y << "\n";
  27         std::cout << x / y << "\n";
  28 
  29         // operation with constant
  30         std::cout << x + 1 << "\n";
  31         std::cout << x + 1. << "\n";
  32 
  33         // compound assignment operator
  34         z += x;
  35         z += 1.;
  36 
  37         z = interval(3., 4.);
  38         // access to endpoints
  39         std::cout << z.lower() << "\n";
  40         std::cout << z.upper() << "\n";
  41         z.lower() = 3.5;
  42         std::cout << z << "\n";
  43 
  44         // static functions
  45         // whole and hull
  46         std::cout << interval::whole() << "\n";
  47         std::cout << interval::hull(1., 2.) << "\n";
  48         std::cout << interval::hull(2., 1.) << "\n";
  49         std::cout << interval::hull(1., z) << "\n";
  50         std::cout << interval::hull(z, 1.) << "\n";
  51         std::cout << interval::hull(x, y) << "\n";
  52 
  53         // friend functions
  54         std::cout << width(z) << "\n";
  55         std::cout << rad(z) << "\n";
  56         std::cout << median(z) << "\n";
  57         std::cout << mid(z) << "\n";
  58         std::cout << norm(z) << "\n";
  59         std::cout << mag(z) << "\n";
  60         std::cout << std::boolalpha;
  61         std::cout << in(3.9, z) << "\n";
  62         std::cout << in(4.1, z) << "\n";
  63         std::cout << subset(x, y) << "\n";
  64         std::cout << subset(y, z) << "\n";
  65         std::cout << proper_subset(y, z) << "\n";
  66         std::cout << overlap(x, y) << "\n";
  67         std::cout << overlap(y, z) << "\n";
  68         std::cout << intersect(y, z) << "\n";
  69         std::cout << abs(interval(2., 3.)) << "\n";
  70         std::cout << abs(interval(-2., 3.)) << "\n";
  71         std::cout << abs(interval(-2., -1.)) << "\n";
  72         std::cout << mig(interval(-2., 1.)) << "\n";
  73         std::cout << mig(interval(-2., -1.)) << "\n";
  74         std::cout << mig(interval(2., 3.)) << "\n";
  75 
  76         std::cout << max(interval(2., 4.), interval(3., 5.)) << "\n";
  77         std::cout << min(interval(2., 4.), interval(3., 5.)) << "\n";
  78 
  79         // test for midrad
  80         std::cout << "test for midrad\n";
  81         double m, r;
  82         z = interval(1., 1 + std::numeric_limits<double>::epsilon() * 3);
  83         std::cout << mid(z) << "\n";
  84         std::cout << rad(z) << "\n";
  85         midrad(z, m, r);
  86         std::cout << m << "\n";
  87         std::cout << r << "\n";
  88 
  89         // comparison operators
  90         std::cout << (x < y) << "\n";
  91         std::cout << (x < 1.) << "\n";
  92         std::cout << (1. < x) << "\n";
  93         std::cout << (interval(1.) == 1.) << "\n";
  94         std::cout << (interval(1., 2.) != 3.) << "\n";
  95 
  96         // division_part1, division_part2
  97         // calculate X / (Y \ 0). the result may be divided into two part.
  98         bool parted;
  99         std::cout << division_part1(interval(1., 2.), interval(-3., 4.), parted) << "\n";
 100         // if the result has division_part2, parted is set to true.
 101         if (parted) std::cout << division_part2(interval(1., 2.), interval(-3., 4.)) << "\n";
 102 
 103         // integer power
 104         std::cout << pow(interval(2., 3.), 2) << "\n";
 105         std::cout << pow(interval(2., 3.), -2) << "\n";
 106         std::cout << pow(interval(-2., 3.), 2) << "\n";
 107         std::cout << pow(interval(-2., 3.), 3) << "\n";
 108         // general power
 109         std::cout << pow(interval(2., 3.), interval(2., 3)) << "\n";
 110         // mathematical functions
 111         std::cout << sqrt(interval(2.5, 3.5)) << "\n";
 112         std::cout << exp(interval(2.5, 3.5)) << "\n";
 113         std::cout << exp(interval(-std::numeric_limits<double>::infinity(), 0.)) << "\n";
 114         std::cout << exp(interval(0., std::numeric_limits<double>::infinity())) << "\n";
 115         std::cout << expm1(interval(-0.25, 0.25)) << "\n";
 116         std::cout << log(interval(0.75, 1.25)) << "\n";
 117         std::cout << log(interval(1., std::numeric_limits<double>::infinity())) << "\n";
 118         std::cout << log(interval(0., 1.)) << "\n";
 119         std::cout << log1p(interval(-0.25, 0.25)) << "\n";
 120         std::cout << sin(interval(-0.25, 0.25)) << "\n";
 121         std::cout << cos(interval(-0.25, 0.25)) << "\n";
 122         std::cout << tan(interval(-0.25, 0.25)) << "\n";
 123         std::cout << atan(interval(-0.25, 0.25)) << "\n";
 124         std::cout << asin(interval(-0.25, 0.25)) << "\n";
 125         std::cout << acos(interval(-0.25, 0.25)) << "\n";
 126         std::cout << atan2(interval(1.), interval(1.)) << "\n";
 127         std::cout << sinh(interval(-0.25, 0.25)) << "\n";
 128         std::cout << cosh(interval(-0.25, 0.25)) << "\n";
 129         std::cout << tanh(interval(-0.25, 0.25)) << "\n";
 130         std::cout << asinh(interval(-0.25, 0.25)) << "\n";
 131         std::cout << acosh(interval(1.5, 2.)) << "\n";
 132         std::cout << atanh(interval(-0.25, 0.25)) << "\n";
 133 
 134         // string is converted to interval which includes the number represented
 135         // by the string
 136 
 137         // initialize by string. 
 138         x = "0.1";
 139         std::cout << x << "\n";
 140         // operation with string
 141         std::cout << "0.1" * x << "\n";
 142         x += "0.1";
 143         // comparison with string
 144         std::cout << ("0.20001" > x) << "\n";
 145 
 146         // numeric constants
 147         std::cout << interval::pi() << "\n";
 148         std::cout << interval::e() << "\n";
 149         std::cout << interval::ln2() << "\n";
 150 }

以下解読してみる (かなりの急ぎ仕事なので、間違えている可能性がある)。

数学で区間 $ [a,b]$ と言うと、 $ a,b\in\mathbb{R}$, $ a<b$ であることが仮定されているが、 区間演算では、$ a=b$ の場合も含める。 特にコンピューター上の区間演算では、 $ a$, $ b$ は浮動小数点数とする、いわゆる機械区間が使われる。 浮動小数点数の全体 $ \mathbb{F}$ には、 普通は、実数のうちで有限桁の2進数で表現できるものしか含まれない。 例えば $ 0.1\not\in\mathbb{F}$. ( $ 0.1=\frac{1}{10}=\frac{1}{2\cdot 5}$ が規約分数表示で、 分母に $ 5$ があるせいで、2進数で表すには無限桁が必要になる。 10進法なら分母に$ 5$があっても良いんだけど。) 一方で、 $ \mathbb{F}$$ +\infty$$ -\infty$ が含まれる (これは柏木先生の趣味というべきか?)。

桂田 祐史
2020-09-03