class MUTABLE_BIG_INTEGER

Features exported to NUMBER

A class used to represent multiprecision integers that makes efficient use of allocated space by allowing a number to occupy only part of an array so that the arrays do not have to be reallocated as often. When performing an operation with many iterations the array used to hold a number is only reallocated when necessary and does not have to be the same size as the number it represents. A mutable number allows calculations to occur on the same number without having to create a new number for every step of the calculation as it occurs with NUMBERs.

Direct parents

conformant parents

COMPARABLE, HASHABLE

non-conformant parents

PLATFORM

Summary

creation features

exported features

Creation / initialization from INTEGER_32 or INTEGER_64:

Creation / initialization from STRING:

Conversion tool

Addition:

Subtract:

To divide:

GCD

To multiply:

Comparison:

Printing:

Miscellaneous:

Details

from_integer (value: INTEGER)

Create or initialize Current using value as an initializer.

ensure

  • to_integer = value

from_integer_64 (value: INTEGER_64)

Create or set Current using value as an initializer.

ensure

  • to_integer_64 = value

from_string (str: STRING)

Create or initialize Current using value as an initializer. (value = [-][0-9]^+)

copy (other: MUTABLE_BIG_INTEGER)

Update current object using fields of object attached to other, so as to yield equal objects. Note: you can't copy object from a different dynamic type.

require

  • same_dynamic_type(other)

ensure

  • is_equal(other)

from_integer (value: INTEGER)

Create or initialize Current using value as an initializer.

ensure

  • to_integer = value

is_integer: BOOLEAN

Does Current fit on an INTEGER_32?

ensure

  • Result implies is_integer_64

to_integer: INTEGER

Convert Current as a 32 bit INTEGER.

require

  • is_integer

from_integer_64 (value: INTEGER_64)

Create or set Current using value as an initializer.

ensure

  • to_integer_64 = value

is_integer_64: BOOLEAN

Does Current fit on an INTEGER_64?

ensure

  • not Result implies not is_integer

to_integer_64: INTEGER_64

Convert Current as a INTEGER_64.

require

  • is_integer_64

from_string (str: STRING)

Create or initialize Current using value as an initializer. (value = [-][0-9]^+)

force_to_real_64: REAL

only a tool unsigned conversion *** require ou changer export ? *** (Dom Oct 4th 2004) ***

from_native_array (na: NATIVE_ARRAY [E_][INTEGER], cap: INTEGER, neg: BOOLEAN)

require

  • na.item(cap - 1) /= 0

to_integer_general_number: INTEGER_GENERAL_NUMBER
add (other: MUTABLE_BIG_INTEGER)

Add other into Current. (See also add_integer add_integer_64 and add_natural.)

require

  • other /= Void

add_to (other: MUTABLE_BIG_INTEGER, res: MUTABLE_BIG_INTEGER)

Add other and Current, and put the result in res.

require

  • other /= Void
  • res /= Void
  • res /= Current
  • res /= other

add_integer (other: INTEGER)

Add other into Current.

add_integer_64 (other: INTEGER_64)

Add other into Current.

add_natural (other: MUTABLE_BIG_INTEGER)

Same behavior as add, but this one works only when Current and other are both positive numbers and are both greater than zero. The only one advantage of using add_natural instead of the general add is the gain of efficiency.

require

  • not is_zero and not is_negative
  • not other.is_zero and not other.is_negative

subtract (other: MUTABLE_BIG_INTEGER)

Subtract other from Current.

require

  • other /= Void

subtract_to (other: MUTABLE_BIG_INTEGER, res: MUTABLE_BIG_INTEGER)

Subtract other from Current and put it in res.

require

  • other /= Void
  • res /= Void
  • res /= Current
  • res /= other

subtract_integer (other: INTEGER)
divide (other: MUTABLE_BIG_INTEGER)

Put the the quotient of the Euclidian division of Current by other in Current. (The contents of other is not changed.)

require

  • not other.is_zero
  • other /= Current

mod (other: MUTABLE_BIG_INTEGER)

Put the the remainder of the Euclidian division of Current by other in Current. (The contents of other is not changed.)

require

  • not other.is_zero
  • other /= Current

ensure

    divide_with_remainder_to (other: MUTABLE_BIG_INTEGER, remainder: MUTABLE_BIG_INTEGER)

    Euclidian division. Calculates the quotient and remainder of Current divided by other. Quotient is put in Current. (The contents of other is not changed.)

    require

    • not other.is_zero
    • remainder /= Void
    • remainder /= other
    • remainder /= Current

    ensure

      remainder_with_quotient_to (other: MUTABLE_BIG_INTEGER, quotient: MUTABLE_BIG_INTEGER)

      Euclidian division. Calculates the quotient and remainder of Current divided by other. Remainder is put in Current. (The contents of other is not changed.)

      Note: Uses Algorithm D in Knuth section 4.3.1.

      require

      • not other.is_zero
      • quotient /= Void
      • quotient /= other
      • quotient /= Current

      ensure

        divide_to (other: MUTABLE_BIG_INTEGER, quotient: MUTABLE_BIG_INTEGER, remainder: MUTABLE_BIG_INTEGER)

        Euclidian division. Calculates the quotient and remainder of Current divided by other. (The contents of Current and other are not changed.)

        Note: Uses Algorithm D in Knuth section 4.3.1.

        require

        • not other.is_zero
        • quotient /= Void
        • remainder /= Void
        • quotient /= other
        • quotient /= Current
        • remainder /= other
        • remainder /= Current

        ensure

          shift_left (n: INTEGER)

          Shift bits of magnitude by n position left. (Note that no bit can be lost because the storage area is automatically extended when it is necessary.)

          require

          • n > 0

          shift_right (n: INTEGER)

          Right shift Current n bits. (Current is left in normal form.)

          require

          • n > 0

          gcd (other: MUTABLE_BIG_INTEGER)

          Compute GCD of Current and other. GCD is put in Current (other is not modified).

          require

          • other /= Void

          ensure

          • is_positive or is_zero and other.is_zero

          multiply (other: MUTABLE_BIG_INTEGER)

          Multiply Current by other.

          require

          • other /= Void

          multiply_to (other: MUTABLE_BIG_INTEGER, res: MUTABLE_BIG_INTEGER)

          Multiply the contents of Current and other and place the result in res. (Current and other are not modified.)

          require

          • other /= Void
          • res /= Void
          • res /= Current

          multiply_integer (other: INTEGER, res: MUTABLE_BIG_INTEGER)

          Multiply the contents of Current and other and place the result in res. (Current is not modified.)

          require

          • res /= Current
          • res /= Void

          is_zero: BOOLEAN

          Is it 0?

          ensure

          • Result implies not is_negative

          is_one: BOOLEAN

          Is it 1?

          ensure

          • Result implies not is_negative

          is_one_negative: BOOLEAN

          Is it -1 ?

          ensure

          • Result implies is_negative

          is_negative: BOOLEAN

          Is Current negative integer?

          ensure

          • Result implies not is_positive

          is_positive: BOOLEAN

          Is Current positive integer?

          ensure

          • Result implies not is_negative

          is_even: BOOLEAN

          Is Current even?

          ensure

          • Result = not Current.is_odd

          is_odd: BOOLEAN

          Is Current odd?

          ensure

          • Result = not Current.is_even

          is_equal (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is other attached to an object considered equal to current object ?

          require

          • other /= Void

          ensure

          • commutative: generating_type = other.generating_type implies Result = other.is_equal(Current)
          • trichotomy: Result = (not (Current < other) and not (other < Current))
          • Result implies hash_code = other.hash_code

          < (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is Current strictly less than other?

          See also >, <=, >=, min, max.

          require

          • other_exists: other /= Void

          ensure

          • asymmetric: Result implies not (other < Current)

          abs_compare (other: MUTABLE_BIG_INTEGER): INTEGER

          Compare the magnitude of Current and other. Returns -1, 0 or 1 as this MutableBigInteger is numerically less than, equal to, or greater than other.

          to_string: STRING

          Create a decimal view of the Current big integer. Note: see also append_in to save memory.

          to_unicode_string: UNICODE_STRING

          Create a decimal view of the Current big integer. Note: see also append_in_unicode to save memory.

          append_in (buffer: STRING)

          Append in the buffer the equivalent of to_string. No new STRING creation during the process.

          require

          • is_printable

          append_in_unicode (buffer: UNICODE_STRING)

          Append in the buffer the equivalent of to_string. No new STRING creation during the process.

          require

          • is_printable

          to_string_format (s: INTEGER): STRING

          Same as to_string but the result is on s character and the number is right aligned. Note: see append_in_format to save memory.

          require

          • to_string.count <= s

          ensure

          • Result.count = s

          to_unicode_string_format (s: INTEGER): UNICODE_STRING

          Same as to_unicode_string but the result is on s character and the number is right aligned. Note: see append_in_unicode_format to save memory.

          require

          • to_string.count <= s

          ensure

          • Result.count = s

          append_in_format (str: STRING, s: INTEGER)

          Append the equivalent of to_string_format at the end of str. Thus you can save memory because no other STRING is allocate for the job.

          require

          • to_string.count <= s

          ensure

          • str.count >= old str.count + s

          append_in_unicode_format (str: UNICODE_STRING, s: INTEGER)

          Append the equivalent of to_unicode_string_format at the end of str. Thus you can save memory because no other UNICODE_STRING is allocate for the job.

          require

          • to_string.count <= s

          ensure

          • str.count >= old str.count + s

          is_printable: BOOLEAN

          True if decimal view of Current is short enougth to be put in a STRING.

          out_in_tagged_out_memory

          Append terse printable represention of current object in tagged_out_memory.

          ensure

          • not_cleared: tagged_out_memory.count >= old tagged_out_memory.count
          • append_only: (old tagged_out_memory.twin).is_equal(tagged_out_memory.substring(1, old tagged_out_memory.count))

          fill_tagged_out_memory

          Append a viewable information in tagged_out_memory in order to affect the behavior of out, tagged_out, etc.

          negate

          Negate the sign of Current.

          abs

          Absolute value of Current.

          sign: INTEGER_8

          Sign of Current (0 -1 or 1).

          set_with_zero

          ensure

          • is_zero

          hash_code: INTEGER

          The hash-code value of Current.

          ensure

          • good_hash_value: Result >= 0

          copy (other: MUTABLE_BIG_INTEGER)

          Update current object using fields of object attached to other, so as to yield equal objects. Note: you can't copy object from a different dynamic type.

          require

          • same_dynamic_type(other)

          ensure

          • is_equal(other)

          swap_with (other: MUTABLE_BIG_INTEGER)

          Swap the value of Current with the value of other.

          deferred is_equal (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is other attached to an object considered equal to current object ?

          require

          • other /= Void

          ensure

          • Result implies hash_code = other.hash_code
          • commutative: generating_type = other.generating_type implies Result = other.is_equal(Current)

          is_equal (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is other attached to an object considered equal to current object ?

          require

          • other /= Void

          ensure

          • trichotomy: Result = (not (Current < other) and not (other < Current))
          • commutative: generating_type = other.generating_type implies Result = other.is_equal(Current)

          <= (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is Current less than or equal other?

          See also >=, <, >, min, max.

          require

          • other_exists: other /= Void

          ensure

          • definition: Result = (Current < other or is_equal(other))

          > (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is Current strictly greater than other?

          See also <, >=, <=, min, max.

          require

          • other_exists: other /= Void

          ensure

          • definition: Result = (other < Current)

          >= (other: MUTABLE_BIG_INTEGER): BOOLEAN

          Is Current greater than or equal than other?

          See also <=, >, <, min, max.

          require

          • other_exists: other /= Void

          ensure

          • definition: Result = (other <= Current)

          in_range (lower: MUTABLE_BIG_INTEGER, upper: MUTABLE_BIG_INTEGER): BOOLEAN

          Return True if Current is in range [lower..upper]

          See also min, max, compare.

          ensure

          • Result = (Current >= lower and Current <= upper)

          compare (other: MUTABLE_BIG_INTEGER): INTEGER

          If current object equal to other, 0 if smaller, -1; if greater, 1.

          See also min, max, in_range.

          require

          • other_exists: other /= Void

          ensure

          • equal_zero: Result = 0 = is_equal(other)
          • smaller_negative: Result = -1 = (Current < other)
          • greater_positive: Result = 1 = (Current > other)

          three_way_comparison (other: MUTABLE_BIG_INTEGER): INTEGER

          If current object equal to other, 0 if smaller, -1; if greater, 1.

          See also min, max, in_range.

          require

          • other_exists: other /= Void

          ensure

          • equal_zero: Result = 0 = is_equal(other)
          • smaller_negative: Result = -1 = (Current < other)
          • greater_positive: Result = 1 = (Current > other)

          min (other: MUTABLE_BIG_INTEGER): MUTABLE_BIG_INTEGER

          Minimum of Current and other.

          See also max, in_range.

          require

          • other /= Void

          ensure

          • Result <= Current and then Result <= other
          • compare(Result) = 0 or else other.compare(Result) = 0

          max (other: MUTABLE_BIG_INTEGER): MUTABLE_BIG_INTEGER

          Maximum of Current and other.

          See also min, in_range.

          require

          • other /= Void

          ensure

          • Result >= Current and then Result >= other
          • compare(Result) = 0 or else other.compare(Result) = 0

          Class invariant