/src/lib/numeric/internal/fraction_general_number.e

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