/[eiffelstudio]/FreeELKS/trunk/library/kernel/real_ref.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/real_ref.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91424 - (show annotations)
Tue Oct 26 18:39:32 2004 UTC (15 years, 2 months ago) by manus_eiffel
File size: 7086 byte(s)
Initial revision

1 indexing
2
3 description:
4 "References to objects containing a real value"
5
6 status: "See notice at end of class"
7 date: "$Date$"
8 revision: "$Revision$"
9
10 class REAL_REF inherit
11
12 NUMERIC
13 redefine
14 out, is_equal
15 end
16
17 COMPARABLE
18 redefine
19 out, is_equal
20 end
21
22 HASHABLE
23 redefine
24 is_hashable, out, is_equal
25 end
26
27 feature -- Access
28
29 item: REAL
30 -- Numeric real value
31
32 hash_code: INTEGER is
33 -- Hash code value
34 do
35 Result := truncated_to_integer.hash_code
36 end
37
38 sign: INTEGER is
39 -- Sign value (0, -1 or 1)
40 do
41 if item > 0.0 then
42 Result := 1
43 elseif item < 0.0 then
44 Result := -1
45 end
46 ensure
47 three_way: Result = three_way_comparison (zero)
48 end
49
50 one: like Current is
51 -- Neutral element for "*" and "/"
52 do
53 create Result
54 Result.set_item (1.0)
55 end
56
57 zero: like Current is
58 -- Neutral element for "+" and "-"
59 do
60 create Result
61 Result.set_item (0.0)
62 end
63
64 feature -- Comparison
65
66 infix "<" (other: like Current): BOOLEAN is
67 -- Is `other' greater than current real?
68 do
69 Result := item < other.item
70 end
71
72 is_equal (other: like Current): BOOLEAN is
73 -- Is `other' attached to an object of the same type
74 -- as current object and identical to it?
75 do
76 Result := other.item = item
77 end
78
79 feature -- Element change
80
81 set_item (r: REAL) is
82 -- Make `r' the value of `item'.
83 do
84 item := r
85 end
86
87 feature -- Status report
88
89 divisible (other: REAL_REF): BOOLEAN is
90 -- May current object be divided by `other'?
91 do
92 Result := other.item /= 0.0
93 ensure then
94 ref_not_exact_zero: Result implies (other.item /= 0.0)
95 end
96
97 exponentiable (other: NUMERIC): BOOLEAN is
98 -- May current object be elevated to the power `other'?
99 local
100 integer_value: INTEGER_REF
101 double_value: DOUBLE_REF
102 real_value: REAL_REF
103 do
104 integer_value ?= other
105 real_value ?= other
106 double_value ?= other
107 if integer_value /= Void then
108 Result := integer_value.item >= 0 or item /= 0.0
109 elseif real_value /= Void then
110 Result := real_value.item >= 0.0 or item /= 0.0
111 elseif double_value /= Void then
112 Result := double_value.item >= 0.0 or item /= 0.0
113 end
114 ensure then
115 safe_values: ((other.conforms_to (0) and item /= 0.0) or
116 (other.conforms_to (0.0) and item > 0.0)) implies Result
117 end
118
119 is_hashable: BOOLEAN is
120 -- May current object be hashed?
121 -- (True if it is not its type's default.)
122 do
123 Result := item /= 0.0
124 end
125
126 feature {NONE} -- Initialization
127
128 make_from_reference (v: REAL_REF) is
129 -- Initialize `Current' with `v.item'.
130 require
131 v_not_void: v /= Void
132 do
133 item := v.item
134 ensure
135 item_set: item = v.item
136 end
137
138 feature -- Conversion
139
140 to_reference: REAL_REF is
141 -- Associated reference of Current
142 do
143 create Result
144 Result.set_item (item)
145 ensure
146 to_reference_not_void: Result /= Void
147 end
148
149 truncated_to_integer: INTEGER is
150 -- Integer part (same sign, largest absolute
151 -- value no greater than current object's)
152 do
153 Result := c_truncated_to_integer (item)
154 end
155
156 truncated_to_integer_64: INTEGER_64 is
157 -- Integer part (same sign, largest absolute
158 -- value no greater than current object's)
159 do
160 Result := c_truncated_to_integer_64 (item)
161 end
162
163 to_double: DOUBLE is
164 -- Current seen as a double
165 do
166 Result := item.to_double
167 end
168
169 ceiling: INTEGER is
170 -- Smallest integral value no smaller than current object
171 do
172 Result := c_ceiling (item).truncated_to_integer
173 ensure
174 result_no_smaller: Result >= item
175 close_enough: Result - item < item.one
176 end
177
178 floor: INTEGER is
179 -- Greatest integral value no greater than current object
180 do
181 Result := c_floor (item).truncated_to_integer
182 ensure
183 result_no_greater: Result <= item
184 close_enough: item - Result < Result.one
185 end
186
187 rounded: INTEGER is
188 -- Rounded integral value
189 do
190 Result := sign * ((abs + 0.5).floor)
191 ensure
192 definition: Result = sign * ((abs + 0.5).floor)
193 end
194
195 feature -- Basic operations
196
197 abs: REAL is
198 -- Absolute value
199 do
200 Result := abs_ref.item
201 ensure
202 non_negative: Result >= 0.0
203 same_absolute_value: (Result = item) or (Result = -item)
204 end
205
206 infix "+" (other: like Current): like Current is
207 -- Sum with `other'
208 do
209 create Result
210 Result.set_item (item + other.item)
211 end
212
213 infix "-" (other: like Current): like Current is
214 -- Result of subtracting `other'
215 do
216 create Result
217 Result.set_item (item - other.item)
218 end
219
220 infix "*" (other: like Current): like Current is
221 -- Product by `other'
222 do
223 create Result
224 Result.set_item (item * other.item)
225 end
226
227 infix "/" (other: like Current): like Current is
228 -- Division by `other'
229 do
230 create Result
231 Result.set_item (item / other.item)
232 end
233
234 infix "^" (other: DOUBLE): DOUBLE is
235 -- Current real to the power `other'
236 do
237 Result := item ^ other
238 end
239
240 prefix "+": like Current is
241 -- Unary plus
242 do
243 create Result
244 Result.set_item (+ item)
245 end
246
247 prefix "-": like Current is
248 -- Unary minus
249 do
250 create Result
251 Result.set_item (- item)
252 end
253
254 feature -- Output
255
256 out: STRING is
257 -- Printable representation of real value
258 do
259 Result := c_outr (item)
260 end
261
262 feature {NONE} -- Implementation
263
264 abs_ref: REAL_REF is
265 -- Absolute value
266 do
267 if item >= 0.0 then
268 Result := Current
269 else
270 Result := -Current
271 end
272 ensure
273 result_exists: Result /= Void
274 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
275 end
276
277 c_outr (r: REAL): STRING is
278 -- Printable representation of real value
279 external
280 "built_in"
281 end
282
283 c_truncated_to_integer (r: REAL): INTEGER is
284 -- Integer part of `r' (same sign, largest absolute
285 -- value no greater than `r''s)
286 external
287 "built_in"
288 end
289
290 c_truncated_to_integer_64 (r: REAL): INTEGER_64 is
291 -- Integer part of `r' (same sign, largest absolute
292 -- value no greater than `r''s)
293 external
294 "built_in"
295 end
296
297 c_ceiling (r: REAL): REAL is
298 -- Smallest integral value no smaller than `r'
299 external
300 "built_in"
301 end
302
303 c_floor (r: REAL): REAL is
304 -- Greatest integral value no greater than `r'
305 external
306 "built_in"
307 end
308
309 invariant
310
311 sign_times_abs: sign * abs = item
312
313 indexing
314
315 library: "[
316 EiffelBase: Library of reusable components for Eiffel.
317 ]"
318
319 status: "[
320 Copyright 1986-2001 Interactive Software Engineering (ISE).
321 For ISE customers the original versions are an ISE product
322 covered by the ISE Eiffel license and support agreements.
323 ]"
324
325 license: "[
326 EiffelBase may now be used by anyone as FREE SOFTWARE to
327 develop any product, public-domain or commercial, without
328 payment to ISE, under the terms of the ISE Free Eiffel Library
329 License (IFELL) at http://eiffel.com/products/base/license.html.
330 ]"
331
332 source: "[
333 Interactive Software Engineering Inc.
334 ISE Building
335 360 Storke Road, Goleta, CA 93117 USA
336 Telephone 805-685-1006, Fax 805-685-6869
337 Electronic mail <info@eiffel.com>
338 Customer support http://support.eiffel.com
339 ]"
340
341 info: "[
342 For latest info see award-winning pages: http://eiffel.com
343 ]"
344
345 end -- class REAL_REF
346
347
348

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23