PageRenderTime 19ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/numeric/real_general.e

http://github.com/tybor/Liberty
Specman e | 313 lines | 229 code | 55 blank | 29 comment | 4 complexity | 2676897b768940e08015fd23dd1a1b96 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4deferred class REAL_GENERAL
  5   --
  6   -- Common anchestor of floating-point arithmetic classes, also known as REAL numbers: REAL_32, REAL_64, REAL_128, REAL_EXTENDED.
  7   --
  8
  9inherit
 10   FLOAT
 11      redefine out_in_tagged_out_memory, fill_tagged_out_memory, is_equal, infix "<=", infix ">", infix ">="
 12      end
 13
 14insert
 15   REAL_PRECISION
 16      redefine out_in_tagged_out_memory, fill_tagged_out_memory, is_equal, precision
 17      end
 18
 19feature {ANY}
 20   is_equal (other: like Current): BOOLEAN
 21      do
 22         Result := Current = other
 23      end
 24
 25   prefix "+": like Current
 26      do
 27         Result := Current
 28      end
 29
 30   prefix "-": like Current
 31      external "built_in"
 32      end
 33
 34   infix "+" (other: like Current): like Current
 35      external "built_in"
 36      end
 37
 38   infix "-" (other: like Current): like Current
 39      external "built_in"
 40      end
 41
 42   infix "*" (other: like Current): like Current
 43      external "built_in"
 44      end
 45
 46   infix "/" (other: like Current): like Current
 47      external "built_in"
 48      end
 49
 50   infix "^" (e: INTEGER): like Current
 51      external "built_in"
 52      end
 53
 54   infix "<" (other: like Current): BOOLEAN
 55      external "built_in"
 56      end
 57
 58   infix "<=" (other: like Current): BOOLEAN
 59      external "built_in"
 60      end
 61
 62   infix ">" (other: like Current): BOOLEAN
 63      external "built_in"
 64      end
 65
 66   infix ">=" (other: like Current): BOOLEAN
 67      external "built_in"
 68      end
 69
 70   abs: like Current
 71      do
 72         if Current < {REAL_32 0.0} then
 73            Result := -Current
 74         else
 75            Result := Current
 76         end
 77      end
 78
 79   is_not_a_number: BOOLEAN
 80      external "built_in"
 81      end
 82
 83   is_infinity: BOOLEAN
 84      external "built_in"
 85      end
 86
 87   is_zero: BOOLEAN
 88      do
 89         Result := Current = 0.0 or else Current = -0.0
 90      end
 91
 92   infix "~=" (other: like Current): BOOLEAN
 93      deferred
 94      end
 95
 96   precision: INTEGER_8
 97      do
 98         Result := Precursor
 99         if Result > mantissa_bits then
100            Result := mantissa_bits
101         end
102      ensure then
103         Result <= mantissa_bits
104      end
105
106   is_subnormal: BOOLEAN
107      external "built_in"
108      end
109
110   is_normal: BOOLEAN
111      external "built_in"
112      end
113
114   divisible (other: like Current): BOOLEAN
115      do
116         Result := other /= 0.0
117      end
118
119feature {ANY} -- Conversions:
120   frozen rounded: like Current
121      external "built_in"
122      end
123
124   frozen floor: like Current
125      external "built_in"
126      end
127
128   frozen ceiling: like Current
129         -- Smallest integral value no smaller than Current.
130      external "built_in"
131      end
132
133feature {ANY} -- Object Printing:
134   to_string: STRING
135      do
136         sprintf(sprintf_buffer, 'f', 6, Current)
137         create Result.from_external_copy(sprintf_buffer.to_pointer)
138      end
139
140   to_string_format (f: INTEGER): STRING
141      do
142         sprintf(sprintf_buffer, 'f', f, Current)
143         create Result.from_external_copy(sprintf_buffer.to_pointer)
144      end
145
146   to_string_scientific (f: INTEGER): STRING
147      do
148         sprintf(sprintf_buffer, 'e', f, Current)
149         create Result.from_external_copy(sprintf_buffer.to_pointer)
150      end
151
152   append_in (buffer: STRING)
153      do
154         append_in_format(buffer, 6)
155      end
156
157   append_in_format (str: STRING; f: INTEGER)
158      local
159         i: INTEGER
160      do
161         from
162            sprintf(sprintf_buffer, 'f', f, Current)
163            i := 0
164         until
165            sprintf_buffer.item(i) = '%U'
166         loop
167            str.extend(sprintf_buffer.item(i))
168            i := i + 1
169         end
170      end
171
172   append_in_scientific (str: STRING; f: INTEGER)
173      local
174         i: INTEGER
175      do
176         from
177            sprintf(sprintf_buffer, 'e', f, Current)
178            i := 0
179         until
180            sprintf_buffer.item(i) = '%U'
181         loop
182            str.extend(sprintf_buffer.item(i))
183            i := i + 1
184         end
185      end
186
187   out_in_tagged_out_memory, fill_tagged_out_memory
188      do
189         Current.append_in(tagged_out_memory)
190      end
191
192feature {ANY} -- Maths functions:
193   frozen sqrt: like Current
194      external "built_in"
195      end
196
197   frozen sin: like Current
198      external "built_in"
199      end
200
201   frozen cos: like Current
202      external "built_in"
203      end
204
205   frozen tan: like Current
206      external "built_in"
207      end
208
209   frozen asin: like Current
210      external "built_in"
211      end
212
213   frozen acos: like Current
214      external "built_in"
215      end
216
217   frozen atan: like Current
218      external "built_in"
219      end
220
221   frozen atan2 (x: like Current): like Current
222      external "built_in"
223      end
224
225   frozen sinh: like Current
226      external "built_in"
227      end
228
229   frozen cosh: like Current
230      external "built_in"
231      end
232
233   frozen tanh: like Current
234      external "built_in"
235      end
236
237   frozen exp: like Current
238      external "built_in"
239      end
240
241   frozen log: like Current
242      external "built_in"
243      end
244
245   frozen log10: like Current
246      external "built_in"
247      end
248
249   frozen pow (e: like Current): like Current
250      external "built_in"
251      end
252
253feature {ANY} -- Hashing:
254   hash_code: INTEGER
255      deferred
256      end
257
258feature {ANY} -- Miscellaneous:
259   sign: INTEGER_8
260      do
261         if Current < {REAL_32 0.0 } then
262            Result := -1
263         elseif Current > {REAL_32 0.0 } then
264            Result := 1
265         else
266            Result := 0
267         end
268      end
269
270   mantissa_bits: INTEGER_8
271      deferred
272      end
273
274   exponent_bits: INTEGER_8
275      deferred
276      end
277
278feature {}
279   sprintf_buffer: NATIVE_ARRAY[CHARACTER]
280      once
281         Result := Result.calloc(1024)
282      end
283
284   sprintf (buffer: NATIVE_ARRAY[CHARACTER]; mode: CHARACTER; f: INTEGER; value: like Current)
285         -- Put in the `buffer' a viewable version of the `value' using `mode' with `f' digits for the fractional
286         -- part. Assume the `buffer' is large enough.
287      require
288         mode = 'f' xor mode = 'e'
289         f >= 0
290      deferred
291      end
292
293end -- class REAL_GENERAL
294--
295-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
296--
297-- Permission is hereby granted, free of charge, to any person obtaining a copy
298-- of this software and associated documentation files (the "Software"), to deal
299-- in the Software without restriction, including without limitation the rights
300-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
301-- copies of the Software, and to permit persons to whom the Software is
302-- furnished to do so, subject to the following conditions:
303--
304-- The above copyright notice and this permission notice shall be included in
305-- all copies or substantial portions of the Software.
306--
307-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
308-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
309-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
310-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
311-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
312-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
313-- THE SOFTWARE.