/src/lib/numeric/integer_16.e

http://github.com/tybor/Liberty · Specman e · 203 lines · 138 code · 28 blank · 37 comment · 3 complexity · 2a04c43d8ffdd48d790a42bdc99e4418 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class INTEGER_16
  5. insert
  6. INTEGER_GENERAL
  7. feature {ANY} -- Conversions:
  8. fit_integer_8: BOOLEAN
  9. -- Does `Current' fit in INTEGER_8?
  10. do
  11. if Current >= -128 then
  12. Result := Current <= 127
  13. end
  14. ensure
  15. Result = Current.in_range(-128, 127)
  16. end
  17. to_integer_8: INTEGER_8
  18. -- Explicit conversion to INTEGER_8.
  19. require
  20. fit_integer_8
  21. external "built_in"
  22. ensure
  23. Current.is_equal(Result)
  24. end
  25. to_integer_32: INTEGER_32
  26. -- Explicit conversion to INTEGER_32.
  27. do
  28. Result := Current
  29. ensure
  30. Current = Result
  31. end
  32. to_integer_64: INTEGER_64
  33. -- Explicit conversion to INTEGER_64.
  34. do
  35. Result := Current
  36. ensure
  37. Current = Result
  38. end
  39. fit_natural_8: BOOLEAN
  40. -- Does `Current' fit in NATURAL_8?
  41. do
  42. if Current >= 0 then
  43. Result := Current <= 255
  44. end
  45. ensure
  46. Result = Current.in_range(0, 255)
  47. end
  48. to_natural_8: NATURAL_8
  49. -- Explicit conversion to NATURAL_8.
  50. require
  51. fit_natural_8
  52. external "built_in"
  53. ensure
  54. Result.to_integer_16 = Current
  55. end
  56. to_natural_16: NATURAL_16
  57. -- Explicit conversion to NATURAL_16.
  58. require
  59. Current >= 0
  60. external "built_in"
  61. ensure
  62. Result.to_integer_16 = Current
  63. end
  64. to_natural_32: NATURAL_32
  65. -- Explicit conversion to NATURAL_32.
  66. require
  67. Current >= 0
  68. external "built_in"
  69. ensure
  70. Result.to_integer_16 = Current
  71. end
  72. to_natural_64: NATURAL_64
  73. -- Explicit conversion to NATURAL_64.
  74. require
  75. Current >= 0
  76. external "built_in"
  77. ensure
  78. Result.to_integer_16 = Current
  79. end
  80. to_real_32: REAL_32
  81. -- Explicit conversion to REAL_32.
  82. do
  83. Result := Current
  84. end
  85. to_real_64: REAL_64
  86. -- Explicit conversion to REAL_64.
  87. do
  88. Result := Current
  89. end
  90. to_number: NUMBER
  91. -- Explicit conversion to NUMBER.
  92. local
  93. number_tools: NUMBER_TOOLS
  94. do
  95. Result := number_tools.from_integer(Current)
  96. ensure then
  97. Result @= Current
  98. end
  99. decimal_digit: CHARACTER
  100. do
  101. Result := (Current + '0'.code).to_character
  102. end
  103. hexadecimal_digit: CHARACTER
  104. do
  105. if Current <= 9 then
  106. Result := (Current + '0'.code).to_character
  107. else
  108. Result := ('A'.code + (Current - 10)).to_character
  109. end
  110. end
  111. infix "|..|" (other: INTEGER_16): INTEGER_RANGE[INTEGER_16]
  112. require
  113. Current <= other
  114. do
  115. create Result.make(to_integer_32, other.to_integer_32, integer_range_itemize, integer_range_indexize)
  116. end
  117. feature {}
  118. integer_range_itemize: FUNCTION[TUPLE[INTEGER], INTEGER_16]
  119. once
  120. Result := agent (i: INTEGER): INTEGER_16 do Result := i.to_integer_16 end (?)
  121. end
  122. integer_range_indexize: FUNCTION[TUPLE[INTEGER_16], INTEGER]
  123. once
  124. Result := agent (i: INTEGER_16): INTEGER do Result := i.to_integer_32 end (?)
  125. end
  126. feature {ANY}
  127. low_8: INTEGER_8
  128. -- The 8 low bits of `Current' (i.e. the right-most part).
  129. external "built_in"
  130. end
  131. high_8: INTEGER_8
  132. -- The 8 high bits of `Current' (i.e. the left-most part).
  133. do
  134. Result := (Current |>> 8).low_8
  135. end
  136. one: INTEGER_8 1
  137. zero: INTEGER_8 0
  138. hash_code: INTEGER
  139. do
  140. Result := Current & 0x7FFF
  141. end
  142. sqrt: REAL
  143. do
  144. Result := to_real_64.sqrt
  145. end
  146. log: REAL
  147. do
  148. Result := to_real_64.log
  149. end
  150. log10: REAL
  151. do
  152. Result := to_real_64.log10
  153. end
  154. bit_count: INTEGER_8 16
  155. end -- class INTEGER_16
  156. --
  157. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  158. --
  159. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  160. -- of this software and associated documentation files (the "Software"), to deal
  161. -- in the Software without restriction, including without limitation the rights
  162. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  163. -- copies of the Software, and to permit persons to whom the Software is
  164. -- furnished to do so, subject to the following conditions:
  165. --
  166. -- The above copyright notice and this permission notice shall be included in
  167. -- all copies or substantial portions of the Software.
  168. --
  169. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  170. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  171. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  172. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  173. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  174. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  175. -- THE SOFTWARE.