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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 indexing
2
3 description:
4 "References to objects containing a double-precision real number"
5
6 status: "See notice at end of class"
7 date: "$Date$"
8 revision: "$Revision$"
9
10 class DOUBLE_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: DOUBLE
30 -- Numeric double 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 double?
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 (d: DOUBLE) is
82 -- Make `d' the `item' value.
83 do
84 item := d
85 end
86
87 feature -- Status report
88
89 divisible (other: DOUBLE_REF): BOOLEAN is
90 -- May current object be divided by `other'?
91 do
92 Result := other.item /= 0.0
93 ensure then
94 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} -- Conversion
127
128 make_from_reference (v: DOUBLE_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: DOUBLE_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 truncated_to_real: REAL is
164 -- Real part (Same sign, largest absolute
165 -- value no greater than current object's)
166 do
167 Result := c_truncated_to_real (item)
168 end
169
170 ceiling: INTEGER is
171 -- Smallest integral value no smaller than current object
172 do
173 Result := c_ceiling (item).truncated_to_integer
174 ensure
175 result_no_smaller: Result >= item
176 close_enough: Result - item < item.one
177 end
178
179 floor: INTEGER is
180 -- Greatest integral value no greater than current object
181 do
182 Result := c_floor (item).truncated_to_integer
183 ensure
184 result_no_greater: Result <= item
185 close_enough: item - Result < Result.one
186 end
187
188 rounded: INTEGER is
189 -- Rounded integral value
190 do
191 Result := sign * (c_floor (abs_ref.item + 0.5).truncated_to_integer)
192 ensure
193 definition: Result = sign * ((abs + 0.5).floor)
194 end
195
196 feature -- Basic operations
197
198 abs: DOUBLE is
199 -- Absolute value
200 do
201 Result := abs_ref.item
202 ensure
203 non_negative: Result >= 0.0
204 same_absolute_value: (Result = item) or (Result = -item)
205 end
206
207 infix "+" (other: like Current): like Current is
208 -- Sum with `other'
209 do
210 create Result
211 Result.set_item (item + other.item)
212 end
213
214 infix "-" (other: like Current): like Current is
215 -- Result of subtracting `other'
216 do
217 create Result
218 Result.set_item (item - other.item)
219 end
220
221 infix "*" (other: like Current): like Current is
222 -- Product with `other'
223 do
224 create Result
225 Result.set_item (item * other.item)
226 end
227
228 infix "/" (other: like Current): like Current is
229 -- Division by `other'
230 do
231 create Result
232 Result.set_item (item / other.item)
233 end
234
235 infix "^" (other: DOUBLE): DOUBLE is
236 -- Current double to the power `other'
237 do
238 Result := item ^ other
239 end
240
241 prefix "+": like Current is
242 -- Unary plus
243 do
244 create Result
245 Result.set_item (+ item)
246 end
247
248 prefix "-": like Current is
249 -- Unary minus
250 do
251 create Result
252 Result.set_item (- item)
253 end
254
255 feature -- Output
256
257 out: STRING is
258 -- Printable representation of double value
259 do
260 Result := c_outd (item)
261 end
262
263 feature {NONE} -- Implementation
264
265 abs_ref: DOUBLE_REF is
266 -- Absolute value
267 do
268 if item >= 0.0 then
269 Result := Current
270 else
271 Result := -Current
272 end
273 ensure
274 result_exists: Result /= Void
275 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
276 end
277
278 c_outd (d: DOUBLE): STRING is
279 -- Printable representation of double value
280 external
281 "built_in"
282 end
283
284 c_truncated_to_integer (d: DOUBLE): INTEGER is
285 -- Integer part of `d' (same sign, largest absolute
286 -- value no greater than `d''s)
287 external
288 "built_in"
289 end
290
291 c_truncated_to_integer_64 (d: DOUBLE): INTEGER_64 is
292 -- Integer part of `d' (same sign, largest absolute
293 -- value no greater than `d''s)
294 external
295 "built_in"
296 end
297
298 c_truncated_to_real (d: DOUBLE): REAL is
299 -- Real part of `d' (same sign, largest absolute
300 -- value no greater than `d''s)
301 external
302 "built_in"
303 end
304
305 c_ceiling (d: DOUBLE): DOUBLE is
306 -- Smallest integral value no smaller than `d'
307 external
308 "built_in"
309 end
310
311 c_floor (d: DOUBLE): DOUBLE is
312 -- Greatest integral value no greater than `d'
313 external
314 "built_in"
315 end
316
317 invariant
318
319 sign_times_abs: sign * abs = item
320
321 indexing
322
323 library: "[
324 EiffelBase: Library of reusable components for Eiffel.
325 ]"
326
327 status: "[
328 Copyright 1986-2001 Interactive Software Engineering (ISE).
329 For ISE customers the original versions are an ISE product
330 covered by the ISE Eiffel license and support agreements.
331 ]"
332
333 license: "[
334 EiffelBase may now be used by anyone as FREE SOFTWARE to
335 develop any product, public-domain or commercial, without
336 payment to ISE, under the terms of the ISE Free Eiffel Library
337 License (IFELL) at http://eiffel.com/products/base/license.html.
338 ]"
339
340 source: "[
341 Interactive Software Engineering Inc.
342 ISE Building
343 360 Storke Road, Goleta, CA 93117 USA
344 Telephone 805-685-1006, Fax 805-685-6869
345 Electronic mail <info@eiffel.com>
346 Customer support http://support.eiffel.com
347 ]"
348
349 info: "[
350 For latest info see award-winning pages: http://eiffel.com
351 ]"
352
353 end -- class DOUBLE_REF
354
355
356

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23