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