Mercurial > octave
comparison liboctave/Range.cc @ 3:9a4c07481e61
[project @ 1993-08-08 01:20:23 by jwe]
Initial revision
author | jwe |
---|---|
date | Sun, 08 Aug 1993 01:21:46 +0000 |
parents | |
children | 380cb9ccc889 |
comparison
equal
deleted
inserted
replaced
2:c0190df9885d | 3:9a4c07481e61 |
---|---|
1 // Range.cc -*- C++ -*- | |
2 /* | |
3 | |
4 Copyright (C) 1992, 1993 John W. Eaton | |
5 | |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with Octave; see the file COPYING. If not, write to the Free | |
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 */ | |
23 | |
24 #ifdef __GNUG__ | |
25 #pragma implementation | |
26 #endif | |
27 | |
28 #include <limits.h> | |
29 | |
30 #include "Range.h" | |
31 | |
32 void | |
33 Range::print_range (void) | |
34 { | |
35 cerr << "Range: _base = " << _base | |
36 << " _limit " << _limit | |
37 << " _inc " << _inc | |
38 << " _nelem " << _nelem << "\n"; | |
39 } | |
40 | |
41 ostream& | |
42 operator << (ostream& os, const Range& a) | |
43 { | |
44 double b = a.base (); | |
45 double increment = a.inc (); | |
46 int num_elem = a.nelem (); | |
47 | |
48 for (int i = 0; i < num_elem; i++) | |
49 os << b + i * increment << " "; | |
50 | |
51 os << "\n"; | |
52 | |
53 return os; | |
54 } | |
55 | |
56 istream& | |
57 operator >> (istream& is, Range& a) | |
58 { | |
59 is >> a._base; | |
60 if (is) | |
61 { | |
62 is >> a._limit; | |
63 if (is) | |
64 { | |
65 is >> a._inc; | |
66 a._nelem = a.nelem_internal (); | |
67 } | |
68 } | |
69 | |
70 return is; | |
71 } | |
72 | |
73 int | |
74 Range::nelem_internal (void) const | |
75 { | |
76 // Find an approximate number of elements, then do the best we can to | |
77 // find the number of elements that we would get if we had done | |
78 // something like | |
79 // | |
80 // nelem = 0; | |
81 // while (base + nelem * inc <= limit) | |
82 // nelem++; | |
83 // | |
84 // (for limit > base && inc > 0) | |
85 | |
86 double ntry = (_limit - _base) / _inc; | |
87 double max_val = (double) INT_MAX; | |
88 | |
89 if (ntry > max_val) | |
90 return -1; | |
91 | |
92 if (_limit > _base && _inc > 0) | |
93 { | |
94 // Our approximation may have been too big. | |
95 | |
96 while (_base + ntry * _inc > _limit && ntry > 0) | |
97 ntry = ntry - 1; | |
98 | |
99 // Now that we are close, get the actual number. | |
100 | |
101 while (_base + ntry * _inc <= _limit && ntry <= max_val) | |
102 ntry = ntry + 1; | |
103 } | |
104 else if (_limit < _base && _inc < 0) | |
105 { | |
106 // Our approximation may have been too big. | |
107 | |
108 while (_base + ntry * _inc < _limit && ntry > 0) | |
109 ntry = ntry - 1; | |
110 | |
111 // Now that we are close, get the actual number. | |
112 | |
113 while (_base + ntry * _inc >= _limit && ntry <= max_val) | |
114 ntry = ntry + 1; | |
115 } | |
116 else if (_limit == _base) | |
117 ntry = 1; | |
118 else | |
119 ntry = 0; | |
120 | |
121 if (ntry > max_val) | |
122 return -1; | |
123 else | |
124 return (int) ntry; | |
125 } | |
126 | |
127 /* | |
128 ;;; Local Variables: *** | |
129 ;;; mode: C++ *** | |
130 ;;; page-delimiter: "^/\\*" *** | |
131 ;;; End: *** | |
132 */ |