PageRenderTime 79ms CodeModel.GetById 70ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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