/src/lib/numeric/internal/integer_general_number.e

http://github.com/tybor/Liberty · Specman e · 164 lines · 122 code · 16 blank · 26 comment · 4 complexity · f54516f879d9f4cd8975ae70128882c0 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 INTEGER_GENERAL_NUMBER
  5. --
  6. -- To implement NUMBER (do not use this class, see NUMBER).
  7. --
  8. inherit
  9. NUMBER
  10. redefine abs
  11. end
  12. feature {ANY}
  13. abs: INTEGER_GENERAL_NUMBER
  14. do
  15. if is_negative then
  16. Result ::= -Current
  17. else
  18. Result := Current
  19. end
  20. end
  21. factorial: NUMBER
  22. do
  23. if is_zero or else is_one then
  24. Result := one
  25. else
  26. from
  27. put_into_mutable_big_integer(mutable_register1)
  28. mutable_register2.copy(mutable_register1)
  29. mutable_register3.from_integer(2)
  30. until
  31. mutable_register3.is_equal(mutable_register2)
  32. loop
  33. mutable_register1.multiply(mutable_register3)
  34. mutable_register3.add_integer(1)
  35. end
  36. Result := mutable_register1.to_integer_general_number
  37. end
  38. end
  39. numerator: INTEGER_GENERAL_NUMBER
  40. do
  41. Result := Current
  42. end
  43. denominator: INTEGER_GENERAL_NUMBER
  44. do
  45. Result := integer_general_number_one
  46. end
  47. feature {ANY}
  48. append_decimal_in (buffer: STRING; digits: INTEGER; all_digits: BOOLEAN)
  49. local
  50. n: INTEGER
  51. do
  52. append_in(buffer)
  53. if all_digits then
  54. from
  55. buffer.extend('.')
  56. until
  57. n >= digits
  58. loop
  59. buffer.extend('0')
  60. n := n + 1
  61. end
  62. end
  63. end
  64. feature {NUMBER} -- Implementation:
  65. integer_divide_integer_64_number (other: INTEGER_64_NUMBER): INTEGER_GENERAL_NUMBER
  66. require
  67. other /= Void
  68. deferred
  69. ensure
  70. Result /= Void
  71. end
  72. remainder_of_divide_integer_64_number (other: INTEGER_64_NUMBER): INTEGER_GENERAL_NUMBER
  73. require
  74. other /= Void
  75. deferred
  76. ensure
  77. Result /= Void
  78. end
  79. integer_divide_big_integer_number (other: BIG_INTEGER_NUMBER): INTEGER_GENERAL_NUMBER
  80. require
  81. other /= Void
  82. deferred
  83. ensure
  84. Result /= Void
  85. end
  86. remainder_of_divide_big_integer_number (other: BIG_INTEGER_NUMBER): INTEGER_GENERAL_NUMBER
  87. require
  88. other /= Void
  89. deferred
  90. ensure
  91. Result /= Void
  92. end
  93. feature {NUMBER} -- Implementation:
  94. gcd_with_big_integer_number (other: BIG_INTEGER_NUMBER): INTEGER_GENERAL_NUMBER
  95. do
  96. put_into_mutable_big_integer(mutable_register1)
  97. other.put_into_mutable_big_integer(mutable_register2)
  98. mutable_register1.gcd(mutable_register2)
  99. Result := mutable_register1.to_integer_general_number
  100. end
  101. feature {NUMBER}
  102. put_into_mutable_big_integer (mut: MUTABLE_BIG_INTEGER)
  103. require
  104. mut /= Void
  105. deferred
  106. ensure
  107. mut.to_integer_general_number.is_equal(Current)
  108. end
  109. feature {INTEGER_GENERAL_NUMBER}
  110. integer_general_number_zero: INTEGER_GENERAL_NUMBER
  111. once
  112. Result ::= zero
  113. ensure
  114. Result.is_zero
  115. end
  116. integer_general_number_one: INTEGER_GENERAL_NUMBER
  117. once
  118. Result ::= one
  119. ensure
  120. Result.is_one
  121. end
  122. integer_general_number_one_negative: INTEGER_GENERAL_NUMBER
  123. once
  124. Result ::= -integer_general_number_one
  125. ensure
  126. (-Result).is_one
  127. end
  128. end -- class INTEGER_GENERAL_NUMBER
  129. --
  130. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  131. --
  132. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  133. -- of this software and associated documentation files (the "Software"), to deal
  134. -- in the Software without restriction, including without limitation the rights
  135. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  136. -- copies of the Software, and to permit persons to whom the Software is
  137. -- furnished to do so, subject to the following conditions:
  138. --
  139. -- The above copyright notice and this permission notice shall be included in
  140. -- all copies or substantial portions of the Software.
  141. --
  142. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  143. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  144. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  145. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  146. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  147. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  148. -- THE SOFTWARE.