note
	description: "[
				An arbitrary precision library for integer numbers.
				
				Creation
					make_from_string,
					make_from_integer64,
					make_from_integer32,
		
				Queries
					absolute: BIG_INTEGER
					as_integer32: INTEGER
					as_integer64: INTEGER
					divide (other: BIG_INTEGER): BIG_INTEGER
					gcd (other: BIG_INTEGER): BIG_INTEGER
					is_equal (other: BIG_INTEGER): BOOLEAN
					is_greater alias ">" (other: [like Current] BIG_INTEGER): BOOLEAN
					is_greater_equal alias ">=" (other: [like Current] BIG_INTEGER): BOOLEAN
					is_less alias "<" (other: BIG_INTEGER): BOOLEAN
					is_less_equal alias "<=" (other: [like Current] BIG_INTEGER): BOOLEAN
					max (other: [like Current] BIG_INTEGER): [like Current] BIG_INTEGER
					min (other: [like Current] BIG_INTEGER): [like Current] BIG_INTEGER
					minus alias "-" (other: BIG_INTEGER): BIG_INTEGER
					opposite alias "-": BIG_INTEGER
					out: STRING
					plus alias "+" (other: BIG_INTEGER): BIG_INTEGER
					power alias "^" (other: INTEGER): BIG_INTEGER
					product alias "*" (other: BIG_INTEGER): BIG_INTEGER
					quotient alias "//" (other: BIG_INTEGER): BIG_INTEGER
					remainder alias "\\" (other: BIG_INTEGER): BIG_INTEGER
					square: BIG_INTEGER
	]"
	author: "JSO"
	date: "$17 July, 2018$"
	revision: "$0.91$"

class interface
	BIG_INTEGER

create 
	make_from_string (s: STRING_8)
			-- Create a BIG_INTEGER from string s
		require
			non_empty: not s.is_empty
			has_correct_format: is_string_int (s)

	make_from_integer64 (int: INTEGER_64)
			-- Create a BIG_INTEGER from int

	make_from_integer32 (int: INTEGER_32)
			-- Create a BIG_INTEGER from int

	default_create
			-- Create BIG_INTEGER 0
		require -- from  ANY
			True
		ensure then
				item.is_equal ("0")

convert
	default_create ({STRING_8}),
	default_create ({INTEGER_64}),
	default_create ({INTEGER_32}),
	default_create: {INTEGER_64},
	default_create: {INTEGER_32},
	default_create: {STRING_8}

feature -- Access

	generating_type: TYPE [detachable BIG_INTEGER]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generating_type_not_void: Result /= Void

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))

	frozen is_deep_equal (other: BIG_INTEGER): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)

	is_greater alias ">" (other: BIG_INTEGER): BOOLEAN
			-- Is current object greater than other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other < Current)

	is_greater_equal alias ">=" (other: BIG_INTEGER): BOOLEAN
			-- Is current object greater than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other <= Current)

	is_less_equal alias "<=" (other: BIG_INTEGER): BOOLEAN
			-- Is current object less than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = ((Current < other) or (Current ~ other))

	max (other: BIG_INTEGER): BIG_INTEGER
			-- The greater of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_smaller: Current >= other implies Result = Current
			other_if_smaller: Current < other implies Result = other

	min (other: BIG_INTEGER): BIG_INTEGER
			-- The smaller of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_greater: Current <= other implies Result = Current
			other_if_greater: Current > other implies Result = other

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))

	frozen standard_is_equal (other: BIG_INTEGER): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)

	three_way_comparison (other: BIG_INTEGER): INTEGER_32
			-- If current object equal to other, 0;
			-- if smaller, -1; if greater, 1
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			equal_zero: (Result = 0) = (Current ~ other)
			smaller_negative: (Result = -1) = (Current < other)
			greater_positive: (Result = 1) = (Current > other)
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
	
feature -- Duplication

	copy (other: BIG_INTEGER)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: Current ~ other

	frozen deep_copy (other: BIG_INTEGER)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)

	frozen deep_twin: BIG_INTEGER
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)

	frozen standard_copy (other: BIG_INTEGER)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)

	frozen standard_twin: BIG_INTEGER
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)

	frozen twin: BIG_INTEGER
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
	
feature -- Basic operations

	frozen default: detachable BIG_INTEGER
			-- Default value of object's type
			-- (from ANY)

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
	
feature -- comparison

	is_equal (other: BIG_INTEGER): BOOLEAN
			-- Is other equal to Current?
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		ensure then -- from COMPARABLE
			trichotomy: Result = (not (Current < other) and not (other < Current))

	is_less alias "<" (other: BIG_INTEGER): BOOLEAN
			-- Is Current less than other?
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			asymmetric: Result implies not (other < Current)
	
feature -- conversion

	as_integer32: INTEGER_32
			-- represent as a integer 32
		require
			valid_int32: out.is_integer_32

	as_integer64: INTEGER_64
			-- represent as a integer 64
		require
			valid_int64: out.is_integer_64
	
feature -- is_string_int

	is_string_int (s: STRING_8): BOOLEAN
			-- Is the given string a representation of big integer?
		require
			non_empty: not s.is_empty
	
feature -- operations

	absolute: BIG_INTEGER
			-- Returns the absolute value of Current
		ensure
				Result >= Result.zero

	divide (other: BIG_INTEGER): BIG_INTEGER
			-- Returns the result of dividing Current from other
			-- Was declared in BIG_INTEGER as synonym of quotient.
		require
			denominator_non_zero: other /~ zero

	exp (other: INTEGER_32): BIG_INTEGER
			-- Returns the result of raising Current to the power other
			-- Was declared in BIG_INTEGER as synonym of power.
		require
			power_non_zero: other >= 0

	gcd (other: BIG_INTEGER): BIG_INTEGER
			-- Return the positive greatest common divisor of Current and other
		require
			both_not_zero: Current /~ zero or other /~ zero

	minus alias "-" (other: BIG_INTEGER): BIG_INTEGER
			-- Returns the result of subtracting other from Current
			-- Was declared in BIG_INTEGER as synonym of subtract.

	negate
			-- negates the number

	plus alias "+" (other: BIG_INTEGER): BIG_INTEGER
			-- Returns the result of adding other to Current

	power alias "^" (other: INTEGER_32): BIG_INTEGER
			-- Returns the result of raising Current to the power other
			-- Was declared in BIG_INTEGER as synonym of exp.
		require
			power_non_zero: other >= 0

	product alias "*" (other: BIG_INTEGER): BIG_INTEGER
			-- Returns the result of multiplying Current with other

	quotient alias "//" (other: BIG_INTEGER): BIG_INTEGER
			-- Returns the result of dividing Current from other
			-- Was declared in BIG_INTEGER as synonym of divide.
		require
			denominator_non_zero: other /~ zero

	remainder alias "\\" (other: BIG_INTEGER): BIG_INTEGER
			-- Return the result of Current mod other (Consistent with WolframAlpha's mod)
		require
			non_zero: other /~ zero

	square: BIG_INTEGER
			-- Returns the result of Current * Current

	subtract (other: BIG_INTEGER): BIG_INTEGER
			-- Returns the result of subtracting other from Current
			-- Was declared in BIG_INTEGER as synonym of minus.
	
feature -- queries

	divisible (other: BIG_INTEGER): BOOLEAN
			-- may current object be divided by other?

	identity alias "+": BIG_INTEGER
			-- unary plus

	is_integer: BOOLEAN
			-- Is string representation item an int?

	is_natural: BOOLEAN
			-- Is Current a natural number?

	is_natural1: BOOLEAN
			-- Is `Current a Natural1 number?

	one: BIG_INTEGER
			-- neutral element for "*" and "/"

	opposite alias "-": BIG_INTEGER
			-- unary minus

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
		ensure -- from ANY
			out_not_void: Result /= Void

	zero: BIG_INTEGER
			-- neutral element for "+" and "-"
	
invariant
		Default_precision = 0
		is_integer

		-- from COMPARABLE
	irreflexive_comparison: not (Current < Current)

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

note
	copyright: "Copyright (c) SEL, York University"
	license: "MIT"
	todo: "[
		integer division \\ is veru ineficient. Fpr js algorithm see:
		www.javascripter.net/math/calculators/100digitbigintcalculator.htm
		and {TEST_INT_JSO}t51.
		To generate large random numbers:
		python3
		import random
		random.getrandbits(128) -- 128 bits
	]"

end -- class BIG_INTEGER

Generated by ISE EiffelStudio