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

1 indexing
2 description: "References to objects containing a real value"
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 REAL_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: REAL is
29 -- Numeric real 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 real?
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 (r: REAL) is
84 -- Make `r' the value of `item'.
85 external
86 "built_in"
87 end
88
89 feature -- Status report
90
91 divisible (other: REAL_REF): BOOLEAN is
92 -- May current object be divided by `other'?
93 do
94 Result := other.item /= 0.0
95 ensure then
96 ref_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} -- Initialization
129
130 make_from_reference (v: REAL_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: REAL_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 to_double: DOUBLE is
166 -- Current seen as a double
167 do
168 Result := item.to_double
169 end
170
171 ceiling: INTEGER is
172 -- Smallest integral value no smaller than current object
173 do
174 Result := ceiling_real_32.truncated_to_integer
175 ensure
176 result_no_smaller: Result >= item
177 close_enough: Result - item < item.one
178 end
179
180 floor: INTEGER is
181 -- Greatest integral value no greater than current object
182 do
183 Result := floor_real_32.truncated_to_integer
184 ensure
185 result_no_greater: Result <= item
186 close_enough: item - Result < Result.one
187 end
188
189 rounded: INTEGER is
190 -- Rounded integral value
191 do
192 Result := sign * ((abs + 0.5).floor)
193 ensure
194 definition: Result = sign * ((abs + 0.5).floor)
195 end
196
197 ceiling_real_32: REAL is
198 -- Smallest integral value no smaller than current object
199 do
200 Result := item.ceiling_real_32
201 ensure
202 result_no_smaller: Result >= item
203 close_enough: Result - item < item.one
204 end
205
206 floor_real_32: REAL is
207 -- Greatest integral value no greater than current object
208 do
209 Result := item.floor_real_32
210 ensure
211 result_no_greater: Result <= item
212 close_enough: item - Result < Result.one
213 end
214
215 rounded_real_32: REAL is
216 -- Rounded integral value
217 do
218 Result := sign * ((abs + 0.5).floor_real_32)
219 ensure
220 definition: Result = sign * ((abs + 0.5).floor_real_32)
221 end
222
223 feature -- Basic operations
224
225 abs: REAL is
226 -- Absolute value
227 do
228 Result := abs_ref.item
229 ensure
230 non_negative: Result >= 0.0
231 same_absolute_value: (Result = item) or (Result = -item)
232 end
233
234 infix "+" (other: like Current): like Current is
235 -- Sum with `other'
236 do
237 create Result
238 Result.set_item (item + other.item)
239 end
240
241 infix "-" (other: like Current): like Current is
242 -- Result of subtracting `other'
243 do
244 create Result
245 Result.set_item (item - other.item)
246 end
247
248 infix "*" (other: like Current): like Current is
249 -- Product by `other'
250 do
251 create Result
252 Result.set_item (item * other.item)
253 end
254
255 infix "/" (other: like Current): like Current is
256 -- Division by `other'
257 do
258 create Result
259 Result.set_item (item / other.item)
260 end
261
262 infix "^" (other: DOUBLE): DOUBLE is
263 -- Current real to the power `other'
264 do
265 Result := item ^ other
266 end
267
268 prefix "+": like Current is
269 -- Unary plus
270 do
271 create Result
272 Result.set_item (+ item)
273 end
274
275 prefix "-": like Current is
276 -- Unary minus
277 do
278 create Result
279 Result.set_item (- item)
280 end
281
282 feature -- Output
283
284 out: STRING is
285 -- Printable representation of real value
286 do
287 Result := item.out
288 end
289
290 feature {NONE} -- Implementation
291
292 abs_ref: like Current is
293 -- Absolute value
294 do
295 if item >= 0.0 then
296 Result := Current
297 else
298 Result := -Current
299 end
300 ensure
301 result_exists: Result /= Void
302 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
303 end
304
305 invariant
306 sign_times_abs: sign * abs = item
307
308 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23