/[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 91458 - (show annotations)
Thu Feb 9 11:23:11 2006 UTC (13 years, 11 months ago) by ericb
File size: 6393 byte(s)
Replaced external "C" features by built-in features.

1 indexing
2 description: "References to objects containing a double-precision real number"
3 library: "Free implementation of ELKS library"
4 copyright: "Copyright (c) 1986-2006, Eiffel Software and others"
5 license: "Eiffel Forum License v2 (see forum.txt)"
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class DOUBLE_REF inherit
10
11 NUMERIC
12 redefine
13 out, is_equal
14 end
15
16 COMPARABLE
17 redefine
18 out, is_equal
19 end
20
21 HASHABLE
22 redefine
23 is_hashable, out, is_equal
24 end
25
26 feature -- Access
27
28 item: DOUBLE is
29 -- Numeric double value
30 external
31 "built_in"
32 end
33
34 hash_code: INTEGER is
35 -- Hash code value
36 do
37 Result := truncated_to_integer.hash_code
38 end
39
40 sign: INTEGER is
41 -- Sign value (0, -1 or 1)
42 do
43 if item > 0.0 then
44 Result := 1
45 elseif item < 0.0 then
46 Result := -1
47 end
48 ensure
49 three_way: Result = three_way_comparison (zero)
50 end
51
52 one: like Current is
53 -- Neutral element for "*" and "/"
54 do
55 create Result
56 Result.set_item (1.0)
57 end
58
59 zero: like Current is
60 -- Neutral element for "+" and "-"
61 do
62 create Result
63 Result.set_item (0.0)
64 end
65
66 feature -- Comparison
67
68 infix "<" (other: like Current): BOOLEAN is
69 -- Is `other' greater than current double?
70 do
71 Result := item < other.item
72 end
73
74 is_equal (other: like Current): BOOLEAN is
75 -- Is `other' attached to an object of the same type
76 -- as current object and identical to it?
77 do
78 Result := other.item = item
79 end
80
81 feature -- Element change
82
83 set_item (d: DOUBLE) is
84 -- Make `d' the `item' value.
85 external
86 "built_in"
87 end
88
89 feature -- Status report
90
91 divisible (other: DOUBLE_REF): BOOLEAN is
92 -- May current object be divided by `other'?
93 do
94 Result := other.item /= 0.0
95 ensure then
96 not_exact_zero: Result implies (other.item /= 0.0)
97 end
98
99 exponentiable (other: NUMERIC): BOOLEAN is
100 -- May current object be elevated to the power `other'?
101 local
102 integer_value: INTEGER_REF
103 double_value: DOUBLE_REF
104 real_value: REAL_REF
105 do
106 integer_value ?= other
107 real_value ?= other
108 double_value ?= other
109 if integer_value /= Void then
110 Result := integer_value.item >= 0 or item /= 0.0
111 elseif real_value /= Void then
112 Result := real_value.item >= 0.0 or item /= 0.0
113 elseif double_value /= Void then
114 Result := double_value.item >= 0.0 or item /= 0.0
115 end
116 ensure then
117 safe_values: ((other.conforms_to (0) and item /= 0.0) or
118 (other.conforms_to (0.0) and item > 0.0)) implies Result
119 end
120
121 is_hashable: BOOLEAN is
122 -- May current object be hashed?
123 -- (True if it is not its type's default.)
124 do
125 Result := item /= 0.0
126 end
127
128 feature {NONE} -- Conversion
129
130 make_from_reference (v: DOUBLE_REF) is
131 -- Initialize `Current' with `v.item'.
132 require
133 v_not_void: v /= Void
134 do
135 set_item (v.item)
136 ensure
137 item_set: item = v.item
138 end
139
140 feature -- Conversion
141
142 to_reference: DOUBLE_REF is
143 -- Associated reference of Current
144 do
145 create Result
146 Result.set_item (item)
147 ensure
148 to_reference_not_void: Result /= Void
149 end
150
151 truncated_to_integer: INTEGER is
152 -- Integer part (Same sign, largest absolute
153 -- value no greater than current object's)
154 do
155 Result := item.truncated_to_integer
156 end
157
158 truncated_to_integer_64: INTEGER_64 is
159 -- Integer part (Same sign, largest absolute
160 -- value no greater than current object's)
161 do
162 Result := item.truncated_to_integer_64
163 end
164
165 truncated_to_real: REAL is
166 -- Real part (Same sign, largest absolute
167 -- value no greater than current object's)
168 do
169 Result := item.truncated_to_real
170 end
171
172 ceiling: INTEGER is
173 -- Smallest integral value no smaller than current object
174 do
175 Result := ceiling_real_64.truncated_to_integer
176 ensure
177 result_no_smaller: Result >= item
178 close_enough: Result - item < item.one
179 end
180
181 floor: INTEGER is
182 -- Greatest integral value no greater than current object
183 do
184 Result := floor_real_64.truncated_to_integer
185 ensure
186 result_no_greater: Result <= item
187 close_enough: item - Result < Result.one
188 end
189
190 rounded: INTEGER is
191 -- Rounded integral value
192 do
193 Result := sign * ((abs + 0.5).floor)
194 ensure
195 definition: Result = sign * ((abs + 0.5).floor)
196 end
197
198 ceiling_real_64: DOUBLE is
199 -- Smallest integral value no smaller than current object
200 do
201 Result := item.ceiling_real_64
202 ensure
203 result_no_smaller: Result >= item
204 close_enough: Result - item < item.one
205 end
206
207 floor_real_64: DOUBLE is
208 -- Greatest integral value no greater than current object
209 do
210 Result := item.floor_real_64
211 ensure
212 result_no_greater: Result <= item
213 close_enough: item - Result < Result.one
214 end
215
216 rounded_real_64: DOUBLE is
217 -- Rounded integral value
218 do
219 Result := sign * ((abs + 0.5).floor_real_64)
220 ensure
221 definition: Result = sign * ((abs + 0.5).floor_real_64)
222 end
223
224 feature -- Basic operations
225
226 abs: DOUBLE is
227 -- Absolute value
228 do
229 Result := abs_ref.item
230 ensure
231 non_negative: Result >= 0.0
232 same_absolute_value: (Result = item) or (Result = -item)
233 end
234
235 infix "+" (other: like Current): like Current is
236 -- Sum with `other'
237 do
238 create Result
239 Result.set_item (item + other.item)
240 end
241
242 infix "-" (other: like Current): like Current is
243 -- Result of subtracting `other'
244 do
245 create Result
246 Result.set_item (item - other.item)
247 end
248
249 infix "*" (other: like Current): like Current is
250 -- Product with `other'
251 do
252 create Result
253 Result.set_item (item * other.item)
254 end
255
256 infix "/" (other: like Current): like Current is
257 -- Division by `other'
258 do
259 create Result
260 Result.set_item (item / other.item)
261 end
262
263 infix "^" (other: DOUBLE): DOUBLE is
264 -- Current double to the power `other'
265 do
266 Result := item ^ other
267 end
268
269 prefix "+": like Current is
270 -- Unary plus
271 do
272 create Result
273 Result.set_item (+ item)
274 end
275
276 prefix "-": like Current is
277 -- Unary minus
278 do
279 create Result
280 Result.set_item (- item)
281 end
282
283 feature -- Output
284
285 out: STRING is
286 -- Printable representation of double value
287 do
288 Result := item.out
289 end
290
291 feature {NONE} -- Implementation
292
293 abs_ref: like Current is
294 -- Absolute value
295 do
296 if item >= 0.0 then
297 Result := Current
298 else
299 Result := -Current
300 end
301 ensure
302 result_exists: Result /= Void
303 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
304 end
305
306 invariant
307 sign_times_abs: sign * abs = item
308
309 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23